Created
October 19, 2018 20:43
-
-
Save generatepress/cca22b47744175d840f01483c3960697 to your computer and use it in GitHub Desktop.
Potential fix for smooth scroll on mobile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
window.Element&&!Element.prototype.closest&&(Element.prototype.closest=function(e){var t,n=(this.document||this.ownerDocument).querySelectorAll(e),o=this;do{for(t=n.length;0<=--t&&n.item(t)!==o;);}while(t<0&&(o=o.parentElement));return o}),function(){if("function"==typeof window.CustomEvent)return;function e(e,t){t=t||{bubbles:!1,cancelable:!1,detail:void 0};var n=document.createEvent("CustomEvent");return n.initCustomEvent(e,t.bubbles,t.cancelable,t.detail),n}e.prototype=window.Event.prototype,window.CustomEvent=e}(),function(){for(var a=0,e=["ms","moz","webkit","o"],t=0;t<e.length&&!window.requestAnimationFrame;++t)window.requestAnimationFrame=window[e[t]+"RequestAnimationFrame"],window.cancelAnimationFrame=window[e[t]+"CancelAnimationFrame"]||window[e[t]+"CancelRequestAnimationFrame"];window.requestAnimationFrame||(window.requestAnimationFrame=function(e,t){var n=(new Date).getTime(),o=Math.max(0,16-(n-a)),i=window.setTimeout(function(){e(n+o)},o);return a=n+o,i}),window.cancelAnimationFrame||(window.cancelAnimationFrame=function(e){clearTimeout(e)})}(),function(e,t){"function"==typeof define&&define.amd?define([],function(){return t(e)}):"object"==typeof exports?module.exports=t(e):e.SmoothScroll=t(e)}("undefined"!=typeof global?global:"undefined"!=typeof window?window:this,function(A){"use strict";var q={ignore:"[data-scroll-ignore]",header:null,topOnEmptyHash:!0,speed:500,clip:!0,offset:0,easing:"easeInOutCubic",customEasing:null,updateURL:!0,popstate:!0,emitEvents:!0},C=function(){for(var n={},e=function(e){for(var t in e)e.hasOwnProperty(t)&&(n[t]=e[t])},t=0;t<arguments.length;t++)e(arguments[t]);return n},c=function(t){var n;try{n=decodeURIComponent(t)}catch(e){n=t}return n},s=function(e){"#"===e.charAt(0)&&(e=e.substr(1));for(var t,n,o=String(e),i=o.length,a=-1,r="",c=o.charCodeAt(0);++a<i;){if(0===(t=o.charCodeAt(a)))throw new InvalidCharacterError("Invalid character: the input contains U+0000.");1<=t&&t<=31||127==t||0===a&&48<=t&&t<=57||1===a&&48<=t&&t<=57&&45===c?r+="\\"+t.toString(16)+" ":r+=128<=t||45===t||95===t||48<=t&&t<=57||65<=t&&t<=90||97<=t&&t<=122?o.charAt(a):"\\"+o.charAt(a)}try{n=decodeURIComponent("#"+r)}catch(e){n="#"+r}return n},O=function(){return Math.max(document.body.scrollHeight,document.documentElement.scrollHeight,document.body.offsetHeight,document.documentElement.offsetHeight,document.body.clientHeight,document.documentElement.clientHeight)},I=function(e){return e?(t=e,parseInt(A.getComputedStyle(t).height,10)+e.offsetTop):0;var t},F=function(e,t,n,o){if(t.emitEvents&&"function"==typeof A.CustomEvent){var i=new CustomEvent(e,{bubbles:!0,detail:{anchor:n,toggle:o}});document.dispatchEvent(i)}};return function(o,e){var y,i,w,S,t,E,b={};b.cancelScroll=function(e){cancelAnimationFrame(E),E=null,e||F("scrollCancel",y)},b.animateScroll=function(r,c,e){var s=C(y||q,e||{}),u="[object Number]"===Object.prototype.toString.call(r),t=u||!r.tagName?null:r;if(u||t){var l=A.pageYOffset;s.header&&!w&&(w=document.querySelector(s.header)),S||(S=I(w));var m,i,a,n,o,d=u?r:function(e,t,n,o){var i=0;if(e.offsetParent)for(;i+=e.offsetTop,e=e.offsetParent;);return i=Math.max(i-t-n,0),o&&(i=Math.min(i,O()-A.innerHeight)),i}(t,S,parseInt("function"==typeof s.offset?s.offset(r,c):s.offset,10),s.clip),f=d-l,h=O(),p=0,g=function(e,t){var n,o,i,a=A.pageYOffset;if(e==t||a==t||(l<t&&A.innerHeight+a)>=h)return b.cancelScroll(!0),o=t,i=u,0===(n=r)&&document.body.focus(),i||(n.focus(),document.activeElement!==n&&(n.setAttribute("tabindex","-1"),n.focus(),n.style.outline="none"),A.scrollTo(0,o)),F("scrollStop",s,r,c),!(E=m=null)},v=function(e){var t,n,o;m||(m=e),i=(p+=e-m)/parseInt(s.speed,10),a=l+f*(n=i=1<i?1:i,"easeInQuad"===(t=s).easing&&(o=n*n),"easeOutQuad"===t.easing&&(o=n*(2-n)),"easeInOutQuad"===t.easing&&(o=n<.5?2*n*n:(4-2*n)*n-1),"easeInCubic"===t.easing&&(o=n*n*n),"easeOutCubic"===t.easing&&(o=--n*n*n+1),"easeInOutCubic"===t.easing&&(o=n<.5?4*n*n*n:(n-1)*(2*n-2)*(2*n-2)+1),"easeInQuart"===t.easing&&(o=n*n*n*n),"easeOutQuart"===t.easing&&(o=1- --n*n*n*n),"easeInOutQuart"===t.easing&&(o=n<.5?8*n*n*n*n:1-8*--n*n*n*n),"easeInQuint"===t.easing&&(o=n*n*n*n*n),"easeOutQuint"===t.easing&&(o=1+--n*n*n*n*n),"easeInOutQuint"===t.easing&&(o=n<.5?16*n*n*n*n*n:1+16*--n*n*n*n*n),t.customEasing&&(o=t.customEasing(n)),o||n),A.scrollTo(0,Math.floor(a)),g(a,d)||(E=A.requestAnimationFrame(v),m=e)};0===A.pageYOffset&&A.scrollTo(0,0),n=r,o=s,u||history.pushState&&o.updateURL&&history.pushState({smoothScroll:JSON.stringify(o),anchor:n.id},document.title,n===document.documentElement?"#top":"#"+n.id),F("scrollStart",s,r,c),b.cancelScroll(!0),A.requestAnimationFrame(v)}};var n=function(e){if(!("matchMedia"in A&&A.matchMedia("(prefers-reduced-motion)").matches)&&0===e.button&&!e.metaKey&&!e.ctrlKey&&"closest"in e.target&&(i=e.target.closest(o))&&"a"===i.tagName.toLowerCase()&&!e.target.closest(y.ignore)&&i.hostname===A.location.hostname&&i.pathname===A.location.pathname&&/#/.test(i.href)){var t=s(c(i.hash)),n=y.topOnEmptyHash&&"#"===t?document.documentElement:document.querySelector(t);(n=n||"#top"!==t?n:document.documentElement)&&(e.preventDefault(),b.animateScroll(n,i))}},a=function(e){if(null!==history.state&&history.state.smoothScroll&&history.state.smoothScroll===JSON.stringify(y)&&history.state.anchor){var t=document.querySelector(s(c(history.state.anchor)));t&&b.animateScroll(t,null,{updateURL:!1})}},r=function(e){t||(t=setTimeout(function(){t=null,S=I(w)},66))};return b.destroy=function(){y&&(document.removeEventListener("click",n,!1),A.removeEventListener("resize",r,!1),A.removeEventListener("popstate",a,!1),b.cancelScroll(),E=t=S=w=i=y=null)},b.init=function(e){if(!("querySelector"in document&&"addEventListener"in A&&"requestAnimationFrame"in A&&"closest"in A.Element.prototype))throw"Smooth Scroll: This browser does not support the required JavaScript methods and browser APIs.";b.destroy(),y=C(q,e||{}),w=y.header?document.querySelector(y.header):null,S=I(w),document.addEventListener("click",n,!1),w&&A.addEventListener("resize",r,!1),y.updateURL&&y.popstate&&A.addEventListener("popstate",a,!1)},b.init(e),b}});var scroll=new SmoothScroll(smooth.elements.join(),{speed:smooth.duration,offset:function(e,t){var n=document.querySelector(".main-navigation:not(.slideout-navigation)"),o=document.querySelector(".main-navigation.toggled"),i=0;return document.body.classList.contains("sticky-enabled")?n.classList.contains("navigation-stick")&&o&&o.contains(t)?i+=n.querySelector(".menu-toggle").clientHeight:i+=n.clientHeight:o&&o.contains(t)&&(i=i+n.clientHeight-n.querySelector(".menu-toggle").clientHeight),i}}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment