js、jq实现滚动监听页面到顶部的距离

  • Post author:
  • Post category:其他


js:

window.onscroll = function () {
    //获取距离页面顶部的距离
    let toTop = document.documentElement.scrollTop || document.body.scrollTop;
    console.log(toTop);
    //div距离顶部的距离
    console.log(document.getElementById('test').offsetTop);
}

jq:

$(window).scroll(function() {
    //获取距离页面顶部的距离
    let toTop = document.documentElement.scrollTop || document.body.scrollTop;
    console.log(toTop);
    //div距离顶部的距离
    $("#test").offset().top;
})



版权声明:本文为t595981879原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。