jquery.malihu.PageScroll2id.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  1. /*
  2. == Page scroll to id ==
  3. Version: 1.5.8
  4. Plugin URI: http://manos.malihu.gr/page-scroll-to-id/
  5. Author: malihu
  6. Author URI: http://manos.malihu.gr
  7. License: MIT License (MIT)
  8. */
  9. /*
  10. Copyright 2013 malihu (email: manos@malihu.gr)
  11. Permission is hereby granted, free of charge, to any person obtaining a copy
  12. of this software and associated documentation files (the "Software"), to deal
  13. in the Software without restriction, including without limitation the rights
  14. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. copies of the Software, and to permit persons to whom the Software is
  16. furnished to do so, subject to the following conditions:
  17. The above copyright notice and this permission notice shall be included in
  18. all copies or substantial portions of the Software.
  19. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. THE SOFTWARE.
  26. */
  27. ;(function($,window,document,undefined){
  28. /* plugin namespace, prefix, default selector(s) */
  29. var pluginNS="mPageScroll2id",
  30. pluginPfx="mPS2id",
  31. defaultSelector=".m_PageScroll2id,a[rel~='m_PageScroll2id'],.page-scroll-to-id,a[rel~='page-scroll-to-id'],._ps2id",
  32. /* default options */
  33. defaults={
  34. /* scroll animation speed in milliseconds: Integer */
  35. scrollSpeed:1000,
  36. /* auto-adjust animation speed (according to target element position and window scroll): Boolean */
  37. autoScrollSpeed:true,
  38. /* scroll animation easing when page is idle: String */
  39. scrollEasing:"easeInOutQuint",
  40. /* scroll animation easing while page is scrolling: String */
  41. scrollingEasing:"easeOutQuint",
  42. /* end of page "smooth scrolling" (auto-adjust the scroll-to position when bottom elements are too short): Boolean */
  43. pageEndSmoothScroll:true,
  44. /*
  45. page layout defines scrolling direction: String
  46. values: "vertical", "horizontal", "auto"
  47. */
  48. layout:"vertical",
  49. /* extra space in pixels for the target element position: Integer */
  50. offset:0,
  51. /* highlight the main/default selectors or insert a different set: Boolean, String */
  52. highlightSelector:false,
  53. /* class of the clicked element: String */
  54. clickedClass:pluginPfx+"-clicked",
  55. /* class of the current target element: String */
  56. targetClass:pluginPfx+"-target",
  57. /* class of the highlighted element: String */
  58. highlightClass:pluginPfx+"-highlight",
  59. /* force a single highlighted element each time: Boolean */
  60. forceSingleHighlight:false,
  61. /* keep element highlighted until next (one element always stays highlighted): boolean */
  62. keepHighlightUntilNext:false,
  63. /* highlight elements according to their target and next target position (useful when targets have zero dimensions). Non "auto" layouts only: boolean */
  64. highlightByNextTarget:false,
  65. /* disable plugin below [x,y] screen size: boolean, integer, array ([x,y]) */
  66. disablePluginBelow:false,
  67. /* enable/disable click events for all selectors */
  68. clickEvents:true,
  69. /* append hash to URL/address bar */
  70. appendHash:false,
  71. /* user callback functions: fn */
  72. onStart:function(){},
  73. onComplete:function(){},
  74. /* enable/disable the default selector: Boolean */
  75. defaultSelector:false,
  76. /* highlight elements now and in the future */
  77. live:true,
  78. /* set specific live selector(s): String */
  79. liveSelector:false
  80. },
  81. /* vars, constants */
  82. selector,opt,_init,_trigger,_clicked,_target,_to,_axis,_offset,_dataOffset,_totalInstances=0,_liveTimer,_speed,
  83. /*
  84. ---------------
  85. methods
  86. ---------------
  87. */
  88. methods={
  89. /* plugin initialization method */
  90. init:function(options){
  91. /* extend options, store each option in jquery data */
  92. var options=$.extend(true,{},defaults,options);
  93. $(document).data(pluginPfx,options);
  94. opt=$(document).data(pluginPfx);
  95. /* check/set jquery (deprecated) selector property if not defined */
  96. if(!this.selector){
  97. var selectorClass="__"+pluginPfx;
  98. this.each(function(){
  99. var el=$(this);
  100. if(!el.hasClass(selectorClass)){
  101. el.addClass(selectorClass);
  102. }
  103. });
  104. this.selector="."+selectorClass;
  105. }
  106. /* live selector */
  107. if(opt.liveSelector) this.selector+=","+opt.liveSelector;
  108. /* set selector */
  109. selector=(!selector) ? this.selector : selector+","+this.selector;
  110. if(opt.defaultSelector){
  111. if(typeof $(selector)!=="object" || $(selector).length===0){
  112. selector=defaultSelector;
  113. }
  114. }
  115. /* plugin events */
  116. if(opt.clickEvents){
  117. $(document)
  118. .undelegate("."+pluginPfx)
  119. .delegate(selector,"click."+pluginPfx,function(e){
  120. if(functions._isDisabled.call(null)){
  121. functions._removeClasses.call(null);
  122. return;
  123. }
  124. var $this=$(this),
  125. href=$this.attr("href"),
  126. hrefProp=$this.prop("href");
  127. if(href && href.indexOf("#/")!==-1){
  128. return;
  129. }
  130. functions._reset.call(null);
  131. _dataOffset=$this.data("ps2id-offset") || 0;
  132. if(functions._isValid.call(null,href,hrefProp) && functions._findTarget.call(null,href)){
  133. e.preventDefault();
  134. _trigger="selector";
  135. _clicked=$this;
  136. functions._setClasses.call(null,true);
  137. functions._scrollTo.call(null);
  138. }
  139. });
  140. }
  141. $(window)
  142. .unbind("."+pluginPfx)
  143. .bind("scroll."+pluginPfx+" resize."+pluginPfx,function(){
  144. if(functions._isDisabled.call(null)){
  145. functions._removeClasses.call(null);
  146. return;
  147. }
  148. var targets=$("._"+pluginPfx+"-t");
  149. targets.each(function(i){
  150. var t=$(this),id=t.attr("id"),
  151. h=functions._findHighlight.call(null,id);
  152. functions._setClasses.call(null,false,t,h);
  153. if(i==targets.length-1){functions._extendClasses.call(null);}
  154. });
  155. });
  156. /* plugin has initialized */
  157. _init=true;
  158. /* setup selectors, target elements, basic plugin classes etc. */
  159. functions._setup.call(null);
  160. /*
  161. monitor for elements matching the current highlight selector and call plugin setup when found (now and in the future)
  162. to manually enable/disable: $(document).data("mPS2id").live=boolean
  163. */
  164. functions._live.call(null);
  165. },
  166. /* scrollTo method */
  167. scrollTo:function(id,options){
  168. if(functions._isDisabled.call(null)){
  169. functions._removeClasses.call(null);
  170. return;
  171. }
  172. if(id && typeof id!=="undefined"){
  173. functions._isInit.call(null);
  174. var defaults={
  175. layout:opt.layout,
  176. offset:opt.offset,
  177. clicked:false
  178. },
  179. options=$.extend(true,{},defaults,options);
  180. functions._reset.call(null);
  181. _axis=options.layout;
  182. _offset=options.offset;
  183. id=(id.indexOf("#")!==-1) ? id : "#"+id;
  184. if(functions._isValid.call(null,id) && functions._findTarget.call(null,id)){
  185. _trigger="scrollTo";
  186. _clicked=options.clicked;
  187. if(_clicked){
  188. functions._setClasses.call(null,true);
  189. }
  190. functions._scrollTo.call(null);
  191. }
  192. }
  193. },
  194. /* destroy method */
  195. destroy:function(){
  196. $(window).unbind("."+pluginPfx);
  197. $(document).undelegate("."+pluginPfx).removeData(pluginPfx);
  198. $("._"+pluginPfx+"-t").removeData(pluginPfx);
  199. functions._removeClasses.call(null,true);
  200. }
  201. },
  202. /*
  203. ---------------
  204. functions
  205. ---------------
  206. */
  207. functions={
  208. /* checks if screen size ([x,y]) is below the value(s) set in disablePluginBelow option */
  209. _isDisabled:function(){
  210. var e=window,a="inner",
  211. val=opt.disablePluginBelow instanceof Array ? [opt.disablePluginBelow[0] || 0,opt.disablePluginBelow[1] || 0] : [opt.disablePluginBelow || 0,0];
  212. if(!("innerWidth" in window )){
  213. a="client";
  214. e=document.documentElement || document.body;
  215. }
  216. return e[a+"Width"]<=val[0] || e[a+"Height"]<=val[1];
  217. },
  218. /* checks if href attribute is valid */
  219. _isValid:function(href,hrefProp){
  220. if(!href){
  221. return;
  222. }
  223. hrefProp=(!hrefProp) ? href : hrefProp;
  224. var str=(hrefProp.indexOf("#/")!==-1) ? hrefProp.split("#/")[0] : hrefProp.split("#")[0],
  225. loc=window.location.toString().split("#")[0];
  226. return href!=="#" && href.indexOf("#")!==-1 && (str==="" || decodeURIComponent(str)===decodeURIComponent(loc));
  227. },
  228. /* setup selectors, target elements, basic plugin classes etc. */
  229. _setup:function(){
  230. var el=functions._highlightSelector(),i=1,tp=0;
  231. return $(el).each(function(){
  232. var $this=$(this),href=$this.attr("href"),hrefProp=$this.prop("href");
  233. if(functions._isValid.call(null,href,hrefProp)){
  234. var id=(href.indexOf("#/")!==-1) ? href.split("#/")[1] : href.split("#")[1],t=$("#"+id);
  235. if(t.length>0){
  236. if(opt.highlightByNextTarget){
  237. if(t!==tp){
  238. if(!tp){t.data(pluginPfx,{tn:"0"});}else{tp.data(pluginPfx,{tn:t});}
  239. tp=t;
  240. }
  241. }
  242. if(!t.hasClass("_"+pluginPfx+"-t")){
  243. t.addClass("_"+pluginPfx+"-t");
  244. }
  245. t.data(pluginPfx,{i:i});
  246. if(!$this.hasClass("_"+pluginPfx+"-h")){
  247. $this.addClass("_"+pluginPfx+"-h");
  248. }
  249. var h=functions._findHighlight.call(null,id);
  250. functions._setClasses.call(null,false,t,h);
  251. _totalInstances=i;
  252. i++
  253. if(i==$(el).length){functions._extendClasses.call(null);}
  254. }
  255. }
  256. });
  257. },
  258. /* returns the highlight selector */
  259. _highlightSelector:function(){
  260. return (opt.highlightSelector && opt.highlightSelector!=="") ? opt.highlightSelector : selector;
  261. },
  262. /* finds the target element */
  263. _findTarget:function(str){
  264. var val=(str.indexOf("#/")!==-1) ? str.split("#/")[1] : str.split("#")[1],
  265. el=$("#"+val);
  266. if(el.length<1 || el.css("position")==="fixed"){
  267. if(val==="top"){
  268. el=$("body");
  269. }else{
  270. return;
  271. }
  272. }
  273. _target=el;
  274. if(!_axis){
  275. _axis=opt.layout;
  276. }
  277. _offset=functions._setOffset.call(null);
  278. _to=[(el.offset().top-_offset[0]).toString(),(el.offset().left-_offset[1]).toString()];
  279. _to[0]=(_to[0]<0) ? 0 : _to[0];
  280. _to[1]=(_to[1]<0) ? 0 : _to[1];
  281. return _to;
  282. },
  283. /* sets the offset value (pixels, objects etc.) */
  284. _setOffset:function(){
  285. if(!_offset){
  286. _offset=(opt.offset) ? opt.offset : 0;
  287. }
  288. if(_dataOffset){
  289. _offset=_dataOffset;
  290. }
  291. var val,obj,y,x;
  292. switch(typeof _offset){
  293. case "object":
  294. case "string":
  295. val=[(_offset["y"]) ? _offset["y"] : _offset,(_offset["x"]) ? _offset["x"] : _offset];
  296. obj=[(val[0] instanceof jQuery) ? val[0] : $(val[0]),(val[1] instanceof jQuery) ? val[1] : $(val[1])];
  297. if(obj[0].length>0){ // js/jquery object
  298. y=obj[0].height();
  299. if(obj[0].css("position")==="fixed"){ // include position for fixed elements
  300. y+=obj[0][0].offsetTop;
  301. }
  302. }else if(!isNaN(parseFloat(val[0])) && isFinite(val[0])){ // numeric string
  303. y=parseInt(val[0]);
  304. }else{
  305. y=0; // non-existing value
  306. }
  307. if(obj[1].length>0){ // js/jquery object
  308. x=obj[1].width();
  309. if(obj[1].css("position")==="fixed"){ // include position for fixed elements
  310. x+=obj[1][0].offsetLeft;
  311. }
  312. }else if(!isNaN(parseFloat(val[1])) && isFinite(val[1])){ // numeric string
  313. x=parseInt(val[1]);
  314. }else{
  315. x=0; // non-existing value
  316. }
  317. break;
  318. case "function":
  319. val=_offset.call(null); // function (single value or array)
  320. if(val instanceof Array){
  321. y=val[0];
  322. x=val[1];
  323. }else{
  324. y=x=val;
  325. }
  326. break;
  327. default:
  328. y=x=parseInt(_offset); // number
  329. }
  330. return [y,x];
  331. },
  332. /* finds the element that should be highlighted */
  333. _findHighlight:function(id){
  334. var wLoc=window.location,loc=wLoc.toString().split("#")[0],locPath=wLoc.pathname;
  335. 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+"']");
  336. },
  337. /* sets plugin classes */
  338. _setClasses:function(c,t,h){
  339. var cc=opt.clickedClass,tc=opt.targetClass,hc=opt.highlightClass;
  340. if(c && cc && cc!==""){
  341. $("."+cc).removeClass(cc);
  342. _clicked.addClass(cc);
  343. }else if(t && tc && tc!=="" && h && hc && hc!==""){
  344. if(functions._currentTarget.call(null,t)){
  345. t.addClass(tc);
  346. h.addClass(hc);
  347. }else{
  348. if(!opt.keepHighlightUntilNext || $("."+hc).length>1){
  349. t.removeClass(tc);
  350. h.removeClass(hc);
  351. }
  352. }
  353. }
  354. },
  355. /* extends plugin classes */
  356. _extendClasses:function(){
  357. var tc=opt.targetClass,hc=opt.highlightClass,
  358. $tc=$("."+tc),$hc=$("."+hc),ftc=tc+"-first",ltc=tc+"-last",fhc=hc+"-first",lhc=hc+"-last";
  359. $("._"+pluginPfx+"-t").removeClass(ftc+" "+ltc);
  360. $("._"+pluginPfx+"-h").removeClass(fhc+" "+lhc);
  361. if(!opt.forceSingleHighlight){
  362. $tc.slice(0,1).addClass(ftc).end().slice(-1).addClass(ltc);
  363. $hc.slice(0,1).addClass(fhc).end().slice(-1).addClass(lhc);
  364. }else{
  365. if(opt.keepHighlightUntilNext && $tc.length>1){
  366. $tc.slice(0,1).removeClass(tc); $hc.slice(0,1).removeClass(hc);
  367. }else{
  368. $tc.slice(1).removeClass(tc); $hc.slice(1).removeClass(hc);
  369. }
  370. }
  371. },
  372. /* removes plugin classes */
  373. _removeClasses:function(destroy){
  374. $("."+opt.clickedClass).removeClass(opt.clickedClass);
  375. $("."+opt.targetClass).removeClass(opt.targetClass+" "+opt.targetClass+"-first "+opt.targetClass+"-last");
  376. $("."+opt.highlightClass).removeClass(opt.highlightClass+" "+opt.highlightClass+"-first "+opt.highlightClass+"-last");
  377. if(destroy){
  378. $("._"+pluginPfx+"-t").removeClass("_"+pluginPfx+"-t");
  379. $("._"+pluginPfx+"-h").removeClass("_"+pluginPfx+"-h");
  380. }
  381. },
  382. /* checks if target element is in viewport */
  383. _currentTarget:function(t){
  384. var o=opt["target_"+t.data(pluginPfx).i],
  385. dataTarget=t.data("ps2id-target"),
  386. rect=dataTarget && $(dataTarget)[0] ? $(dataTarget)[0].getBoundingClientRect() : t[0].getBoundingClientRect();
  387. if(typeof o!=="undefined"){
  388. var y=t.offset().top,x=t.offset().left,
  389. from=(o.from) ? o.from+y : y,to=(o.to) ? o.to+y : y,
  390. fromX=(o.fromX) ? o.fromX+x : x,toX=(o.toX) ? o.toX+x : x;
  391. return(
  392. rect.top >= to && rect.top <= from &&
  393. rect.left >= toX && rect.left <= fromX
  394. );
  395. }else{
  396. var wh=$(window).height(),ww=$(window).width(),
  397. th=dataTarget ? $(dataTarget).height() : t.height(),tw=dataTarget ? $(dataTarget).width() : t.width(),
  398. base=1+(th/wh),
  399. top=base,bottom=(th<wh) ? base*(wh/th) : base,
  400. baseX=1+(tw/ww),
  401. left=baseX,right=(tw<ww) ? baseX*(ww/tw) : baseX,
  402. val=[rect.top <= wh/top,rect.bottom >= wh/bottom,rect.left <= ww/left,rect.right >= ww/right];
  403. if(opt.highlightByNextTarget){
  404. var tn=t.data(pluginPfx).tn;
  405. if(tn){
  406. var rectn=tn[0].getBoundingClientRect();
  407. if(opt.layout==="vertical"){
  408. val=[rect.top <= wh/2,rectn.top > wh/2,1,1];
  409. }else if(opt.layout==="horizontal"){
  410. val=[1,1,rect.left <= ww/2,rectn.left > ww/2];
  411. }
  412. }
  413. }
  414. return(val[0] && val[1] && val[2] && val[3]);
  415. }
  416. },
  417. /* scrolls the page */
  418. _scrollTo:function(){
  419. _speed=functions._scrollSpeed.call(null);
  420. _to=(opt.pageEndSmoothScroll) ? functions._pageEndSmoothScroll.call(null) : _to;
  421. var _scrollable=$("html,body"),
  422. speed=(opt.autoScrollSpeed) ? functions._autoScrollSpeed.call(null) : _speed,
  423. easing=(_scrollable.is(":animated")) ? opt.scrollingEasing : opt.scrollEasing,
  424. _t=$(window).scrollTop(),_l=$(window).scrollLeft();
  425. switch(_axis){
  426. case "horizontal":
  427. if(_l!=_to[1]){
  428. functions._callbacks.call(null,"onStart");
  429. _scrollable.stop().animate({scrollLeft:_to[1]},speed,easing).promise().then(function(){
  430. functions._callbacks.call(null,"onComplete");
  431. });
  432. }
  433. break;
  434. case "auto":
  435. if(_t!=_to[0] || _l!=_to[1]){
  436. functions._callbacks.call(null,"onStart");
  437. if(navigator.userAgent.match(/(iPod|iPhone|iPad|Android)/)){ // mobile fix
  438. var left;
  439. _scrollable.stop().animate({pageYOffset:_to[0],pageXOffset:_to[1]},{
  440. duration:speed,
  441. easing:easing,
  442. step:function(now,fx){
  443. if(fx.prop=='pageXOffset'){
  444. left=now;
  445. }else if(fx.prop=='pageYOffset'){
  446. window.scrollTo(left,now);
  447. }
  448. }
  449. }).promise().then(function(){
  450. functions._callbacks.call(null,"onComplete");
  451. });
  452. }else{
  453. _scrollable.stop().animate({scrollTop:_to[0],scrollLeft:_to[1]},speed,easing).promise().then(function(){
  454. functions._callbacks.call(null,"onComplete");
  455. });
  456. }
  457. }
  458. break;
  459. default:
  460. if(_t!=_to[0]){
  461. functions._callbacks.call(null,"onStart");
  462. _scrollable.stop().animate({scrollTop:_to[0]},speed,easing).promise().then(function(){
  463. functions._callbacks.call(null,"onComplete");
  464. });
  465. }
  466. }
  467. },
  468. /* sets end of page "smooth scrolling" position */
  469. _pageEndSmoothScroll:function(){
  470. var _dh=$(document).height(),_dw=$(document).width(),
  471. _wh=$(window).height(),_ww=$(window).width();
  472. return [((_dh-_to[0])<_wh) ? _dh-_wh : _to[0],((_dw-_to[1])<_ww) ? _dw-_ww : _to[1]];
  473. },
  474. /* sets animation speed (link-specific speed via ps2id-speed-VALUE class on link or link's parent) */
  475. _scrollSpeed:function(){
  476. var speed=opt.scrollSpeed;
  477. if(_clicked && _clicked.length){
  478. _clicked.add(_clicked.parent()).each(function(){
  479. var $this=$(this);
  480. if($this.attr("class")){
  481. var clickedClasses=$this.attr("class").split(" ");
  482. for(var index in clickedClasses){
  483. if(String(clickedClasses[index]).match(/^ps2id-speed-\d+$/)){
  484. speed=clickedClasses[index].split("ps2id-speed-")[1];
  485. break;
  486. }
  487. }
  488. }
  489. });
  490. }
  491. return parseInt(speed);
  492. },
  493. /* sets the auto-adjusted animation speed */
  494. _autoScrollSpeed:function(){
  495. var _t=$(window).scrollTop(),_l=$(window).scrollLeft(),
  496. _h=$(document).height(),_w=$(document).width(),
  497. val=[
  498. _speed+((_speed*(Math.floor((Math.abs(_to[0]-_t)/_h)*100)))/100),
  499. _speed+((_speed*(Math.floor((Math.abs(_to[1]-_l)/_w)*100)))/100)
  500. ];
  501. return Math.max.apply(Math,val);
  502. },
  503. /* user callback functions */
  504. _callbacks:function(c){
  505. if(!opt){
  506. return;
  507. }
  508. this[pluginPfx]={
  509. trigger:_trigger,clicked:_clicked,target:_target,scrollTo:{y:_to[0],x:_to[1]}
  510. };
  511. switch(c){
  512. case "onStart":
  513. //append hash to URL/address bar
  514. if(opt.appendHash && window.history && window.history.pushState && _clicked && _clicked.length){
  515. var h="#"+_clicked.attr("href").split("#")[1];
  516. if(h!==window.location.hash) history.pushState("","",h);
  517. }
  518. opt.onStart.call(null,this[pluginPfx]);
  519. break;
  520. case "onComplete":
  521. opt.onComplete.call(null,this[pluginPfx]);
  522. break;
  523. }
  524. },
  525. /* resets/clears vars and constants */
  526. _reset:function(){
  527. _axis=_offset=_dataOffset=false;
  528. },
  529. /* checks if plugin has initialized */
  530. _isInit:function(){
  531. if(!_init){
  532. methods.init.apply(this);
  533. }
  534. },
  535. /* live fn */
  536. _live:function(){
  537. _liveTimer=setTimeout(function(){
  538. if(opt.live){
  539. if($(functions._highlightSelector()).length!==_totalInstances){
  540. functions._setup.call(null);
  541. }
  542. }else{
  543. if(_liveTimer){clearTimeout(_liveTimer);}
  544. }
  545. functions._live.call(null);
  546. },1000);
  547. },
  548. /* extends jquery with custom easings (as jquery ui) */
  549. _easing:function(){
  550. $.easing.easeInQuad=$.easing.easeInQuad || function(x){
  551. return x*x;
  552. };
  553. $.easing.easeOutQuad=$.easing.easeOutQuad || function(x){
  554. return 1-(1-x)*(1-x);
  555. };
  556. $.easing.easeInOutQuad=$.easing.easeInOutQuad || function(x){
  557. return x<0.5 ? 2*x*x : 1-Math.pow(-2*x+2,2)/2;
  558. };
  559. $.easing.easeInCubic=$.easing.easeInCubic || function(x){
  560. return x*x*x;
  561. };
  562. $.easing.easeOutCubic=$.easing.easeOutCubic || function(x){
  563. return 1-Math.pow(1-x,3);
  564. };
  565. $.easing.easeInOutCubic=$.easing.easeInOutCubic || function(x){
  566. return x<0.5 ? 4*x*x*x : 1-Math.pow(-2*x+2,3)/2;
  567. };
  568. $.easing.easeInQuart=$.easing.easeInQuart || function(x){
  569. return x*x*x*x;
  570. };
  571. $.easing.easeOutQuart=$.easing.easeOutQuart || function(x){
  572. return 1-Math.pow(1-x,4);
  573. };
  574. $.easing.easeInOutQuart=$.easing.easeInOutQuart || function(x){
  575. return x<0.5 ? 8*x*x*x*x : 1-Math.pow(-2*x+2,4)/2;
  576. };
  577. $.easing.easeInQuint=$.easing.easeInQuint || function(x){
  578. return x*x*x*x*x;
  579. };
  580. $.easing.easeOutQuint=$.easing.easeOutQuint || function(x){
  581. return 1-Math.pow(1-x,5);
  582. };
  583. $.easing.easeInOutQuint=$.easing.easeInOutQuint || function(x){
  584. return x<0.5 ? 16*x*x*x*x*x : 1-Math.pow(-2*x+2,5)/2;
  585. };
  586. $.easing.easeInExpo=$.easing.easeInExpo || function(x){
  587. return x===0 ? 0 : Math.pow(2,10*x-10);
  588. };
  589. $.easing.easeOutExpo=$.easing.easeOutExpo || function(x){
  590. return x===1 ? 1 : 1-Math.pow(2,-10*x);
  591. };
  592. $.easing.easeInOutExpo=$.easing.easeInOutExpo || function(x){
  593. 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;
  594. };
  595. $.easing.easeInSine=$.easing.easeInSine || function(x){
  596. return 1-Math.cos(x*Math.PI/2);
  597. };
  598. $.easing.easeOutSine=$.easing.easeOutSine || function(x){
  599. return Math.sin(x*Math.PI/2);
  600. };
  601. $.easing.easeInOutSine=$.easing.easeInOutSine || function(x){
  602. return -(Math.cos(Math.PI*x)-1)/2;
  603. };
  604. $.easing.easeInCirc=$.easing.easeInCirc || function(x){
  605. return 1-Math.sqrt(1-Math.pow(x,2));
  606. };
  607. $.easing.easeOutCirc=$.easing.easeOutCirc || function(x){
  608. return Math.sqrt(1-Math.pow(x-1,2));
  609. };
  610. $.easing.easeInOutCirc=$.easing.easeInOutCirc || function(x){
  611. 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;
  612. };
  613. $.easing.easeInElastic=$.easing.easeInElastic || function(x){
  614. return x===0 ? 0 : x===1 ? 1 : -Math.pow(2,10*x-10)*Math.sin((x*10-10.75)*((2*Math.PI)/3));
  615. };
  616. $.easing.easeOutElastic=$.easing.easeOutElastic || function(x){
  617. return x===0 ? 0 : x===1 ? 1 : Math.pow(2,-10*x)*Math.sin((x*10-0.75)*((2*Math.PI)/3))+1;
  618. };
  619. $.easing.easeInOutElastic=$.easing.easeInOutElastic || function(x){
  620. 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;
  621. };
  622. $.easing.easeInBack=$.easing.easeInBack || function(x){
  623. return (1.70158+1)*x*x*x-1.70158*x*x;
  624. };
  625. $.easing.easeOutBack=$.easing.easeOutBack || function(x){
  626. return 1+(1.70158+1)*Math.pow(x-1,3)+1.70158*Math.pow(x-1,2);
  627. };
  628. $.easing.easeInOutBack=$.easing.easeInOutBack || function(x){
  629. 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;
  630. };
  631. $.easing.easeInBounce=$.easing.easeInBounce || function(x){
  632. return 1-__bounceOut(1-x);
  633. };
  634. $.easing.easeOutBounce=$.easing.easeOutBounce || __bounceOut;
  635. $.easing.easeInOutBounce=$.easing.easeInOutBounce || function(x){
  636. return x<0.5 ? (1-__bounceOut(1-2*x))/2 : (1+__bounceOut(2*x-1))/2;
  637. };
  638. function __bounceOut(x){
  639. var n1=7.5625,d1=2.75;
  640. if(x<1/d1){
  641. return n1*x*x;
  642. }else if(x<2/d1){
  643. return n1*(x-=(1.5/d1))*x+.75;
  644. }else if(x<2.5/d1){
  645. return n1*(x-=(2.25/d1))*x+.9375;
  646. }else{
  647. return n1*(x-=(2.625/d1))*x+.984375;
  648. }
  649. }
  650. }
  651. }
  652. /*
  653. ---------------
  654. plugin setup
  655. ---------------
  656. */
  657. /* extend jquery with custom easings */
  658. functions._easing.call();
  659. /* plugin constructor functions */
  660. $.fn[pluginNS]=function(method){
  661. if(methods[method]){
  662. return methods[method].apply(this,Array.prototype.slice.call(arguments,1));
  663. }else if(typeof method==="object" || !method){
  664. return methods.init.apply(this,arguments);
  665. }else{
  666. $.error("Method "+method+" does not exist");
  667. }
  668. };
  669. $[pluginNS]=function(method){
  670. if(methods[method]){
  671. return methods[method].apply(this,Array.prototype.slice.call(arguments,1));
  672. }else if(typeof method==="object" || !method){
  673. return methods.init.apply(this,arguments);
  674. }else{
  675. $.error("Method "+method+" does not exist");
  676. }
  677. };
  678. /*
  679. allow setting plugin default options.
  680. example: $.plugin_name.defaults.option_name="option_value";
  681. */
  682. $[pluginNS].defaults=defaults;
  683. })(jQuery,window,document);