Skip to content

Instantly share code, notes, and snippets.

@simonid
Created January 29, 2018 14:05
Show Gist options
  • Save simonid/e3fc2bd9b1ef77e0922c06729b487bf6 to your computer and use it in GitHub Desktop.
Save simonid/e3fc2bd9b1ef77e0922c06729b487bf6 to your computer and use it in GitHub Desktop.
JavaScript片段

JavaScript代码片段

区域滚动延迟加载图片

function show(){
    var showId = document.getElementById("target");
    var client = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
    var targetTop = showId.getBoundingClientRect().top;
    if(targetTop <= client){
        //图片加载
    }
}
window.onscroll = show();

判断网页滚动到底部和顶部

function scrollBottomOrTop(){
    var client = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
    var scrollTop = document.body.scrollTop;
    var wholeHeight = document.body.scrollHeight;
    if(client+scrollTop >= wholeHeight){
        alert("已经滚动到底部");
        //ajax加载
    }
    if(scrollTop==0){
        alert("已经滚动到顶部");
    }
}
window.onscroll = scrollBottomOrTop();

判断DIV块加载到底部或顶部

var divScroll = document.getElementById("target");
function DivScroll(){
    var wholeHeight = divScroll.scrollHeight;
    var scrollTop = divScroll.scrollTop;
    var divHeight = divScroll.clientHeight;
    if(scrollTop+divHeight >= wholeHeight){
        alert("DIV滚动到底部了");
    }
    if(scrollTop==0){
        alert("DIV滚动到顶部了");
    }
}
divScroll.onscroll = DivScroll;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment