簡體   English   中英

如何使用jQuery使用滾動到頂部功能

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

鼠標滾動到特定位置時如何產生效果?

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

jQuery的

var target = $('#target');

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

您可能需要為滾動功能選擇整個HTML文檔,而不僅僅是一個特定的div

jQuery的

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

您需要將代碼放入$(window).on('scroll', function(){}); 每次像這樣滾動窗口時都會觸發:

    $(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