例子:当video滚动到可视区时自动播放
监听窗口滚动事件
记得在恰当的地方清除事件监听
mounted () {
window.addEventListener('scroll', this.handleScroll)
},
destroyed () {
window.removeEventListener('scroll', this.handleScroll)
},
处理函数
methods: {
handleScroll () {
var el_topHeight = this.$refs.one_video.offsetTop
var el_Height = this.$refs.one_video.clientHeight
var clientHeight = document.documentElement.clientHeight
var scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop
if (clientHeight + scrollTop > el_topHeight && this.one_video_num === 0) {
this.one_video_num += 1
this.$refs.one_video.play()
}
if (clientHeight + scrollTop < el_topHeight || scrollTop > el_topHeight + el_Height) {
this.one_video_num = 0
}
},
},
|