-
Notifications
You must be signed in to change notification settings - Fork 98
/
jquery.malihu.PageScroll2id.js
801 lines (710 loc) · 27 KB
/
jquery.malihu.PageScroll2id.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
/*
== Page scroll to id ==
Version: 1.6.8
Plugin URI: http://manos.malihu.gr/page-scroll-to-id/
Author: malihu
Author URI: http://manos.malihu.gr
License: MIT License (MIT)
*/
/*
Copyright 2013 malihu (email: [email protected])
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
;(function($,window,document,undefined){
/* plugin namespace, prefix, default selector(s) */
var pluginNS="mPageScroll2id",
pluginPfx="mPS2id",
defaultSelector=".m_PageScroll2id,a[rel~='m_PageScroll2id'],.page-scroll-to-id,a[rel~='page-scroll-to-id'],._ps2id",
/* default options */
defaults={
/* scroll animation speed in milliseconds: Integer */
scrollSpeed:1000,
/* auto-adjust animation speed (according to target element position and window scroll): Boolean */
autoScrollSpeed:true,
/* scroll animation easing when page is idle: String */
scrollEasing:"easeInOutQuint",
/* scroll animation easing while page is scrolling: String */
scrollingEasing:"easeOutQuint",
/* end of page "smooth scrolling" (auto-adjust the scroll-to position when bottom elements are too short): Boolean */
pageEndSmoothScroll:true,
/*
page layout defines scrolling direction: String
values: "vertical", "horizontal", "auto"
*/
layout:"vertical",
/* extra space in pixels for the target element position: Integer */
offset:0,
/* highlight the main/default selectors or insert a different set: Boolean, String */
highlightSelector:false,
/* class of the clicked element: String */
clickedClass:pluginPfx+"-clicked",
/* class of the current target element: String */
targetClass:pluginPfx+"-target",
/* class of the highlighted element: String */
highlightClass:pluginPfx+"-highlight",
/* force a single highlighted element each time: Boolean */
forceSingleHighlight:false,
/* keep element highlighted until next (one element always stays highlighted): boolean */
keepHighlightUntilNext:false,
/* highlight elements according to their target and next target position (useful when targets have zero dimensions). Non "auto" layouts only: boolean */
highlightByNextTarget:false,
/* disable plugin below [x,y] screen size: boolean, integer, array ([x,y]) */
disablePluginBelow:false,
/* enable/disable click events for all selectors */
clickEvents:true,
/* append hash to URL/address bar */
appendHash:false,
/* user callback functions: fn */
onStart:function(){},
onComplete:function(){},
/* enable/disable the default selector: Boolean */
defaultSelector:false,
/* highlight elements now and in the future */
live:true,
/* set specific live selector(s): String */
liveSelector:false,
/* set specific selector(s) that will be excluded from being handled by the plugin: String */
excludeSelectors:false,
/* enable encodeURI for links (enable if your links have href values with UTF-8 encoding): boolean */
encodeLinks:false,
/* enable to run the script inside iframe */
inIframe:false
},
/* vars, constants */
selector,opt,_init,_trigger,_clicked,_target,_to,_axis,_offset,_dataOffset,_totalInstances=0,_liveTimer,_speed,
specialChars=/[ `!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?~]/,
/*
---------------
methods
---------------
*/
methods={
/* plugin initialization method */
init:function(options){
/* extend options, store each option in jquery data */
var options=$.extend(true,{},defaults,options);
$(document).data(pluginPfx,options);
opt=$(document).data(pluginPfx);
/* check/set jquery (deprecated) selector property if not defined */
if(!this.selector){
var selectorClass="__"+pluginPfx;
this.each(function(){
var el=$(this);
if(!el.hasClass(selectorClass)){
el.addClass(selectorClass);
}
});
this.selector="."+selectorClass;
}
/* live selector */
if(opt.liveSelector) this.selector+=","+opt.liveSelector;
/* set selector */
selector=(!selector) ? this.selector : selector+","+this.selector;
if(opt.defaultSelector){
if(typeof $(selector)!=="object" || $(selector).length===0){
selector=defaultSelector;
}
}
/* plugin events */
if(opt.clickEvents){
$(document)
.off("."+pluginPfx)
.on("click."+pluginPfx,selector,function(e){
if(functions._isDisabled.call(null)){
functions._removeClasses.call(null);
return;
}
var $this=$(this),
href=$this.attr("href"),
hrefProp=$this.prop("href").baseVal || $this.prop("href");
if(opt.excludeSelectors && $this.is(opt.excludeSelectors)){ //excluded selectors
return;
}
if(href && href.indexOf("#/")!==-1){
return;
}
functions._reset.call(null);
_dataOffset=$this.data("ps2id-offset") || 0;
if(functions._isValid.call(null,href,hrefProp) && functions._findTarget.call(null,href)){
e.preventDefault();
_trigger="selector";
_clicked=$this;
functions._setClasses.call(null,true);
functions._scrollTo.call(null);
}
});
}
$(window)
.off("."+pluginPfx)
.on("scroll."+pluginPfx+" resize."+pluginPfx,function(){
if(functions._isDisabled.call(null)){
functions._removeClasses.call(null);
return;
}
var targets=$("._"+pluginPfx+"-t");
targets.each(function(i){
var t=$(this),id=t.attr("id"),
h=functions._findHighlight.call(null,id);
functions._setClasses.call(null,false,t,h);
if(i==targets.length-1){functions._extendClasses.call(null);}
});
});
/* plugin has initialized */
_init=true;
/* setup selectors, target elements, basic plugin classes etc. */
functions._setup.call(null);
/*
monitor for elements matching the current highlight selector and call plugin setup when found (now and in the future)
to manually enable/disable: $(document).data("mPS2id").live=boolean
*/
functions._live.call(null);
},
/* scrollTo method */
scrollTo:function(id,options){
if(functions._isDisabled.call(null)){
functions._removeClasses.call(null);
return;
}
if(id && typeof id!=="undefined"){
functions._isInit.call(null);
var defaults={
layout:opt.layout,
offset:opt.offset,
clicked:false
},
options=$.extend(true,{},defaults,options);
functions._reset.call(null);
_axis=options.layout;
_offset=options.offset;
id=(id.indexOf("#")!==-1) ? id : "#"+id;
if(functions._isValid.call(null,id) && functions._findTarget.call(null,id)){
_trigger="scrollTo";
_clicked=options.clicked;
if(_clicked){
functions._setClasses.call(null,true);
}
functions._scrollTo.call(null);
}
}
},
/* destroy method */
destroy:function(){
$(window).off("."+pluginPfx);
$(document).off("."+pluginPfx).removeData(pluginPfx);
$("._"+pluginPfx+"-t").removeData(pluginPfx);
functions._removeClasses.call(null,true);
}
},
/*
---------------
functions
---------------
*/
functions={
/* checks if screen size ([x,y]) is below the value(s) set in disablePluginBelow option */
_isDisabled:function(){
var e=window,a="inner",
val=opt.disablePluginBelow instanceof Array ? [opt.disablePluginBelow[0] || 0,opt.disablePluginBelow[1] || 0] : [opt.disablePluginBelow || 0,0];
if(!("innerWidth" in window )){
a="client";
e=document.documentElement || document.body;
}
return e[a+"Width"]<=val[0] || e[a+"Height"]<=val[1];
},
/* checks if href attribute is valid */
_isValid:function(href,hrefProp){
if(!href){
return;
}
hrefProp=(!hrefProp) ? href : hrefProp;
var str=(hrefProp.indexOf("#/")!==-1) ? hrefProp.split("#/")[0] : hrefProp.split("#")[0],
wloc=!opt.inIframe && window.location !== window.parent.location ? window.parent.location : window.location,
loc=wloc.toString().split("#")[0];
return href!=="#" && href.indexOf("#")!==-1 && (str==="" || decodeURIComponent(str)===decodeURIComponent(loc));
},
/* setup selectors, target elements, basic plugin classes etc. */
_setup:function(){
var el=functions._highlightSelector(),i=1,tp=0;
return $(el).each(function(){
var $this=$(this),href=$this.attr("href"),hrefProp=$this.prop("href").baseVal || $this.prop("href");
if(functions._isValid.call(null,href,hrefProp)){
if(opt.excludeSelectors && $this.is(opt.excludeSelectors)){ //excluded selectors
return;
}
var id=(href.indexOf("#/")!==-1) ? href.split("#/")[1] : href.substring(href.indexOf('#')+1), //we're not using str.split("#")[1] because we want only the first occurrence of # in case the id has # in its actual name
t=specialChars.test(id) ? $(document.getElementById(id)) : $("#"+id); //fix bug with special characters like %, ?, & etc. in selector
if(t.length>0){
if(opt.highlightByNextTarget){
if(t!==tp){
if(!tp){t.data(pluginPfx,{tn:"0"});}else{tp.data(pluginPfx,{tn:t});}
tp=t;
}
}
if(!t.hasClass("_"+pluginPfx+"-t")){
t.addClass("_"+pluginPfx+"-t");
}
t.data(pluginPfx,{i:i});
if(!$this.hasClass("_"+pluginPfx+"-h")){
$this.addClass("_"+pluginPfx+"-h");
}
var h=functions._findHighlight.call(null,id);
functions._setClasses.call(null,false,t,h);
_totalInstances=i;
i++
if(i==$(el).length){functions._extendClasses.call(null);}
}
}
});
},
/* returns the highlight selector */
_highlightSelector:function(){
return (opt.highlightSelector && opt.highlightSelector!=="") ? opt.highlightSelector : selector;
},
/* finds the target element */
_findTarget:function(str){
var val=(str.indexOf("#/")!==-1) ? str.split("#/")[1] : str.substring(str.indexOf('#')+1), //we're not using str.split("#")[1] because we want only the first occurrence of # in case the id has # in its actual name
el=specialChars.test(val) ? $(document.getElementById(val)) : $("#"+val); //fix bug with special characters like %, ?, & etc. in selector
if(el.length<1 || el.css("position")==="fixed"){
if(val==="top"){
el=$("body");
}else{
return;
}
}
_target=el;
if(!_axis){
_axis=opt.layout;
}
_offset=functions._setOffset.call(null);
_to=[(el.offset().top-_offset[0]).toString(),(el.offset().left-_offset[1]).toString()];
_to[0]=(_to[0]<0) ? 0 : _to[0];
_to[1]=(_to[1]<0) ? 0 : _to[1];
return _to;
},
/* sets the offset value (pixels, objects etc.) */
_setOffset:function(){
if(!_offset){
_offset=(opt.offset) ? opt.offset : 0;
}
if(_dataOffset){
_offset=_dataOffset;
}
var val,obj,y,x;
switch(typeof _offset){
case "object":
case "string":
val=[(_offset["y"]) ? _offset["y"] : _offset,(_offset["x"]) ? _offset["x"] : _offset];
obj=[(val[0] instanceof jQuery) ? val[0] : $(val[0]),(val[1] instanceof jQuery) ? val[1] : $(val[1])];
if(obj[0].length>0){ // js/jquery object
y=obj[0].height();
if(obj[0].css("position")==="fixed"){ // include position for fixed elements
y+=obj[0][0].offsetTop;
}
}else if(!isNaN(parseFloat(val[0])) && isFinite(val[0])){ // numeric string
y=parseInt(val[0]);
}else{
y=0; // non-existing value
}
if(obj[1].length>0){ // js/jquery object
x=obj[1].width();
if(obj[1].css("position")==="fixed"){ // include position for fixed elements
x+=obj[1][0].offsetLeft;
}
}else if(!isNaN(parseFloat(val[1])) && isFinite(val[1])){ // numeric string
x=parseInt(val[1]);
}else{
x=0; // non-existing value
}
break;
case "function":
val=_offset.call(null); // function (single value or array)
if(val instanceof Array){
y=val[0];
x=val[1];
}else{
y=x=val;
}
break;
default:
y=x=parseInt(_offset); // number
}
return [y,x];
},
/* finds the element that should be highlighted */
_findHighlight:function(id){
var wLoc=!opt.inIframe && window.location !== window.parent.location ? window.parent.location : window.location,
loc=wLoc.toString().split("#")[0],
locPath=wLoc.pathname;
if(loc.indexOf("'")!==-1) loc=loc.replace("'","\\'");
if(locPath.indexOf("'")!==-1) locPath=locPath.replace("'","\\'");
loc=decodeURIComponent(loc);
locPath=decodeURIComponent(locPath);
if(opt.encodeLinks){
var locEnc=encodeURI(loc).toLowerCase(),locPathEnc=encodeURI(locPath).toLowerCase();
return $("._"+pluginPfx+"-h[href='#"+id+"'],._"+pluginPfx+"-h[href='"+loc+"#"+id+"'],._"+pluginPfx+"-h[href='"+locPath+"#"+id+"'],._"+pluginPfx+"-h[href='#/"+id+"'],._"+pluginPfx+"-h[href='"+loc+"#/"+id+"'],._"+pluginPfx+"-h[href='"+locPath+"#/"+id+"'],._"+pluginPfx+"-h[href='"+locEnc+"#/"+id+"'],._"+pluginPfx+"-h[href='"+locEnc+"#"+id+"'],._"+pluginPfx+"-h[href='"+locPathEnc+"#/"+id+"'],._"+pluginPfx+"-h[href='"+locPathEnc+"#"+id+"']");
}else{
return $("._"+pluginPfx+"-h[href='#"+id+"'],._"+pluginPfx+"-h[href='"+loc+"#"+id+"'],._"+pluginPfx+"-h[href='"+locPath+"#"+id+"'],._"+pluginPfx+"-h[href='#/"+id+"'],._"+pluginPfx+"-h[href='"+loc+"#/"+id+"'],._"+pluginPfx+"-h[href='"+locPath+"#/"+id+"']");
}
},
/* sets plugin classes */
_setClasses:function(c,t,h){
var cc=opt.clickedClass,tc=opt.targetClass,hc=opt.highlightClass;
if(c && cc && cc!==""){
$("."+cc).removeClass(cc);
_clicked.addClass(cc);
}else if(t && tc && tc!=="" && h && hc && hc!==""){
if(functions._currentTarget.call(null,t)){
t.addClass(tc);
h.addClass(hc);
}else{
if(!opt.keepHighlightUntilNext || $("."+hc).length>1){
t.removeClass(tc);
h.removeClass(hc);
}
}
}
},
/* extends plugin classes */
_extendClasses:function(){
var tc=opt.targetClass,hc=opt.highlightClass,
$tc=$("."+tc),$hc=$("."+hc),ftc=tc+"-first",ltc=tc+"-last",fhc=hc+"-first",lhc=hc+"-last";
$("._"+pluginPfx+"-t").removeClass(ftc+" "+ltc);
$("._"+pluginPfx+"-h").removeClass(fhc+" "+lhc);
if(!opt.forceSingleHighlight){
$tc.slice(0,1).addClass(ftc).end().slice(-1).addClass(ltc);
$hc.slice(0,1).addClass(fhc).end().slice(-1).addClass(lhc);
}else{
if(opt.keepHighlightUntilNext && $tc.length>1){
$tc.slice(0,1).removeClass(tc); $hc.slice(0,1).removeClass(hc);
}else{
$tc.slice(1).removeClass(tc); $hc.slice(1).removeClass(hc);
}
}
},
/* removes plugin classes */
_removeClasses:function(destroy){
$("."+opt.clickedClass).removeClass(opt.clickedClass);
$("."+opt.targetClass).removeClass(opt.targetClass+" "+opt.targetClass+"-first "+opt.targetClass+"-last");
$("."+opt.highlightClass).removeClass(opt.highlightClass+" "+opt.highlightClass+"-first "+opt.highlightClass+"-last");
if(destroy){
$("._"+pluginPfx+"-t").removeClass("_"+pluginPfx+"-t");
$("._"+pluginPfx+"-h").removeClass("_"+pluginPfx+"-h");
}
},
/* checks if target element is in viewport */
_currentTarget:function(t){
if(!t.data(pluginPfx)) return; //handle Uncaught TypeError (undefined data)
var o=opt["target_"+t.data(pluginPfx).i],
dataTarget=t.data("ps2id-target"),
rect=dataTarget && $(dataTarget)[0] ? $(dataTarget)[0].getBoundingClientRect() : t[0].getBoundingClientRect();
if(typeof o!=="undefined"){
var y=t.offset().top,x=t.offset().left,
from=(o.from) ? o.from+y : y,to=(o.to) ? o.to+y : y,
fromX=(o.fromX) ? o.fromX+x : x,toX=(o.toX) ? o.toX+x : x;
return(
rect.top >= to && rect.top <= from &&
rect.left >= toX && rect.left <= fromX
);
}else{
var wh=$(window).height(),ww=$(window).width(),
th=dataTarget ? $(dataTarget).height() : t.height(),tw=dataTarget ? $(dataTarget).width() : t.width(),
base=1+(th/wh),
top=base,bottom=(th<wh) ? base*(wh/th) : base,
baseX=1+(tw/ww),
left=baseX,right=(tw<ww) ? baseX*(ww/tw) : baseX,
val=[rect.top <= wh/top,rect.bottom >= wh/bottom,rect.left <= ww/left,rect.right >= ww/right];
if(opt.highlightByNextTarget){
var tn=t.data(pluginPfx).tn;
if(tn){
var rectn=tn[0].getBoundingClientRect();
if(opt.layout==="vertical"){
val=[rect.top <= wh/2,rectn.top > wh/2,1,1];
}else if(opt.layout==="horizontal"){
val=[1,1,rect.left <= ww/2,rectn.left > ww/2];
}
}
}
return(val[0] && val[1] && val[2] && val[3]);
}
},
/* scrolls the page */
_scrollTo:function(){
_speed=functions._scrollSpeed.call(null);
_to=(opt.pageEndSmoothScroll) ? functions._pageEndSmoothScroll.call(null) : _to;
var _scrollable=$("html,body"),
speed=(opt.autoScrollSpeed) ? functions._autoScrollSpeed.call(null) : _speed,
easing=(_scrollable.is(":animated")) ? opt.scrollingEasing : opt.scrollEasing,
_t=$(window).scrollTop(),_l=$(window).scrollLeft();
switch(_axis){
case "horizontal":
if(_l!=_to[1]){
functions._callbacks.call(null,"onStart");
_scrollable.stop().animate({scrollLeft:_to[1]},speed,easing).promise().then(function(){
functions._callbacks.call(null,"onComplete");
});
}
break;
case "auto":
if(_t!=_to[0] || _l!=_to[1]){
functions._callbacks.call(null,"onStart");
if(navigator.userAgent.match(/(iPod|iPhone|iPad|Android)/)){ // mobile fix
var left;
_scrollable.stop().animate({pageYOffset:_to[0],pageXOffset:_to[1]},{
duration:speed,
easing:easing,
step:function(now,fx){
if(fx.prop=='pageXOffset'){
left=now;
}else if(fx.prop=='pageYOffset'){
window.scrollTo(left,now);
}
}
}).promise().then(function(){
functions._callbacks.call(null,"onComplete");
});
}else{
_scrollable.stop().animate({scrollTop:_to[0],scrollLeft:_to[1]},speed,easing).promise().then(function(){
functions._callbacks.call(null,"onComplete");
});
}
}
break;
default:
if(_t!=_to[0]){
functions._callbacks.call(null,"onStart");
_scrollable.stop().animate({scrollTop:_to[0]},speed,easing).promise().then(function(){
functions._callbacks.call(null,"onComplete");
});
}
}
},
/* sets end of page "smooth scrolling" position */
_pageEndSmoothScroll:function(){
var _dh=$(document).height(),_dw=$(document).width(),
_wh=$(window).height(),_ww=$(window).width();
return [((_dh-_to[0])<_wh) ? _dh-_wh : _to[0],((_dw-_to[1])<_ww) ? _dw-_ww : _to[1]];
},
/* sets animation speed (link-specific speed via ps2id-speed-VALUE class on link or link's parent) */
_scrollSpeed:function(){
var speed=opt.scrollSpeed;
if(_clicked && _clicked.length){
_clicked.add(_clicked.parent()).each(function(){
var $this=$(this);
if($this.attr("class")){
var clickedClasses=$this.attr("class").split(" ");
for(var index in clickedClasses){
if(String(clickedClasses[index]).match(/^ps2id-speed-\d+$/)){
speed=clickedClasses[index].split("ps2id-speed-")[1];
break;
}
}
}
});
}
return parseInt(speed);
},
/* sets the auto-adjusted animation speed */
_autoScrollSpeed:function(){
var _t=$(window).scrollTop(),_l=$(window).scrollLeft(),
_h=$(document).height(),_w=$(document).width(),
val=[
_speed+((_speed*(Math.floor((Math.abs(_to[0]-_t)/_h)*100)))/100),
_speed+((_speed*(Math.floor((Math.abs(_to[1]-_l)/_w)*100)))/100)
];
return Math.max.apply(Math,val);
},
/* user callback functions */
_callbacks:function(c){
if(!opt){
return;
}
this[pluginPfx]={
trigger:_trigger,clicked:_clicked,target:_target,scrollTo:{y:_to[0],x:_to[1]}
};
switch(c){
case "onStart":
//append hash to URL/address bar
if(opt.appendHash && window.history && window.history.pushState && _clicked && _clicked.length){
var hval=_clicked.attr("href"),h="#"+(hval.substring(hval.indexOf('#')+1)); //we're not using hval.split("#")[1] because we want only the first occurrence of # in case the id has # in its actual name
if(h!==window.location.hash) history.pushState("","",h);
}
opt.onStart.call(null,this[pluginPfx]);
break;
case "onComplete":
opt.onComplete.call(null,this[pluginPfx]);
break;
}
},
/* resets/clears vars and constants */
_reset:function(){
_axis=_offset=_dataOffset=false;
},
/* checks if plugin has initialized */
_isInit:function(){
if(!_init){
methods.init.apply(this);
}
},
/* live fn */
_live:function(){
_liveTimer=setTimeout(function(){
if(opt.live){
if($(functions._highlightSelector()).length!==_totalInstances){
functions._setup.call(null);
}
}else{
if(_liveTimer){clearTimeout(_liveTimer);}
}
functions._live.call(null);
},1000);
},
/* extends jquery with custom easings (as jquery ui) */
_easing:function(){
$.easing.easeInQuad=$.easing.easeInQuad || function(x){
return x*x;
};
$.easing.easeOutQuad=$.easing.easeOutQuad || function(x){
return 1-(1-x)*(1-x);
};
$.easing.easeInOutQuad=$.easing.easeInOutQuad || function(x){
return x<0.5 ? 2*x*x : 1-Math.pow(-2*x+2,2)/2;
};
$.easing.easeInCubic=$.easing.easeInCubic || function(x){
return x*x*x;
};
$.easing.easeOutCubic=$.easing.easeOutCubic || function(x){
return 1-Math.pow(1-x,3);
};
$.easing.easeInOutCubic=$.easing.easeInOutCubic || function(x){
return x<0.5 ? 4*x*x*x : 1-Math.pow(-2*x+2,3)/2;
};
$.easing.easeInQuart=$.easing.easeInQuart || function(x){
return x*x*x*x;
};
$.easing.easeOutQuart=$.easing.easeOutQuart || function(x){
return 1-Math.pow(1-x,4);
};
$.easing.easeInOutQuart=$.easing.easeInOutQuart || function(x){
return x<0.5 ? 8*x*x*x*x : 1-Math.pow(-2*x+2,4)/2;
};
$.easing.easeInQuint=$.easing.easeInQuint || function(x){
return x*x*x*x*x;
};
$.easing.easeOutQuint=$.easing.easeOutQuint || function(x){
return 1-Math.pow(1-x,5);
};
$.easing.easeInOutQuint=$.easing.easeInOutQuint || function(x){
return x<0.5 ? 16*x*x*x*x*x : 1-Math.pow(-2*x+2,5)/2;
};
$.easing.easeInExpo=$.easing.easeInExpo || function(x){
return x===0 ? 0 : Math.pow(2,10*x-10);
};
$.easing.easeOutExpo=$.easing.easeOutExpo || function(x){
return x===1 ? 1 : 1-Math.pow(2,-10*x);
};
$.easing.easeInOutExpo=$.easing.easeInOutExpo || function(x){
return x===0 ? 0 : x===1 ? 1 : x<0.5 ? Math.pow(2,20*x-10)/2 : (2-Math.pow(2,-20*x+10))/2;
};
$.easing.easeInSine=$.easing.easeInSine || function(x){
return 1-Math.cos(x*Math.PI/2);
};
$.easing.easeOutSine=$.easing.easeOutSine || function(x){
return Math.sin(x*Math.PI/2);
};
$.easing.easeInOutSine=$.easing.easeInOutSine || function(x){
return -(Math.cos(Math.PI*x)-1)/2;
};
$.easing.easeInCirc=$.easing.easeInCirc || function(x){
return 1-Math.sqrt(1-Math.pow(x,2));
};
$.easing.easeOutCirc=$.easing.easeOutCirc || function(x){
return Math.sqrt(1-Math.pow(x-1,2));
};
$.easing.easeInOutCirc=$.easing.easeInOutCirc || function(x){
return x<0.5 ? (1-Math.sqrt(1-Math.pow(2*x,2)))/2 : (Math.sqrt(1-Math.pow(-2*x+2,2))+1)/2;
};
$.easing.easeInElastic=$.easing.easeInElastic || function(x){
return x===0 ? 0 : x===1 ? 1 : -Math.pow(2,10*x-10)*Math.sin((x*10-10.75)*((2*Math.PI)/3));
};
$.easing.easeOutElastic=$.easing.easeOutElastic || function(x){
return x===0 ? 0 : x===1 ? 1 : Math.pow(2,-10*x)*Math.sin((x*10-0.75)*((2*Math.PI)/3))+1;
};
$.easing.easeInOutElastic=$.easing.easeInOutElastic || function(x){
return x===0 ? 0 : x===1 ? 1 : x<0.5 ? -(Math.pow(2,20*x-10)*Math.sin((20*x-11.125)*((2*Math.PI)/4.5)))/2 : Math.pow(2,-20*x+10)*Math.sin((20*x-11.125)*((2*Math.PI)/4.5))/2+1;
};
$.easing.easeInBack=$.easing.easeInBack || function(x){
return (1.70158+1)*x*x*x-1.70158*x*x;
};
$.easing.easeOutBack=$.easing.easeOutBack || function(x){
return 1+(1.70158+1)*Math.pow(x-1,3)+1.70158*Math.pow(x-1,2);
};
$.easing.easeInOutBack=$.easing.easeInOutBack || function(x){
return x<0.5 ? (Math.pow(2*x,2)*(((1.70158*1.525)+1)*2*x-(1.70158*1.525)))/2 : (Math.pow(2*x-2,2)*(((1.70158*1.525)+1)*(x*2-2)+(1.70158*1.525))+2)/2;
};
$.easing.easeInBounce=$.easing.easeInBounce || function(x){
return 1-__bounceOut(1-x);
};
$.easing.easeOutBounce=$.easing.easeOutBounce || __bounceOut;
$.easing.easeInOutBounce=$.easing.easeInOutBounce || function(x){
return x<0.5 ? (1-__bounceOut(1-2*x))/2 : (1+__bounceOut(2*x-1))/2;
};
function __bounceOut(x){
var n1=7.5625,d1=2.75;
if(x<1/d1){
return n1*x*x;
}else if(x<2/d1){
return n1*(x-=(1.5/d1))*x+.75;
}else if(x<2.5/d1){
return n1*(x-=(2.25/d1))*x+.9375;
}else{
return n1*(x-=(2.625/d1))*x+.984375;
}
}
}
}
/*
---------------
plugin setup
---------------
*/
/* extend jquery with custom easings */
functions._easing.call();
/* plugin constructor functions */
$.fn[pluginNS]=function(method){
if(methods[method]){
return methods[method].apply(this,Array.prototype.slice.call(arguments,1));
}else if(typeof method==="object" || !method){
return methods.init.apply(this,arguments);
}else{
$.error("Method "+method+" does not exist");
}
};
$[pluginNS]=function(method){
if(methods[method]){
return methods[method].apply(this,Array.prototype.slice.call(arguments,1));
}else if(typeof method==="object" || !method){
return methods.init.apply(this,arguments);
}else{
$.error("Method "+method+" does not exist");
}
};
/*
allow setting plugin default options.
example: $.plugin_name.defaults.option_name="option_value";
*/
$[pluginNS].defaults=defaults;
})(jQuery,window,document);