简体   繁体   English

如何使用jQuery使用滚动到顶部功能

[英]How to use scroll to top function using jquery

How can I make an effect when mouse scrolls to a particular position? 鼠标滚动到特定位置时如何产生效果?

<div id="target">
  <!-- some data here -->
</div>

jQuery jQuery的

var target = $('#target');

if(target.scrollTop() > 10){
  alert('');
}

You might have to select the entire HTML document for the scroll function, instead of just one specific div 您可能需要为滚动功能选择整个HTML文档,而不仅仅是一个特定的div

jQuery jQuery的

$(document).scroll(function(){  
    if ($(window).scrollTop() > 10){
      // if the current scroll of the window is greater than 10px
    alert('');
}
});

You need to put your code inside $(window).on('scroll', function(){}); 您需要将代码放入$(window).on('scroll', function(){}); which will fire each time the window is scrolled like this: 每次像这样滚动窗口时都会触发:

    $(window).on('scroll', function(){
    var target = $('#target');
    if(target.scrollTop() > 10){
    console.log("Scrolled 10px");
    alert('');
       }
    });

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM