Skip to content

Commit

Permalink
* [html5] fix bug of scroll to
Browse files Browse the repository at this point in the history
  • Loading branch information
MrRaindrop committed Aug 19, 2016
1 parent fd429ee commit c007c86
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
45 changes: 45 additions & 0 deletions html5/browser/extend/api/dom.js
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)
}
}
2 changes: 2 additions & 0 deletions html5/browser/extend/api/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import dom from './dom'
import event from './event'
import pageInfo from './pageInfo'
import stream from './stream'
Expand All @@ -11,6 +12,7 @@ import clipboard from './clipboard'

export default {
init: function (Weex) {
Weex.install(dom)
Weex.install(event)
Weex.install(pageInfo)
Weex.install(stream)
Expand Down

0 comments on commit c007c86

Please sign in to comment.