手機網頁,div內滾動條,以及div內部滾動條拉到底部之后觸發事件


var gao = document.documentElement.clientHeight;
        var headHeight = parseInt($('.yhead').css('height'))
        $('.ybody').css('height', gao - headHeight + 'px')
        //前端分頁
        $(".yright").scroll(function(){
            var scrollHeight  = document.querySelector(".yright").scrollHeight;  // 沒用滾動條的情況下,元素內容的總高度
            var scrollTop     = document.querySelector(".yright").scrollTop;
            console.log(gao,scrollHeight,scrollTop)
            if((scrollTop + gao - scrollHeight) == headHeight){
                $('.fenye').click()
            }
            console.log("沒到底: ", );
        })
            

    div布局就不用講了,主要是滾動條事件這一塊是真暈,上網查資料查到的情況,用了一下真的可以,真是太高興了

以后有時間了在好好研究研究,暫時就先這樣,另附幾個查閱的資料:

javascript、jquery獲取網頁的高度和寬度

 

javascript:

可視區域寬 :document.documentElement.clientWidth  (width + padding)

可視區域高 :document.documentElement.clientHeight (height + padding)

可視區域寬: document.body.offsetWidth (包括邊線的寬: width + padding + border)

可視區域高: document.body.offsetHeight (包括邊線的高:height + padding + border)

內容高 : document.body.scrollHeight

文檔高 : document.body.offsetHeight

縱向滾動的距離 : document.body.scrollTop || document.documentElement.scrollTop

橫向滾動的距離 : document.body.scrollLeft || document.documentElement.scrollLeft

jquery:

可視區域寬  : $(window).width()  

可視區域高 :$(window).height()

頁面的文檔寬 :$(document).width();

頁面的文檔高 :$(document).height();

獲取滾動條到左邊的垂直寬度 :$(document).scrollLeft();

獲取滾動條到頂部的垂直高度 :$(document).scrollTop();

div設置overflow-y:scroll后, 如何監聽滾動到底部的事件

大多數情況下,我們都是判斷頁面滾動到底部,如下:

window.addEventListener('scroll', this.handleScroll);
// 處理滾動到底部的事件
handleScroll(arg) {
    var clientHeight  = document.documentElement.clientHeight;  // 客戶區大小
    var scrollHeight  = document.documentElement.scrollHeight;  // 沒用滾動條的情況下,元素內容的總高度
    var scrollTop     = document.documentElement.scrollTop;     // 被隱藏在內容區域上方的像素數
    if(scrollTop + clientHeight == scrollHeight && this.loading!=true && this.nomore==false){
    console.log("到底了...");
    this.getComments();
    return;
    }
    console.log("沒到底: ", clientHeight, scrollHeight, scrollTop);
}

在移動端,有時候會有這樣的需求,需要一個彈出層,高度和寬度都是100%,然后設置overflow-y:scroll。 

document.querySelector(".item").addEventListener('scroll', this.handleScroll2);
handleScroll2(arg) {
    var clientHeight  = document.documentElement.clientHeight;         // 客戶區大小
    var scrollHeight  = document.querySelector(".item").scrollHeight;  // 沒用滾動條的情況下,元素內容的總高度
    var scrollTop     = document.querySelector(".item").scrollTop;     // 被隱藏在內容區域上方的像素數
    console.log("1:: ", clientHeight, scrollHeight, scrollTop);

    if(scrollTop + clientHeight == scrollHeight && this.loading!=true && this.nomore==false){
    console.log("到底了...");
    // this.getComments();
    return;
    }
    console.log("沒到底: ", );
}

  

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM