react-scroll
Advanced tools
Comparing version 0.21.0 to 0.22.0
@@ -51,2 +51,4 @@ "use strict"; | ||
componentDidMount: function() { | ||
scrollSpy.mount(); | ||
if(this.props.spy) { | ||
@@ -53,0 +55,0 @@ var to = this.props.to; |
@@ -1,44 +0,47 @@ | ||
var spyCallbacks = []; | ||
var spySetState = []; | ||
var scrollSpy = { | ||
spyCallbacks: [], | ||
spySetState: [], | ||
var currentPositionY = function() { | ||
var supportPageOffset = window.pageXOffset !== undefined; | ||
var isCSS1Compat = ((document.compatMode || "") === "CSS1Compat"); | ||
return supportPageOffset ? window.pageYOffset : isCSS1Compat ? | ||
document.documentElement.scrollTop : document.body.scrollTop; | ||
}; | ||
mount: function () { | ||
if (typeof document !== 'undefined') { | ||
document.addEventListener('scroll', this.scrollHandler.bind(this)); | ||
} | ||
}, | ||
currentPositionY: function () { | ||
var supportPageOffset = window.pageXOffset !== undefined; | ||
var isCSS1Compat = ((document.compatMode || "") === "CSS1Compat"); | ||
return supportPageOffset ? window.pageYOffset : isCSS1Compat ? | ||
document.documentElement.scrollTop : document.body.scrollTop; | ||
}, | ||
var scrollHandler = function() { | ||
for(var i = 0; i < spyCallbacks.length; i = i + 1) { | ||
spyCallbacks[i](currentPositionY()); | ||
} | ||
}; | ||
if (typeof document !== 'undefined') { | ||
document.addEventListener('scroll', scrollHandler); | ||
} | ||
module.exports = { | ||
unmount: function(){ | ||
document.removeEventListener('scroll', scrollHandler); | ||
spySetState = []; | ||
spyCallbacks = []; | ||
scrollHandler: function () { | ||
for(var i = 0; i < this.spyCallbacks.length; i++) { | ||
this.spyCallbacks[i](this.currentPositionY()); | ||
} | ||
}, | ||
addStateHandler: function(handler){ | ||
spySetState.push(handler); | ||
this.spySetState.push(handler); | ||
}, | ||
addSpyHandler: function(handler){ | ||
spyCallbacks.push(handler); | ||
this.spyCallbacks.push(handler); | ||
}, | ||
updateStates: function(){ | ||
var length = this.spySetState.length; | ||
var length = spySetState.length; | ||
for(var i = 0; i < length; i++) { | ||
this.spySetState[i](); | ||
} | ||
}, | ||
unmount: function () { | ||
this.spyCallbacks = []; | ||
this.spySetState = []; | ||
for(var i = 0; i < length; i = i + 1) { | ||
spySetState[i](); | ||
} | ||
document.removeEventListener('scroll', this.scrollHandler); | ||
} | ||
}; | ||
} | ||
module.exports = scrollSpy; |
@@ -44,3 +44,3 @@ var animateScroll = require('./animate-scroll'); | ||
var cordinates = target.getBoundingClientRect(); | ||
var coordinates = target.getBoundingClientRect(); | ||
@@ -52,3 +52,5 @@ /* | ||
if(!animate) { | ||
window.scrollTo(0, cordinates.top + (offset || 0)); | ||
var bodyRect = document.body.getBoundingClientRect(); | ||
var scrollOffset = coordinates.top - bodyRect.top; | ||
window.scrollTo(0, scrollOffset + (offset || 0)); | ||
return; | ||
@@ -65,3 +67,3 @@ } | ||
animateScroll.animateTopScroll(cordinates.top + (offset || 0), options); | ||
animateScroll.animateTopScroll(coordinates.top + (offset || 0), options); | ||
@@ -68,0 +70,0 @@ } |
{ | ||
"name": "react-scroll", | ||
"version": "0.21.0", | ||
"version": "0.22.0", | ||
"description": "A scroll component for React.js", | ||
@@ -5,0 +5,0 @@ "main": "lib", |
11890
318