简体   繁体   English

如何从顶部滚动页面一定量的像素后触发事件? Javascript或Jquery

[英]How to trigger event after i scroll a page certain amount in pixels from the top? Javascript or Jquery

I am trying to trigger a video when I scroll a page 300px from the top. 当我从顶部滚动页面300px时,我试图触发视频。 This works if I scroll 如果我滚动,这是有效的

window.onscroll = function(event) {
myvideo.play();
}

but I would like it to play once I scroll 300px 但是一旦我滚动300px,我希望它能够播放

You can check $(document).scrollTop() 你可以检查$(document).scrollTop()

Try: 尝试:

 $(document).bind("scroll", function(){    
    if ($(document).scrollTop() >= 300) {
       myvideo.play();
    }
});

EDIT 编辑

Because We dont want the movie to play every time they scroll a pixel beyond 300 after the video is played you can unbind the event 因为我们不想在播放视频后每次将像素滚动超过300时播放电影,所以您可以取消绑定事件

$(document).bind("scroll.myScroll", function(){    
    if ($(document).scrollTop() >= 300) {
        myvideo.play();
        $(document).unbind('.myScroll');
    }
});

DEMO DEMO

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 jQuery滚动事件:如何确定以像素为单位滚动的数量(滚动增量)? - jQuery scroll event: how to determine amount scrolled (scroll delta) in pixels? 使用jQuery向下滚动到一定数量的像素 - Scroll down to a certain amount of pixels using jQuery 滚动某些像素后如何触发CSS动画 - how to trigger a css animation after certain pixels of scroll 滚动div一定数量的像素:如何滚动div而不引起滚动事件 - Scroll div for certain amount of pixels: how to scroll div without causing scroll event 在jQuery中的可拖动对象达到特定点后,如何使JavaScript事件触发? - How do I make a JavaScript event trigger after a draggable object in jQuery has reached a certain point? 经过一定时间后,如何停止刷新页面 - How can I stop a page from refreshing after a certain amount of time javascript 检查滚动位置在顶部后,经过一段时间后将页面滚动到特定高度 - Making a page scroll to a specific height after a certain amount of time, after checking scroll position is at top 如何使用 JavaScript/jQuery 滚动到页面顶部? - How to scroll to top of page with JavaScript/jQuery? 滚动具有固定像素量的页面,并通过滚轮进行操作 - Scroll page with a fixed amount of pixels AND doing it from the scroll wheel javascript | 如何在用户滚动一定量后触发事件 - javascript | how to trigger an event after the user has scrolled by some amount
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM