diff --git a/src/index.js b/src/index.js index 12ebeb5..d6c83e8 100644 --- a/src/index.js +++ b/src/index.js @@ -75,16 +75,7 @@ const Sortable = createReactClass({ }, componentDidUpdate() { - const container = ReactDOM.findDOMNode(this); - const rect = container.getBoundingClientRect(); - - const scrollTop = (doc.docElement && doc.docElement.scrollTop) || doc.body.scrollTop; - const scrollLeft = (doc.docElement && doc.docElement.scrollLeft) || doc.body.scrollLeft; - - this._top = rect.top + scrollTop; - this._left = rect.left + scrollLeft; - this._bottom = this._top + rect.height; - this._right = this._left + rect.width; + this.calculateContainer(); }, componentWillReceiveProps(nextProps) { @@ -170,6 +161,8 @@ const Sortable = createReactClass({ this._isReadyForDragging = true; this._hasInitDragging = false; + // calculate container current position + this.calculateContainer(); // start listening mousemove and mouseup this.bindEvent(); }, @@ -334,6 +327,20 @@ const Sortable = createReactClass({ return arr; }, + calculateContainer() { + const container = ReactDOM.findDOMNode(this); + if (!container) return; + const rect = container.getBoundingClientRect(); + + const scrollTop = (doc.docElement && doc.docElement.scrollTop) || doc.body.scrollTop; + const scrollLeft = (doc.docElement && doc.docElement.scrollLeft) || doc.body.scrollLeft; + + this._top = rect.top + scrollTop; + this._left = rect.left + scrollLeft; + this._bottom = this._top + rect.height; + this._right = this._left + rect.width; + }, + /** * calculate new offset * @param {object} e MouseMove event