Skip to content

Commit 5cccc88

Browse files
李钿李钿
authored andcommitted
增加快速拖动切换图片功能
1 parent db5091f commit 5cccc88

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

src/components/ListContainer.jsx

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import {Motion, spring} from 'react-motion';
44

55
import ImageContainer from './ImageContainer'
66

7+
//快速拖动时间限制
8+
const DEDAULT_TIME_DIFF = 200;
9+
710
class ListContainer extends PureComponent {
811
static propTypes = {
912
gap: PropTypes.number,
@@ -14,6 +17,7 @@ class ListContainer extends PureComponent {
1417
}
1518

1619
state = {
20+
index: 0,
1721
left: 0,
1822
isNeedSpring: false
1923
}
@@ -31,6 +35,7 @@ class ListContainer extends PureComponent {
3135
this.maxLeft = this.perDistance * (this.length - 1);
3236

3337
this.setState({
38+
index,
3439
left: - this.perDistance * index,
3540
isNeedSpring: false
3641
})
@@ -55,6 +60,7 @@ class ListContainer extends PureComponent {
5560
handleStart = () =>{
5661
console.info("ListContainer handleStart")
5762
this.startLeft = this.state.left;
63+
this.startTime = (new Date()).getTime();
5864
this.setState({
5965
isNeedSpring: false
6066
})
@@ -74,11 +80,26 @@ class ListContainer extends PureComponent {
7480
}
7581

7682
handleEnd = () =>{
77-
let left = Math.round(this.state.left / this.perDistance) * this.perDistance
78-
if(left > 0){ left = 0}
79-
if(left < -this.maxLeft){ left = -this.maxLeft}
83+
let index, left, diffTime = (new Date()).getTime() - this.startTime;
84+
85+
//快速拖动情况下切换图片
86+
if(diffTime < DEDAULT_TIME_DIFF){
87+
if(this.state.left < this.startLeft){
88+
index = this.state.index + 1;
89+
} else{
90+
index = this.state.index - 1;
91+
}
92+
} else{
93+
index = Math.abs(Math.round(this.state.left / this.perDistance));
94+
}
95+
96+
//处理边界情况
97+
if(index < 0){ index = 0}
98+
else if(index > this.length - 1){ index = this.length - 1}
99+
80100
this.setState({
81-
left,
101+
index,
102+
left: - this.perDistance * index,
82103
isNeedSpring: true
83104
})
84105
}

0 commit comments

Comments
 (0)