From 09a596edc9ddad9d833fb7db5e80728186fcc923 Mon Sep 17 00:00:00 2001 From: SaFrMo Date: Thu, 11 May 2017 16:30:17 -0400 Subject: [PATCH] If before callback returns false, cancel scroll. * Update jquery.scrollify.js If `before` callback returns `false,` cancel scroll * Updated min.js --- jquery.scrollify.js | 10 ++++++---- jquery.scrollify.min.js | 2 +- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/jquery.scrollify.js b/jquery.scrollify.js index 6da72eb..77e6024 100644 --- a/jquery.scrollify.js +++ b/jquery.scrollify.js @@ -101,7 +101,9 @@ if touchScroll is false - update index if(names[index]) { scrollable = false; if(callbacks) { - settings.before(index,elements); + if( typeof settings.before == 'function' && settings.before(index,elements) === false ){ + return true; + } } interstitialIndex = 1; destination = heights[index]; @@ -301,7 +303,7 @@ if touchScroll is false - update index return false; } if(delta<0) { - if(indexc&&f===!1&&p[c]&&(s=parseInt(o[c].outerHeight()/u.height()),E=parseInt(m[c])+(o[c].outerHeight()-u.height())),G.updateHash&&G.sectionName&&(C!==!0||0!==c))if(history.pushState)try{history.replaceState(null,null,n[c])}catch(a){b.console&&console.warn("Scrollify warning: Page must be hosted to manipulate the hash value.")}else b.location.hash=n[c];if(C&&(G.afterRender(),C=!1),r=c,d)a(G.target).stop().scrollTop(E),e&&G.after(c,o);else{if(x=!0,a().velocity?a(G.target).stop().velocity("scroll",{duration:G.scrollSpeed,easing:G.easing,offset:E,mobileHA:!1}):a(G.target).stop().animate({scrollTop:E},G.scrollSpeed,G.easing),b.location.hash.length&&G.sectionName&&b.console)try{a(b.location.hash).length&&console.warn("Scrollify warning: ID matches hash value - this will cause the page to anchor.")}catch(a){}a(G.target).promise().done(function(){x=!1,C=!1,e&&G.after(c,o)})}}}function f(a){function b(b){for(var c=0,d=a.slice(Math.max(a.length-b,1)),e=0;e=d}function g(a,b){for(var c=n.length;c>=0;c--)"string"==typeof a?n[c]===a&&(q=c,e(c,b,!0,!0)):c===a&&(q=c,e(c,b,!0,!0))}var h,i,j,k,l,m=[],n=[],o=[],p=[],q=0,r=0,s=1,t=!1,u=a(b),v=u.scrollTop(),w=!1,x=!1,y=!1,z=!1,A=[],B=(new Date).getTime(),C=!0,D=!1,E=0,F="onwheel"in c?"wheel":c.onmousewheel!==d?"mousewheel":"DOMMouseScroll",G={section:".section",sectionName:"section-name",interstitialSection:"",easing:"easeOutExpo",scrollSpeed:1100,offset:0,scrollbars:!0,target:"html,body",standardScrollElements:!1,setHeights:!0,overflowScroll:!0,updateHash:!0,touchScroll:!0,before:function(){},after:function(){},afterResize:function(){},afterRender:function(){}},H=function(d){function g(b){a().velocity?a(G.target).stop().velocity("scroll",{duration:G.scrollSpeed,easing:G.easing,offset:b,mobileHA:!1}):a(G.target).stop().animate({scrollTop:b},G.scrollSpeed,G.easing)}function r(b){b&&(v=u.scrollTop());var c=G.section;p=[],G.interstitialSection.length&&(c+=","+G.interstitialSection),G.scrollbars===!1&&(G.overflowScroll=!1),a(c).each(function(b){var c=a(this);G.setHeights?c.is(G.interstitialSection)?p[b]=!1:c.css("height","auto").outerHeight()0?m[c]=parseInt(d.offset().top)+G.offset:m[c]=parseInt(d.offset().top),G.sectionName&&d.data(G.sectionName)?n[c]="#"+d.data(G.sectionName).toString().replace(/ /g,"-"):d.is(G.interstitialSection)===!1?n[c]="#"+(c+1):(n[c]="#",c===a(f).length-1&&c>1&&(m[c]=m[c-1]+(parseInt(a(a(f)[c-1]).outerHeight())-parseInt(a(b).height()))+parseInt(d.outerHeight()))),o[c]=d;try{a(n[c]).length&&b.console&&console.warn("Scrollify warning: Section names can't match IDs - this will cause the browser to anchor.")}catch(a){}b.location.hash===n[c]&&(q=c,t=!0)}),!0===c&&e(q,!1,!1,!1)}function E(){return!p[q]||(v=u.scrollTop(),!(v>parseInt(m[q])))}function H(){return!p[q]||(v=u.scrollTop(),!(vq||E())&&(q=g,e(g,a,b,!1))},wheelHandler:function(c){if(z===!0)return!0;if(G.standardScrollElements&&(a(c.target).is(G.standardScrollElements)||a(c.target).closest(G.standardScrollElements).length))return!0;p[q]||c.preventDefault();var d=(new Date).getTime();c=c||b.event;var g=c.originalEvent.wheelDelta||-c.originalEvent.deltaY||-c.originalEvent.detail,h=Math.max(-1,Math.min(1,g));if(A.length>149&&A.shift(),A.push(Math.abs(g)),d-B>200&&(A=[]),B=d,x)return!1;if(h<0){if(q0&&q>0&&E()){if(!f(A))return!1;c.preventDefault(),q--,x=!0,e(q,!1,!0,!1)}},keyHandler:function(a){return z===!0||x!==!0&&void(38==a.keyCode||33==a.keyCode?q>0&&E()&&(a.preventDefault(),q--,e(q,!1,!0,!1)):40!=a.keyCode&&34!=a.keyCode||qMath.abs(k.touches.touchstart.x-k.touches.touchmove.x)&&(b.preventDefault(),k.touches.direction="y",k.options.timeStamp+k.options.timeGap<(new Date).getTime()&&0==k.touches.touchend&&(k.touches.touchend=!0,k.touches.touchstart.y>-1&&Math.abs(k.touches.touchmove.y-k.touches.touchstart.y)>k.options.distance&&(k.touches.touchstart.y-1&&k.touches.touchmove.y>-1&&"y"===k.touches.direction&&(Math.abs(k.touches.touchmove.y-k.touches.touchstart.y)>k.options.distance&&(k.touches.touchstart.ys?(g(parseInt(m[q])+u.height()*s),s+=1):g(parseInt(m[q])+(o[q].outerHeight()-u.height())))},up:function(){q>=0&&(E()&&q>0?(q--,e(q,!1,!0,!1)):s>2?(s-=1,g(parseInt(m[q])+u.height()*s)):(s=1,g(parseInt(m[q]))))},init:function(){c.addEventListener&&G.touchScroll&&(c.addEventListener("touchstart",k.touchHandler,!1),c.addEventListener("touchmove",k.touchHandler,!1),c.addEventListener("touchend",k.touchHandler,!1))}},l={refresh:function(a,b){clearTimeout(i),i=setTimeout(function(){r(!0),C(b,!1),a&&G.afterResize()},400)},handleUpdate:function(){l.refresh(!1,!1)},handleResize:function(){l.refresh(!0,!1)},handleOrientation:function(){l.refresh(!0,!0)}},G=a.extend(G,d),r(!1),C(!1,!0),!0===t?e(q,!1,!0,!0):setTimeout(function(){j.calculateNearest(!0,!1)},200),m.length&&(j.init(),k.init(),u.on("resize",l.handleResize),c.addEventListener&&b.addEventListener("orientationchange",l.handleOrientation,!1))};return H.move=function(b){return b!==d&&(b.originalEvent&&(b=a(this).attr("href")),void g(b,!1))},H.instantMove=function(a){return a!==d&&void g(a,!0)},H.next=function(){q0&&(q-=1,e(q,!1,!0,!0))},H.instantNext=function(){q0&&(q-=1,e(q,!0,!0,!0))},H.destroy=function(){return!!D&&(G.setHeights&&a(G.section).each(function(){a(this).css("height","auto")}),u.off("resize",l.handleResize),G.scrollbars&&(u.off("mousedown",j.handleMousedown),u.off("mouseup",j.handleMouseup),u.off("scroll",j.handleScroll)),u.off(F,j.wheelHandler),u.off("keydown",j.keyHandler),c.addEventListener&&G.touchScroll&&(c.removeEventListener("touchstart",k.touchHandler,!1),c.removeEventListener("touchmove",k.touchHandler,!1),c.removeEventListener("touchend",k.touchHandler,!1)),m=[],n=[],o=[],void(p=[]))},H.update=function(){return!!D&&void l.handleUpdate()},H.current=function(){return o[q]},H.disable=function(){z=!0},H.enable=function(){z=!1,D&&j.calculateNearest(!1,!1)},H.isDisabled=function(){return z},H.setOptions=function(c){return!!D&&void("object"==typeof c?(G=a.extend(G,c),l.handleUpdate()):b.console&&console.warn("Scrollify warning: setOptions expects an object."))},a.scrollify=H,H}); \ No newline at end of file +!function(a,b){"use strict";"function"==typeof define&&define.amd?define(["jquery"],function(c){return b(c,a,a.document)}):"object"==typeof module&&module.exports?module.exports=b(require("jquery"),a,a.document):b(jQuery,a,a.document)}("undefined"!=typeof window?window:this,function(a,b,c,d){"use strict";function E(c,d,i,l){if(j===c&&(i=!1),!0===w)return!0;if(f[c]){if(q=!1,i&&"function"==typeof D.before&&!1===D.before(c,g))return!0;if(k=1,B=e[c],!1===z&&j>c&&!1===l&&h[c]&&(k=parseInt(g[c].outerHeight()/o.height()),B=parseInt(e[c])+(g[c].outerHeight()-o.height())),D.updateHash&&D.sectionName&&(!0!==z||0!==c))if(history.pushState)try{history.replaceState(null,null,f[c])}catch(a){b.console&&console.warn("Scrollify warning: Page must be hosted to manipulate the hash value.")}else b.location.hash=f[c];if(z&&(D.afterRender(),z=!1),j=c,d)a(D.target).stop().scrollTop(B),i&&D.after(c,g);else{if(r=!0,a().velocity?a(D.target).stop().velocity("scroll",{duration:D.scrollSpeed,easing:D.easing,offset:B,mobileHA:!1}):a(D.target).stop().animate({scrollTop:B},D.scrollSpeed,D.easing),b.location.hash.length&&D.sectionName&&b.console)try{a(b.location.hash).length&&console.warn("Scrollify warning: ID matches hash value - this will cause the page to anchor.")}catch(a){}a(D.target).promise().done(function(){r=!1,z=!1,i&&D.after(c,g)})}}}function F(a){function b(b){for(var c=0,d=a.slice(Math.max(a.length-b,1)),e=0;e=b(70)}function H(a,b){for(var c=f.length;c>=0;c--)"string"==typeof a?f[c]===a&&(i=c,E(c,b,!0,!0)):c===a&&(i=c,E(c,b,!0,!0))}var m,n,t,u,v,e=[],f=[],g=[],h=[],i=0,j=0,k=1,l=!1,o=a(b),p=o.scrollTop(),q=!1,r=!1,s=!1,w=!1,x=[],y=(new Date).getTime(),z=!0,A=!1,B=0,C="onwheel"in c?"wheel":c.onmousewheel!==d?"mousewheel":"DOMMouseScroll",D={section:".section",sectionName:"section-name",interstitialSection:"",easing:"easeOutExpo",scrollSpeed:1100,offset:0,scrollbars:!0,target:"html,body",standardScrollElements:!1,setHeights:!0,overflowScroll:!0,updateHash:!0,touchScroll:!0,before:function(){},after:function(){},afterResize:function(){},afterRender:function(){}},G=function(d){function j(b){a().velocity?a(D.target).stop().velocity("scroll",{duration:D.scrollSpeed,easing:D.easing,offset:b,mobileHA:!1}):a(D.target).stop().animate({scrollTop:b},D.scrollSpeed,D.easing)}function z(b){b&&(p=o.scrollTop());var c=D.section;h=[],D.interstitialSection.length&&(c+=","+D.interstitialSection),!1===D.scrollbars&&(D.overflowScroll=!1),a(c).each(function(b){var c=a(this);D.setHeights?c.is(D.interstitialSection)?h[b]=!1:c.css("height","auto").outerHeight()0?parseInt(d.offset().top)+D.offset:parseInt(d.offset().top),D.sectionName&&d.data(D.sectionName)?f[c]="#"+d.data(D.sectionName).toString().replace(/ /g,"-"):!1===d.is(D.interstitialSection)?f[c]="#"+(c+1):(f[c]="#",c===a(h).length-1&&c>1&&(e[c]=e[c-1]+(parseInt(a(a(h)[c-1]).outerHeight())-parseInt(a(b).height()))+parseInt(d.outerHeight()))),g[c]=d;try{a(f[c]).length&&b.console&&console.warn("Scrollify warning: Section names can't match IDs - this will cause the browser to anchor.")}catch(a){}b.location.hash===f[c]&&(i=c,l=!0)}),!0===c&&E(i,!1,!1,!1)}function G(){return!h[i]||!((p=o.scrollTop())>parseInt(e[i]))}function H(){return!h[i]||!((p=o.scrollTop())i||G())&&(i=f,E(f,a,b,!1))},wheelHandler:function(c){if(!0===w)return!0;if(D.standardScrollElements&&(a(c.target).is(D.standardScrollElements)||a(c.target).closest(D.standardScrollElements).length))return!0;h[i]||c.preventDefault();var d=(new Date).getTime();c=c||b.event;var f=c.originalEvent.wheelDelta||-c.originalEvent.deltaY||-c.originalEvent.detail,g=Math.max(-1,Math.min(1,f));if(x.length>149&&x.shift(),x.push(Math.abs(f)),d-y>200&&(x=[]),y=d,r)return!1;if(g<0){if(i0&&i>0&&G()){if(!F(x))return!1;c.preventDefault(),i--,r=!0,E(i,!1,!0,!1)}},keyHandler:function(a){return!0===w||!0!==r&&void(38==a.keyCode||33==a.keyCode?i>0&&G()&&(a.preventDefault(),i--,E(i,!1,!0,!1)):40!=a.keyCode&&34!=a.keyCode||iMath.abs(u.touches.touchstart.x-u.touches.touchmove.x)&&(b.preventDefault(),u.touches.direction="y",u.options.timeStamp+u.options.timeGap<(new Date).getTime()&&0==u.touches.touchend&&(u.touches.touchend=!0,u.touches.touchstart.y>-1&&Math.abs(u.touches.touchmove.y-u.touches.touchstart.y)>u.options.distance&&(u.touches.touchstart.y-1&&u.touches.touchmove.y>-1&&"y"===u.touches.direction&&(Math.abs(u.touches.touchmove.y-u.touches.touchstart.y)>u.options.distance&&(u.touches.touchstart.yk?(j(parseInt(e[i])+o.height()*k),k+=1):j(parseInt(e[i])+(g[i].outerHeight()-o.height())))},up:function(){i>=0&&(G()&&i>0?(i--,E(i,!1,!0,!1)):k>2?(k-=1,j(parseInt(e[i])+o.height()*k)):(k=1,j(parseInt(e[i]))))},init:function(){c.addEventListener&&D.touchScroll&&(c.addEventListener("touchstart",u.touchHandler,!1),c.addEventListener("touchmove",u.touchHandler,!1),c.addEventListener("touchend",u.touchHandler,!1))}},v={refresh:function(a,b){clearTimeout(n),n=setTimeout(function(){z(!0),B(b,!1),a&&D.afterResize()},400)},handleUpdate:function(){v.refresh(!1,!1)},handleResize:function(){v.refresh(!0,!1)},handleOrientation:function(){v.refresh(!0,!0)}},D=a.extend(D,d),z(!1),B(!1,!0),!0===l?E(i,!1,!0,!0):setTimeout(function(){t.calculateNearest(!0,!1)},200),e.length&&(t.init(),u.init(),o.on("resize",v.handleResize),c.addEventListener&&b.addEventListener("orientationchange",v.handleOrientation,!1))};return G.move=function(b){if(b===d)return!1;b.originalEvent&&(b=a(this).attr("href")),H(b,!1)},G.instantMove=function(a){if(a===d)return!1;H(a,!0)},G.next=function(){i0&&(i-=1,E(i,!1,!0,!0))},G.instantNext=function(){i0&&(i-=1,E(i,!0,!0,!0))},G.destroy=function(){if(!A)return!1;D.setHeights&&a(D.section).each(function(){a(this).css("height","auto")}),o.off("resize",v.handleResize),D.scrollbars&&(o.off("mousedown",t.handleMousedown),o.off("mouseup",t.handleMouseup),o.off("scroll",t.handleScroll)),o.off(C,t.wheelHandler),o.off("keydown",t.keyHandler),c.addEventListener&&D.touchScroll&&(c.removeEventListener("touchstart",u.touchHandler,!1),c.removeEventListener("touchmove",u.touchHandler,!1),c.removeEventListener("touchend",u.touchHandler,!1)),e=[],f=[],g=[],h=[]},G.update=function(){if(!A)return!1;v.handleUpdate()},G.current=function(){return g[i]},G.disable=function(){w=!0},G.enable=function(){w=!1,A&&t.calculateNearest(!1,!1)},G.isDisabled=function(){return w},G.setOptions=function(c){if(!A)return!1;"object"==typeof c?(D=a.extend(D,c),v.handleUpdate()):b.console&&console.warn("Scrollify warning: setOptions expects an object.")},a.scrollify=G,G});