簡體   English   中英

如何捕捉滾動事件?

[英]How to capture scroll event?

我想實現無限滾動。 以下是我的布局的簡短形式。 由於我有一些相對定位的元素,因此javascript滾動事件不會觸發。

我怎樣才能解決這個問題,以便觸發滾動事件並實現無限滾動?

我的主要布局是:

<div id="container">
    <div class="wrapper">
        <div id="header">
        ...
        </div> <%-- header --%>

        <div id="main">
        ...
        </div>

    </div> <%-- wrapper --%>
</div> <%-- container --%>
<div id="footer">
</div>

我的CSS是:

#container {
    position: absolute;
    z-index: 1;
    top: 0;
    bottom: 35px;
    left: 0;
    right: 0;
    overflow-y: auto;      
    overflow-x: hidden;      
}

.wrapper {
    margin: 0 auto;
    width: 960px;
    position: relative;
}   

#header {
    position: relative;
}

#main {
}

#footer {
    z-index: 2;
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    height: 35px;
} 

我需要更改什么才能使用我的布局接收瀏覽器滾動事件以實現無限滾動?

實現它的正確方法是:

 <div id="container" onScroll="handleOnScroll();">

<script>
function handleOnScroll() {
        alert("scroll");
    };
</script>

編輯:因為你用標記你的問題...


使用jQuery捕獲scroll事件...

HTML:

<div id="container">
    CONTENT
</div> 

jQuery的:

$(document).ready(function() {

    $('#container').scroll(function() {
        alert('scroll');
        // presumably your infinite scrolling code here
    });

});

請參閱: http//api.jquery.com/scroll/

這是我在我的代碼中使用的...

 <div id="DataDiv" style="overflow: auto; width: 280px; height:400px; margin-top: 10px;"
                    onscroll="Onscrollfnction();">
      my content here 
 </div>

功能如下

function Onscrollfnction() {
            var div = document.getElementById('DataDiv');
            div.scrollLeft;
            return false;
        };

內容越過400px后,滾動將開始並將是無限的...享受

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM