Showing
1 changed file
with
98 additions
and
0 deletions
@@ -24,6 +24,103 @@ var start = 0, | @@ -24,6 +24,103 @@ var start = 0, | ||
24 | 24 | ||
25 | lazyLoad($('img.lazy')); | 25 | lazyLoad($('img.lazy')); |
26 | 26 | ||
27 | +function preventDefault(e) { | ||
28 | + e = e || window.event; | ||
29 | + e.preventDefault && e.preventDefault(); | ||
30 | + e.returnValue = false; | ||
31 | +} | ||
32 | + | ||
33 | +function stopPropagation(e){ | ||
34 | + e = e || window.event; | ||
35 | + e.stopPropagation && e.stopPropagation(); | ||
36 | + e.cancelBubble = false; | ||
37 | +} | ||
38 | + | ||
39 | +function innerScroll(e){ | ||
40 | + // 阻止冒泡到document | ||
41 | + // document上已经preventDefault | ||
42 | + stopPropagation(e); | ||
43 | + | ||
44 | + var delta = e.wheelDelta || e.detail || 0; | ||
45 | + var box = $(this).get(0); | ||
46 | + | ||
47 | + if($(box).height() + box.scrollTop >= box.scrollHeight){ | ||
48 | + if(delta < 0) { | ||
49 | + preventDefault(e); | ||
50 | + return false; | ||
51 | + } | ||
52 | + } | ||
53 | + if(box.scrollTop === 0){ | ||
54 | + if(delta > 0) { | ||
55 | + preventDefault(e); | ||
56 | + return false; | ||
57 | + } | ||
58 | + } | ||
59 | + // 会阻止原生滚动 | ||
60 | + // return false; | ||
61 | +} | ||
62 | + | ||
63 | +var disableScroll = function(){ | ||
64 | + $(document).on('mousewheel', preventDefault); | ||
65 | + $(document).on('touchmove', preventDefault); | ||
66 | +}; | ||
67 | + | ||
68 | +var enableScroll = function(){ | ||
69 | + $(document).off('mousewheel', preventDefault); | ||
70 | + $(document).off('touchmove', preventDefault); | ||
71 | +}; | ||
72 | + | ||
73 | +// bind | ||
74 | +/**$('#closePopup').on('click', function(e){ | ||
75 | + $('#popupLayer').hide(); | ||
76 | + $('#bgMask').hide(); | ||
77 | + enableScroll(); | ||
78 | +});**/ | ||
79 | + | ||
80 | +// 内部可滚 | ||
81 | +$sideNav.on('mousewheel', innerScroll); | ||
82 | +// 外部禁用 | ||
83 | +disableScroll(); | ||
84 | + | ||
85 | + | ||
86 | +// 移动端touch重写 | ||
87 | +var startX, startY; | ||
88 | +$sideNav.on('touchstart', function(e){ | ||
89 | + startX = e.changedTouches[0].pageX; | ||
90 | + startY = e.changedTouches[0].pageY; | ||
91 | +}); | ||
92 | + | ||
93 | +// 仿innerScroll方法 | ||
94 | +$sideNav.on('touchmove', function(e){ | ||
95 | + e.stopPropagation(); | ||
96 | + | ||
97 | + var deltaX = e.changedTouches[0].pageX - startX; | ||
98 | + var deltaY = e.changedTouches[0].pageY - startY; | ||
99 | + | ||
100 | + // 只能纵向滚 | ||
101 | + if(Math.abs(deltaY) < Math.abs(deltaX)){ | ||
102 | + e.preventDefault(); | ||
103 | + return false; | ||
104 | + } | ||
105 | + | ||
106 | + var box = $(this).get(0); | ||
107 | + | ||
108 | + if($(box).height() + box.scrollTop >= box.scrollHeight){ | ||
109 | + if(deltaY < 0) { | ||
110 | + e.preventDefault(); | ||
111 | + return false; | ||
112 | + } | ||
113 | + } | ||
114 | + if(box.scrollTop === 0){ | ||
115 | + if(deltaY > 0) { | ||
116 | + e.preventDefault(); | ||
117 | + return false; | ||
118 | + } | ||
119 | + } | ||
120 | + // 会阻止原生滚动 | ||
121 | + // return false; | ||
122 | +}); | ||
123 | + | ||
27 | $('.nav-btn').on('touchstart', function(event) { | 124 | $('.nav-btn').on('touchstart', function(event) { |
28 | $sideNav.css('pointer-events', 'none'); | 125 | $sideNav.css('pointer-events', 'none'); |
29 | $mobileWrap.addClass('menu-open'); | 126 | $mobileWrap.addClass('menu-open'); |
@@ -57,6 +154,7 @@ function hideSideBar() { | @@ -57,6 +154,7 @@ function hideSideBar() { | ||
57 | height: 'auto', | 154 | height: 'auto', |
58 | overflow: 'auto' | 155 | overflow: 'auto' |
59 | }); | 156 | }); |
157 | + enableScroll(); | ||
60 | } | 158 | } |
61 | } | 159 | } |
62 | 160 |
-
Please register or login to post a comment