-
Notifications
You must be signed in to change notification settings - Fork 0
/
npScroll.js
241 lines (238 loc) · 8.23 KB
/
npScroll.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/**
* jQuery Plugin - v1.1.0 - 2016-11-18
* @author huayeta
*/
;(function (factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['jquery'], factory);
} else if (typeof exports === 'object') {
// CommonJS / nodejs module
module.exports = factory(require('jquery'));
} else {
// Browser globals
factory(jQuery);
}
}(function ($) {
var npScroll=function(defaults){
var opts={
containerClass:'.np-container',
sectionClass:'.np-section',
parallaxClass:'.np-parallax',
effect:1
};
$.extend(opts,defaults);
var _this=this;
this.opts=opts;
this.$body=$('body');
this.$window=$(window);
this.wH=this.$window.height();
this.$box=$(opts.containerClass);
this.$scrollTarget=this.$box;
if(this.opts.scrollTarget)this.$scrollTarget=$(this.opts.scrollTarget);
this.$sections=this.$box.find(opts.sectionClass);
// 是否有满屏的
this.$box.find('.np-full').css('height',this.wH);
this.section_top_arr=[];
this.$sections.each(function(){
var top=$(this).offset().top;
_this.section_top_arr.push(top);
})
this.$parallaxs=this.$box.find(opts.parallaxClass);
this.pagesMax=this.$sections.size();
this.pageIndex=0;
this.$navigation=$('<div class="np-navigation-menu"></div>');
this.time=new Date().getTime();
this._events={};
//初始化
this.init();
return this;
}
npScroll.prototype.init=function(){
//初始化section
var _this=this;
this.$sections.each(function(index){
var $this=$(this);
$this.data('section',index);
$this.addClass('np-section-'+index);
//初始化导航
_this.$navigation.append('<div class="navigation-item" data-section="'+index+'"></div>')
})
this.$navigationItems=this.$navigation.find('.navigation-item');
//导航点击
this.$navigation.on('click','.navigation-item',function(){
_this.itemClick(this);
})
// 插入导航
this.$body.append(this.$navigation);
// 初始化视差
if(this.$parallaxs.size()>0){
this.$box.on('scroll',function(){
var scrollPosition = -(_this.$box.scrollTop() / 5);
_this.$parallaxs.each(function(){
var $this=$(this);
var imagePosition = 'center '+ scrollPosition + 'px';
$this.css({'background-position':imagePosition});
})
})
}
//如果是效果2
if(this.opts.effect==2){
this.dh=this.$window.height();
this.$sections.each(function(index){
var $this=$(this);
if(index!=0){
// $this.css('webkitTransform','translateY(-'+_this.dh+'px)');
$this.css('transform','translateY('+_this.dh+'px)');
}
setTimeout(function(){
$this.css({
'transition':'all 1s ease',
'display':'block'
});
},0)
})
}
//监控鼠标滚动或者向上向下按键
this.$window.on('keydown DOMMouseScroll mousewheel',function(event){
return _this.judge(event);
})
//监听hash的变化
this.$window.on('hashchange',function(){
_this.hashchange();
})
//初始化滚动
this.hashchange();
return this;
}
npScroll.prototype.judge=function(event){
// event.preventDefault();
// 现在时间
var now = new Date().getTime();
if(event.keyCode == 116){
window.location.reload();
}
if(now-this.time>100){
this.time=now;
// Down event
if (event.originalEvent.detail > 0 || event.originalEvent.wheelDelta < 0 || event.keyCode == 40) {
// Next section element
this.nextPage();
} else if (event.originalEvent.detail < 0 || event.originalEvent.wheelDelta > 0 || event.keyCode == 38) {
// Previous section element
this.prevPage();
}
event.stopPropagation();
}else{
this.time = now;
return 0;
}
}
npScroll.prototype.hashchange=function(){
if(location.hash && location.hash.indexOf('#page')!=-1){
var index=parseInt(location.hash.slice(5));
if(index!==undefined && index!==this.pageIndex && index<=this.pagesMax){
this.scrollTo(index);
}else{
this.scrollTo(this.pageIndex);
}
}else{
this.scrollTo(this.pageIndex);
}
}
npScroll.prototype.itemClick=function(obj){
var $obj=$(obj);
var index=$obj.data('section');
this.scrollTo(index);
return this;
}
npScroll.prototype.nextPage=function(){
if(this.pageIndex < this.pagesMax-1){
var index=this.pageIndex+1;
this.scrollTo(index);
}
return this;
}
npScroll.prototype.prevPage=function(){
if(this.pageIndex > 0){
var index=this.pageIndex-1;
this.scrollTo(index);
}
return this;
}
npScroll.prototype.scrollTo=function(index){
var _this=this;
//滚动钩子
setTimeout(function(){
_this.trigger('scrollStart',index);
},0)
//老的pageIndex
var old_pageIndex=this.pageIndex;
this.pageIndex=index;
//更新导航
this.$navigationItems.filter(':not('+this.pageIndex+')').removeClass('navigation-item-active');
this.$navigationItems.filter(':eq('+this.pageIndex+')').addClass('navigation-item-active');
//更新section
var $section=this.$sections.filter(':eq('+this.pageIndex+')');
this.$sections.filter(':not('+this.pageIndex+')').removeClass('np-section-active');
$section.addClass('np-section-active');
var top=this.section_top_arr[index];
if(this.opts.effect==1){
this.$scrollTarget.stop().animate({scrollTop: top},1000);
}else if(this.opts.effect==2){
//如果效果2
var $old_section=this.$sections.filter(':eq('+old_pageIndex+')');
if(old_pageIndex<this.pageIndex){
$section.css({
'transform':'translateY(0)'
});
}else if(old_pageIndex>this.pageIndex){
$old_section.css({
'transform':'translateY('+_this.dh+'px)'
});
}
}
//滚动结束钩子
setTimeout(function(){
_this.trigger('scrollEnd',index);
},1000)
return this;
}
npScroll.prototype.on=function(type,cb){
if(!this._events[type]){
this._events[type]=[];
}
this._events[type].push($.proxy(cb,this));
return this;
}
npScroll.prototype.trigger=function(){
var args=[].slice.apply(arguments);
var type=args[0];
var params=args.slice(1);
var events=this._events[type];
if(events && events.length>0){
for(var i=0,n=events.length;i<n;i++){
events[i](params);
}
}
return this;
}
npScroll.prototype.off=function(type,cb){
if(!type){
return this._events={};
}
var events=this._events[type];
if(events && events.length>0){
for(var i=0,n=events.length;i<n;i++){
if(events[i]===cb){
events.splice(i,-1);
break;
}
}
}
this._events[type]=events;
return this;
}
window.npScroll=npScroll;
return npScroll;
}));