-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
103 lines (98 loc) · 3.68 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/**
* Created by JetBrains WebStorm.
* User: yuzhou
* Date: 12-7-17
* Time: 下午8:49
* To change this template use File | Settings | File Templates.
*/
$(function () {
var chapters = [5, 3, 7, 4, 8];
var chapterPoint = [], currentPoint = 0;
for (var chapter = 0; chapter < chapters.length; chapter++) {
$('<div class="chapter"></div>').appendTo('#chapterScrollContent').html(chapter).css({
left:chapter * 1024
});
for (var page = 0; page < chapters[chapter]; page++) {
var pageDiv = $('<div class="page"></div>').appendTo('#pageScrollContent').html((chapter + 1) + '-' + (page + 1)).css({
left:currentPoint + page * 200
});
if (page != 0) {
pageDiv.css({
top:30,
height:150
});
}
}
chapterPoint.push(currentPoint);
if (chapters[chapter] < 6) {
$('<div class="blank"></div>').appendTo('#chapterScrollContent').css({
left:currentPoint + page * 200 + 200,
width:1024 - chapters[chapter] * 200
});
currentPoint += 1024;
}
else {
currentPoint += chapters[chapter] * 200;
}
}
var currentIndex = 0, touchBegin, touchLast, lastDistance, totalDistance;
$('#chapterScroll').on('touchstart touchmove touchend', function (e) {
if (e.type == 'touchstart') {
touchBegin = touchLast = {
x:e.originalEvent.touches[0].clientX,
y:e.originalEvent.touches[0].clientY
};
$('#chapterScrollContent').css({
'-webkit-transition-duration':'0s'
});
}
else if (e.type == 'touchmove') {
lastDistance = {
x:e.originalEvent.touches[0].clientX - touchLast.x,
y:e.originalEvent.touches[0].clientY - touchLast.y
};
touchLast = {
x:e.originalEvent.touches[0].clientX,
y:e.originalEvent.touches[0].clientY
};
var left = -currentIndex * 1024 + touchLast.x - touchBegin.x;
$('#chapterScrollContent').css({
'-webkit-transform':'translate3d(' + left + 'px,0,0)',
'-webkit-transition-duration':'0'
});
}
else if (e.type == 'touchend') {
totalDistance = touchLast.x - touchBegin.x;
if (totalDistance >= 50 || lastDistance.x > 999) {
if (currentIndex > 0) {
currentIndex -= 1;
}
}
else if (totalDistance < -50 || lastDistance.x < -999) {
if (currentIndex < chapters.length - 1) {
currentIndex += 1;
}
}
$('#chapterScrollContent').css({
'-webkit-transform':'translate3d(' + (-currentIndex) * 1024 + 'px,0,0)',
'-webkit-transition-duration':'0.5s'
});
$('#pageScroll').animate({scrollLeft:chapterPoint[currentIndex]}, 300);
}
return false;
});
$('#pageScroll').on('touchend', function (e) {
console.log(this.scrollLeft);
if (this.scrollLeft < chapterPoint[currentIndex]) {
if (currentIndex > 0) {
currentIndex -= 1;
$('#pageScroll').animate({scrollLeft:chapterPoint[currentIndex]}, 300);
e.preventDefault();
}
}
$('#chapterScrollContent').css({
'-webkit-transform':'translate3d(' + (-currentIndex) * 1024 + 'px,0,0)',
'-webkit-transition-duration':'0.5s'
});
});
});