Skip to content

Commit 93157cd

Browse files
author
yunkui.zhou
committed
improve
1 parent 06e591b commit 93157cd

File tree

2 files changed

+42
-42
lines changed

2 files changed

+42
-42
lines changed

client/Components/Slide/index.js

+41-41
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
* Created by zhouyunkui on 2017/6/9.
33
*/
44
/* eslint-disable */
5-
import React, { Component} from 'react';
5+
import React, {Component} from 'react';
66
import PropTypes from 'prop-types';
77
import './Slide.less';
8-
import { preventDocMove, offPreventDefault, } from '../../pages/example/common/js/preventDocMove';
8+
import {preventDocMove, offPreventDefault, } from '../../pages/example/common/js/preventDocMove';
99
const VERTICAL = 'vertical',
1010
HORIZONTAL = 'horizontal';
1111
export class Slide extends Component {
@@ -38,20 +38,20 @@ export class Slide extends Component {
3838
}
3939
constructor(options){
4040
super(options);
41-
const { children, defaultActiveIndex, activeIndex, width, height, } = this.props;
41+
const {children, defaultActiveIndex, activeIndex, width, height, } = this.props;
4242
const count = React.Children.count( children );
4343
this.state = {
4444
transform: - ( defaultActiveIndex * width ),
45-
transition: 'transition',
46-
region: this.progressRegion( { width, count } ),
45+
transition: false,
46+
region: this.progressRegion( {width, count } ),
4747
activeIndex: activeIndex ,
4848
width: width,
4949
height: height,
5050
}
5151
}
5252
getSlidePane = () => {
53-
const { children } = this.props;
54-
const { width, height } = this.state;
53+
const {children } = this.props;
54+
const {width, height } = this.state;
5555
return React.Children.map( children , (child) => {
5656
if( !child ) return;
5757
const order = parseInt(child.props.order, 10);
@@ -69,8 +69,8 @@ export class Slide extends Component {
6969
* @returns {boolean}
7070
*/
7171
slideFeedbackSwitch = ( direction ) => {
72-
const { slideFeedback, children, } = this.props;
73-
const { activeIndex } = this.state;
72+
const {slideFeedback, children, } = this.props;
73+
const {activeIndex } = this.state;
7474
const count = React.Children.count( children );
7575
if ( !slideFeedback ) {
7676
if ( direction === -1 ) {
@@ -88,8 +88,8 @@ export class Slide extends Component {
8888
return true;
8989
}
9090
whenSlideReachFeedbackTransformLimit = ( transformDistance ) => {
91-
const { region } = this.state;
92-
const { slideFeedbackTransformLimit, slideFeedback, } = this.props;
91+
const {region } = this.state;
92+
const {slideFeedbackTransformLimit, slideFeedback, } = this.props;
9393
const first = region[0];
9494
const last = region[region.length-1];
9595
const howFarFromBeginToCurrent = transformDistance - first,
@@ -112,17 +112,17 @@ export class Slide extends Component {
112112
return finalTransformDistance;
113113
}
114114
start = ( event ) => {
115-
const { preSlide } = this.props;
116-
const { activeIndex } = this.state;
117-
preSlide ? preSlide( { activeIndex, } ) : '';
115+
const {preSlide } = this.props;
116+
const {activeIndex } = this.state;
117+
preSlide ? preSlide( {activeIndex, } ) : '';
118118
this.startX = event.changedTouches[0].clientX;
119119
this.startY = event.changedTouches[0].clientY;
120120
this.switchRate = undefined;
121121
this.preventDocMove = false;
122122
this.cacheTransform = this.state.transform;
123123
}
124124
move = ( event ) => {
125-
const { layout } = this.props;
125+
const {layout } = this.props;
126126
this.moveX = event.changedTouches[0].clientX;
127127
this.moveY = event.changedTouches[0].clientY;
128128
let currentMove;
@@ -145,14 +145,14 @@ export class Slide extends Component {
145145
let transformDistance = this.whenSlideReachFeedbackTransformLimit( this.cacheTransform + currentMove );
146146
this.setState({
147147
transform: transformDistance,
148-
transition: ''
148+
transition: false
149149
})
150150
}
151151
}
152152
end = ( event ) => {
153153
this.preventDocMove = false;
154-
const { afterSlide, layout, } = this.props;
155-
const { transform, } = this.state;
154+
const {afterSlide, layout, } = this.props;
155+
const {transform, } = this.state;
156156
this.endX = event.changedTouches[0].clientX;
157157
this.endY = event.changedTouches[0].clientY
158158
let distance;
@@ -162,16 +162,16 @@ export class Slide extends Component {
162162
distance = this.endY - this.startY;
163163
}
164164
const direction = distance > 0 ? 1 : ( distance < 0 ? -1 : 0 );
165-
const { transform: newTransform, index: activeIndex}= this.onlySlideInRegion( direction );
165+
const {transform: newTransform, index: activeIndex}= this.onlySlideInRegion( direction );
166166
this.setState( {
167167
transform: newTransform,
168-
transition: 'transition',
168+
transition: true,
169169
activeIndex: activeIndex
170170
},() => {
171-
afterSlide ? afterSlide( { activeIndex, } ) : '';
171+
afterSlide ? afterSlide( {activeIndex, } ) : '';
172172
})
173173
}
174-
binaryChop = ( { region, target, direction } ) => {
174+
binaryChop = ( {region, target, direction } ) => {
175175
//此处region由大到小排列
176176
let left = 0,
177177
right = region.length - 1,
@@ -216,7 +216,7 @@ export class Slide extends Component {
216216
//手指向右滑动
217217
distance = target - region[result[1]];
218218
}
219-
const { transformLimit } = this.props;
219+
const {transformLimit } = this.props;
220220
if ( distance >= transformLimit ) {
221221
if ( direction === -1 ) {
222222
return {
@@ -258,8 +258,8 @@ export class Slide extends Component {
258258
}
259259
}
260260
onlySlideInRegion = ( direction ) => {
261-
const { region, transform: target } = this.state;
262-
return this.binaryChop( { region, target, direction } );
261+
const {region, transform: target } = this.state;
262+
return this.binaryChop( {region, target, direction } );
263263
}
264264
progressRegion = ({size, count}) => {
265265
const region = [];
@@ -277,39 +277,39 @@ export class Slide extends Component {
277277
//获取元素宽度和高度,因为css中对Slide中的包裹元素设置了width和height都是100%。经历两次render
278278
getLayout = ( instance ) => {
279279
if ( !instance ) return;
280-
const { children, layout, defaultActiveIndex, } = this.props;
280+
const {children, layout, defaultActiveIndex, } = this.props;
281281
const count = React.Children.count( children );
282282
const width = instance.offsetWidth,
283283
height = instance.offsetHeight;
284284
const size = (layout === HORIZONTAL) ? width : height;
285285
this.setState( {
286286
width,
287287
height,
288-
region: this.progressRegion( { size, count } ),
289-
transform: - ( defaultActiveIndex * size ),
288+
region: this.progressRegion( {size, count } ),
289+
transform: - ( defaultActiveIndex * size )
290290
})
291291
}
292292
componentDidMount() {
293-
preventDocMove.call( this );
293+
preventDocMove.call(this);
294294
}
295295
componentWillUnmount() {
296-
offPreventDefault.call( this );
296+
offPreventDefault.call(this);
297297
}
298298
render() {
299299
console.log('Slide')
300-
const { children, style, className, layout, } = this.props;
301-
const { width, height, transition, transform, } = this.state;
300+
const {children, style, className, layout, } = this.props;
301+
const {width, height, transition, transform, } = this.state;
302302
const count = React.Children.count(children);
303303
const styleOfSlideCollection = {
304-
width: `${ layout === HORIZONTAL ? width * count: width }px`,
305-
height: `${ layout === HORIZONTAL ? height : height * count }px`,
306-
transform: `translate${ layout === HORIZONTAL ? 'X' : 'Y' }(${ transform }px)`,
307-
WebkitTransform: `translate${ layout === HORIZONTAL ? 'X' :'Y' }(${ transform }px)`,
308-
transition: `${ transition ? 'transform .6s cubic-bezier(.53,.54,.02,.99)' : ''}`,
309-
WebkitTransition: `${ transition ? '-webkit-transform .6s cubic-bezier(.53,.54,.02,.99)' : ''}`
304+
width: `${layout === HORIZONTAL ? width * count: width }px`,
305+
height: `${layout === HORIZONTAL ? height : height * count }px`,
306+
transform: `translate${layout === HORIZONTAL ? 'X' : 'Y' }(${transform }px)`,
307+
WebkitTransform: `translate${layout === HORIZONTAL ? 'X' :'Y' }(${transform }px)`,
308+
transition: `${transition ? 'transform .6s cubic-bezier(.53,.54,.02,.99)' : ''}`,
309+
WebkitTransition: `${transition ? '-webkit-transform .6s cubic-bezier(.53,.54,.02,.99)' : ''}`
310310
};
311311
const wrapClassName = `slide-wrap ${className}`;
312-
return <div ref={ this.getLayout } className={ wrapClassName } style={ style }>
312+
return <div ref={this.getLayout } className={wrapClassName } style={style }>
313313
<div
314314
className={`slide-collection`}
315315
style={styleOfSlideCollection}
@@ -340,14 +340,14 @@ export class SlideItem extends Component {
340340
}
341341
render(){
342342
console.log('SlideItem')
343-
const { width, height, style, className, children, } = this.props;
343+
const {width, height, style, className, children, } = this.props;
344344
const styleOfSlidePane = Object.assign( {}, style, {
345345
width: `${width}px`,
346346
height: `${height}px`,
347347
})
348348
const sectionClassName = `slide-item ${className}`;
349349
return <section className={sectionClassName} style={styleOfSlidePane}>
350-
{ children }
350+
{children }
351351
</section>
352352
}
353353
}

client/pages/example/Slide/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class Index extends Hoc {
3030
<div>
3131
<p>这是一个水平滚动slide实例</p>
3232
<div style={style}>
33-
<Slide layout={'horizontal'} defaultActiveIndex={0} className={''} style={{}} slideFeedback={false} preSlide={function ({activeIndex}) {
33+
<Slide layout='horizontal' defaultActiveIndex={0} className={''} style={{}} slideFeedback={false} preSlide={function ({activeIndex}) {
3434
console.log('滑动开始:当前的slide序号', activeIndex)
3535
}} afterSlide={function ({activeIndex}) {
3636
console.log('滑动结束:当前的slide序号', activeIndex);

0 commit comments

Comments
 (0)