diff --git a/src/pageanimation.js b/src/pageanimation.js index cc0b858..c7030b7 100644 --- a/src/pageanimation.js +++ b/src/pageanimation.js @@ -33,15 +33,12 @@ * @returns {Node} the anchor element */ function _getTargetAnchor(e) { - var url; - if (e.target.href) { - return e.target; - } else { - for (var i = 0; i < e.path.length; i++) { - if (e.path[i].href) { - return e.path[i]; - } + var target = e.target; + while (target.parentNode) { + if (target.href) { + return target; } + target = target.parentNode; } } @@ -245,7 +242,6 @@ percentComplete = elapsed / scrollDuration; scrollToPercent = _easeInOutQuad(percentComplete, elapsed, 0, 1, scrollDuration); scrollToY = scrollToPercent * distanceToTravel + startY; - console.log(percentComplete, elapsed, scrollToPercent, scrollToY, startY); window.scrollTo(0, scrollToY); requestAnimationFrame(step); } else { diff --git a/src/pageanimation.min.js b/src/pageanimation.min.js index fad9099..de2c9ad 100644 --- a/src/pageanimation.min.js +++ b/src/pageanimation.min.js @@ -1 +1 @@ -!function(n){"use strict";function t(){var n,t=document.createElement("fakeelement"),i={WebkitTransition:"webkitTransitionEnd",MozTransition:"transitionend",MSTransition:"msTransitionEnd",OTransition:"otransitionend",transition:"transitionend"};for(n in i)if(i.hasOwnProperty(n)&&void 0!==t.style[n])return i[n]}function i(n){if(n.target.href)return n.target;for(var t=0;th?(r=Date.now()-a,h=r/t,s=e(h,r,0,1,t),l=s*u+c,console.log(h,r,s,l,c),window.scrollTo(0,l),requestAnimationFrame(o)):(window.scrollTo(0,n),i())},15)}i=i||function(){};var r,s,l,a=Date.now(),c=window.scrollY,u=n-c,h=0;o()},r.prototype._onTransitionEnd=function(n){if(this.currentAnimation){var t=this.currentAnimation;this.currentAnimation=null;var i=function(){window.location.pathname===this.targetUrl?window.location.reload():window.location=this.targetUrl}.bind(this);t.shouldScroll&&"after"===t.scrollTiming?r.scrollTo(this.cb.computeScrollOffset(t),t.scrollDuration,i):i()}},r.prototype._animate=function(n){this.cb.beforeAnimationStart(n),n.finalElement.addEventListener(this.transitionEndEvent,this.boundOnTransitionEnd),this.currentAnimation=n;var t=function(){this.targetUrl=n.path,this.body.className+=n.bodyClass}.bind(this);n.shouldScroll&&"before"===n.scrollTiming?r.scrollTo(this.cb.computeScrollOffset(n),n.scrollDuration,t):n.shouldScroll&&"during"===n.scrollTiming?(setTimeout(t,0),r.scrollTo(this.cb.computeScrollOffset(n),n.scrollDuration)):t()},r.prototype._onClick=function(n){var t=i(n),e=o(t);if(!this.currentAnimation&&this.cb.shouldAnimate(t,e)&&(this.settings.animateLinksToSelf||this.path!==window.location.pathname))for(var r in this.animations)if(this.animations.hasOwnProperty(r)&&this.animations[r].regex.test(e)){n.preventDefault();var s=this.animations[r];return s.anchor=t,s.path=e,void this._animate(s)}},"function"==typeof define&&define.amd?define(r):"undefined"!=typeof module&&module.exports?module.exports=r:n.PageAnimation=r}(this); \ No newline at end of file +!function(n){"use strict";function t(){var n,t=document.createElement("fakeelement"),i={WebkitTransition:"webkitTransitionEnd",MozTransition:"transitionend",MSTransition:"msTransitionEnd",OTransition:"otransitionend",transition:"transitionend"};for(n in i)if(i.hasOwnProperty(n)&&void 0!==t.style[n])return i[n]}function i(n){for(var t=n.target;t.parentNode;){if(t.href)return t;t=t.parentNode}}function o(n){return n.href.replace(window.location.origin,"")}function e(n,t,i,o,e){return(t/=e/2)<1?o/2*t*t+i:-o/2*(--t*(t-2)-1)+i}function r(n){return this.defaults={shouldScroll:n.shouldScroll||!0,scrollTiming:n.scrollTiming||"before",scrollDuration:n.scrollDuration||200},this.settings={animateLinksToSelf:n.animateLinksToSelf||!1},this.cb={computeScrollOffset:n.computeScrollOffset||function(){return 0},shouldAnimate:n.shouldAnimate||function(){return!0},beforeAnimationStart:n.beforeAnimationStart||function(){},onTransitionEnd:n.onTransitionEnd||function(){}},this.animations={},this.body=document.getElementsByTagName("body")[0],this.transitionEndEvent=t(),this.links=document.getElementsByTagName("a"),this.currentAnimation=null,this.boundOnTransitionEnd=this._onTransitionEnd.bind(this),this.boundOnClick=this._onClick.bind(this),0===this.links.length&&console.error("No links found in page."),this}r.prototype.register=function(n,t,i,o){var e=o||{},r={anchor:null,bodyClass:" "+i+" ",finalElement:document.getElementById(t),path:null,regex:new RegExp(n),shouldScroll:e.shouldScroll||this.defaults.shouldScroll,scrollTiming:e.scrollTiming||this.defaults.scrollTiming,scrollDuration:e.scrollDuration||this.defaults.scrollDuration};return r.finalElement||console.error("No element with ID "+t),this.animations[n]=r,this},r.prototype.deregister=function(n){return this.animations[n]||console.error("No animation registered with regex "+n),delete this.animations[n],this},r.prototype.enable=function(){for(var n=0;nh?(r=Date.now()-a,h=r/t,s=e(h,r,0,1,t),l=s*u+c,window.scrollTo(0,l),requestAnimationFrame(o)):(window.scrollTo(0,n),i())},15)}i=i||function(){};var r,s,l,a=Date.now(),c=window.scrollY,u=n-c,h=0;o()},r.prototype._onTransitionEnd=function(n){if(this.currentAnimation){var t=this.currentAnimation;this.currentAnimation=null;var i=function(){window.location.pathname===this.targetUrl?window.location.reload():window.location=this.targetUrl}.bind(this);t.shouldScroll&&"after"===t.scrollTiming?r.scrollTo(this.cb.computeScrollOffset(t),t.scrollDuration,i):i()}},r.prototype._animate=function(n){this.cb.beforeAnimationStart(n),n.finalElement.addEventListener(this.transitionEndEvent,this.boundOnTransitionEnd),this.currentAnimation=n;var t=function(){this.targetUrl=n.path,this.body.className+=n.bodyClass}.bind(this);n.shouldScroll&&"before"===n.scrollTiming?r.scrollTo(this.cb.computeScrollOffset(n),n.scrollDuration,t):n.shouldScroll&&"during"===n.scrollTiming?(setTimeout(t,0),r.scrollTo(this.cb.computeScrollOffset(n),n.scrollDuration)):t()},r.prototype._onClick=function(n){var t=i(n),e=o(t);if(!this.currentAnimation&&this.cb.shouldAnimate(t,e)&&(this.settings.animateLinksToSelf||this.path!==window.location.pathname))for(var r in this.animations)if(this.animations.hasOwnProperty(r)&&this.animations[r].regex.test(e)){n.preventDefault();var s=this.animations[r];return s.anchor=t,s.path=e,void this._animate(s)}},"function"==typeof define&&define.amd?define(r):"undefined"!=typeof module&&module.exports?module.exports=r:n.PageAnimation=r}(this); \ No newline at end of file