-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fd429ee
commit c007c86
Showing
2 changed files
with
47 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
'use strict' | ||
|
||
import scroll from 'scroll-to' | ||
|
||
const dom = { | ||
/** | ||
* scrollToElement | ||
* @param {string} ref | ||
* @param {obj} options {offset:Number} | ||
* ps: scroll-to has 'ease' and 'duration'(ms) as options. | ||
*/ | ||
scrollToElement: function (ref, options) { | ||
!options && (options = {}) | ||
const offset = (Number(options.offset) || 0) * this.scale | ||
const elem = this.getComponentManager().getComponent(ref) | ||
if (!elem) { | ||
return console.error(`[h5-render] component of ref ${ref} doesn\'t exist.`) | ||
} | ||
const parentScroller = elem.getParentScroller() | ||
if (parentScroller) { | ||
parentScroller.scroller.scrollToElement(elem.node, true, offset) | ||
} | ||
else { | ||
const offsetTop = elem.node.getBoundingClientRect().top | ||
+ document.body.scrollTop | ||
const tween = scroll(0, offsetTop + offset, options) | ||
tween.on('end', function () { | ||
console.log('scroll end.') | ||
}) | ||
} | ||
} | ||
} | ||
|
||
const meta = { | ||
dom: [{ | ||
name: 'scrollToElement', | ||
args: ['string', 'object'] | ||
}] | ||
} | ||
|
||
export default { | ||
init: function (Weex) { | ||
Weex.registerApiModule('dom', dom, meta) | ||
} | ||
} |
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