forked from peachananr/onepage-scroll
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.onepage-scroll.min.js
1 lines (1 loc) · 8.36 KB
/
jquery.onepage-scroll.min.js
1
!function(e){var a={sectionContainer:"section",easing:"ease",animationTime:1e3,pagination:!0,updateURL:!1,keyboard:!0,beforeMove:null,afterMove:null,loop:!0,responsiveFallback:!1,direction:"vertical"},n="";n="onwheel"in document?"wheel":"onmousewheel"in document?"mousewheel":"MozMousePixelScroll",e.fn.swipeEvents=function(){return this.each(function(){function a(e){var a=e.originalEvent.touches;a&&a.length&&(i=a[0].pageX,t=a[0].pageY,o.bind("touchmove",n))}function n(e){var a=e.originalEvent.touches;if(a&&a.length){var s=i-a[0].pageX,d=t-a[0].pageY;s>=50&&o.trigger("swipeLeft"),-50>=s&&o.trigger("swipeRight"),d>=50&&o.trigger("swipeUp"),-50>=d&&o.trigger("swipeDown"),(Math.abs(s)>=50||Math.abs(d)>=50)&&o.unbind("touchmove",n)}}var i,t,o=e(this);o.bind("touchstart",a)})},e.fn.onepage_scroll=function(i){function t(){var a=!1,i=typeof s.responsiveFallback;if("number"==i&&(a=e(window).width()<s.responsiveFallback),"boolean"==i&&(a=s.responsiveFallback),"function"==i){var t=s.responsiveFallback();a=t;var r=typeof a;"number"==r&&(a=e(window).width()<t)}a?(e("body").addClass("disabled-onepage-scroll"),e(document).unbind(n),d.swipeEvents().unbind("swipeDown swipeUp")):(e("body").hasClass("disabled-onepage-scroll")&&(e("body").removeClass("disabled-onepage-scroll"),e("html, body, .wrapper").animate({scrollTop:0},"fast")),d.swipeEvents().bind("swipeDown",function(a){e("body").hasClass("disabled-onepage-scroll")||a.preventDefault(),d.moveUp()}).bind("swipeUp",function(a){e("body").hasClass("disabled-onepage-scroll")||a.preventDefault(),d.moveDown()}),e(document).bind(n,function(e){e.preventDefault(),o(e)}))}function o(e){var a=-e.originalEvent.deltaY||-e.originalEvent.detail||e.originalEvent.wheelDelta,n=(new Date).getTime();return n-g<v+s.animationTime?void e.preventDefault():(0>a?d.moveDown():d.moveUp(),void(g=n))}var s=e.extend({},a,i),d=e(this),r=e(s.sectionContainer),l=r.length,c=0,p=0,g=0,v=500,f="";if(e.fn.transformPage=function(a,n,i){if("function"==typeof a.beforeMove&&a.beforeMove(i),e("html").hasClass("ie8"))if("horizontal"==a.direction){var t=d.width()/100*n;e(this).animate({left:t+"px"},a.animationTime)}else{var t=d.height()/100*n;e(this).animate({top:t+"px"},a.animationTime)}else e(this).css({"-webkit-transform":"horizontal"==a.direction?"translate3d("+n+"%, 0, 0)":"translate3d(0, "+n+"%, 0)","-webkit-transition":"all "+a.animationTime+"ms "+a.easing,"-moz-transform":"horizontal"==a.direction?"translate3d("+n+"%, 0, 0)":"translate3d(0, "+n+"%, 0)","-moz-transition":"all "+a.animationTime+"ms "+a.easing,"-ms-transform":"horizontal"==a.direction?"translate3d("+n+"%, 0, 0)":"translate3d(0, "+n+"%, 0)","-ms-transition":"all "+a.animationTime+"ms "+a.easing,transform:"horizontal"==a.direction?"translate3d("+n+"%, 0, 0)":"translate3d(0, "+n+"%, 0)",transition:"all "+a.animationTime+"ms "+a.easing});e(this).one("webkitTransitionEnd otransitionend oTransitionEnd msTransitionEnd transitionend",function(){"function"==typeof a.afterMove&&a.afterMove(i)})},e.fn.moveDown=function(){var a,n=e(this),i=e(s.sectionContainer+".active").data("index"),t=e(s.sectionContainer+"[data-index='"+i+"']"),o=e(s.sectionContainer+"[data-index='"+(i+1)+"']");if(o.length<1){if(1!=s.loop)return;a=0,o=e(s.sectionContainer+"[data-index='1']")}else a=100*i*-1;if("function"==typeof s.beforeMove&&s.beforeMove(o.data("index")),t.removeClass("active"),o.addClass("active"),1==s.pagination&&(e(".onepage-pagination li a[data-index='"+i+"']").removeClass("active"),e(".onepage-pagination li a[data-index='"+o.data("index")+"']").addClass("active")),e("body")[0].className=e("body")[0].className.replace(/\bviewing-page-\d.*?\b/g,""),e("body").addClass("viewing-page-"+o.data("index")),history.replaceState&&1==s.updateURL){var d=window.location.href.substr(0,window.location.href.indexOf("#"))+"#"+(i+1);history.pushState({},document.title,d)}n.transformPage(s,a,o.data("index"))},e.fn.moveUp=function(){var a,n=e(this),i=e(s.sectionContainer+".active").data("index"),t=e(s.sectionContainer+"[data-index='"+i+"']"),o=e(s.sectionContainer+"[data-index='"+(i-1)+"']");if(o.length<1){if(1!=s.loop)return;a=100*(l-1)*-1,o=e(s.sectionContainer+"[data-index='"+l+"']")}else a=100*(o.data("index")-1)*-1;if("function"==typeof s.beforeMove&&s.beforeMove(o.data("index")),t.removeClass("active"),o.addClass("active"),1==s.pagination&&(e(".onepage-pagination li a[data-index='"+i+"']").removeClass("active"),e(".onepage-pagination li a[data-index='"+o.data("index")+"']").addClass("active")),e("body")[0].className=e("body")[0].className.replace(/\bviewing-page-\d.*?\b/g,""),e("body").addClass("viewing-page-"+o.data("index")),history.replaceState&&1==s.updateURL){var d=window.location.href.substr(0,window.location.href.indexOf("#"))+"#"+(i-1);history.pushState({},document.title,d)}n.transformPage(s,a,o.data("index"))},e.fn.moveTo=function(a){var n,i=e(s.sectionContainer+".active"),t=e(s.sectionContainer+"[data-index='"+a+"']");if(t.length>0){if("function"==typeof s.beforeMove&&s.beforeMove(t.data("index")),i.removeClass("active"),t.addClass("active"),e(".onepage-pagination li a.active").removeClass("active"),e(".onepage-pagination li a[data-index='"+a+"']").addClass("active"),e("body")[0].className=e("body")[0].className.replace(/\bviewing-page-\d.*?\b/g,""),e("body").addClass("viewing-page-"+t.data("index")),n=100*(a-1)*-1,history.replaceState&&1==s.updateURL){var o=window.location.href.substr(0,window.location.href.indexOf("#"))+"#"+(a-1);history.pushState({},document.title,o)}d.transformPage(s,n,a)}},d.addClass("onepage-wrapper").css("position","relative"),e.each(r,function(a){e(this).css({position:"absolute",top:c+"%"}).addClass("section").attr("data-index",a+1),e(this).css({position:"absolute",left:"horizontal"==s.direction?p+"%":0,top:"vertical"==s.direction||"horizontal"!=s.direction?c+"%":0}),"horizontal"==s.direction?p+=100:c+=100,1==s.pagination&&(f+="<li><a data-index='"+(a+1)+"' href='#"+(a+1)+"'></a></li>")}),d.swipeEvents().bind("swipeDown",function(a){e("body").hasClass("disabled-onepage-scroll")||a.preventDefault(),d.moveUp()}).bind("swipeUp",function(a){e("body").hasClass("disabled-onepage-scroll")||a.preventDefault(),d.moveDown()}),1==s.pagination){if(e("ul.onepage-pagination").length<1&&e("<ul class='onepage-pagination'></ul>").prependTo("body"),"horizontal"==s.direction){var u=d.find(".onepage-pagination").width()/2*-1;d.find(".onepage-pagination").css("margin-left",u)}else{var b=d.find(".onepage-pagination").height()/2*-1;d.find(".onepage-pagination").css("margin-top",b)}e("ul.onepage-pagination").html(f)}if(""!=window.location.hash&&"#1"!=window.location.hash){var h=window.location.hash.replace("#","");if(parseInt(h)<=l&&parseInt(h)>0){if(e(s.sectionContainer+"[data-index='"+h+"']").addClass("active"),e("body").addClass("viewing-page-"+h),1==s.pagination&&e(".onepage-pagination li a[data-index='"+h+"']").addClass("active"),next=e(s.sectionContainer+"[data-index='"+h+"']"),next&&(next.addClass("active"),1==s.pagination&&e(".onepage-pagination li a[data-index='"+h+"']").addClass("active"),e("body")[0].className=e("body")[0].className.replace(/\bviewing-page-\d.*?\b/g,""),e("body").addClass("viewing-page-"+next.data("index")),history.replaceState&&1==s.updateURL)){var m=window.location.href.substr(0,window.location.href.indexOf("#"))+"#"+h;history.pushState({},document.title,m)}var w=100*(h-1)*-1;d.transformPage(s,w,h)}else e(s.sectionContainer+"[data-index='1']").addClass("active"),e("body").addClass("viewing-page-1"),1==s.pagination&&e(".onepage-pagination li a[data-index='1']").addClass("active")}else e(s.sectionContainer+"[data-index='1']").addClass("active"),e("body").addClass("viewing-page-1"),1==s.pagination&&e(".onepage-pagination li a[data-index='1']").addClass("active");return 1==s.pagination&&e(".onepage-pagination li a").click(function(){var a=e(this).data("index");d.moveTo(a)}),e(document).bind(n,function(a){a.preventDefault(),e("body").hasClass("disabled-onepage-scroll")||o(a)}),0!=s.responsiveFallback&&(e(window).resize(function(){t()}),t()),1==s.keyboard&&e(document).keydown(function(a){var n=a.target.tagName.toLowerCase();if(!e("body").hasClass("disabled-onepage-scroll"))switch(a.which){case 38:"input"!=n&&"textarea"!=n&&d.moveUp();break;case 40:"input"!=n&&"textarea"!=n&&d.moveDown();break;case 32:"input"!=n&&"textarea"!=n&&d.moveDown();break;case 33:"input"!=n&&"textarea"!=n&&d.moveUp();break;case 34:"input"!=n&&"textarea"!=n&&d.moveDown();break;case 36:d.moveTo(1);break;case 35:d.moveTo(l);break;default:return}}),!1}}(window.jQuery);