回到顶部js

回到顶部功能Demo:


(function () {

function scroll() {
    var obj = document.getElementById("scrollto");
    if (!obj || obj == undefined || obj == null) {
        obj = document.createElement("div");
        obj.setAttribute("class", "goTop");
        obj.setAttribute("id", "scrollto");
        obj.setAttribute("style", "display:none;");
        var objImg = document.createElement("img");
        objImg.setAttribute("src", "../App_Image/Webpage/goTop.png");
        objImg.setAttribute("id", "goTop");
        objImg.setAttribute("onclick", "scrollTo(0,0);");
        obj.appendChild(objImg);
        document.body.appendChild(obj);
    }
    if (document.documentElement.scrollTop + document.body.scrollTop > 100) {
        document.getElementById("scrollto").style.display = "block";
    } else {
        document.getElementById("scrollto").style.display = "none";
    }
}
window.onscroll = scroll;

})();

你可能感兴趣的:(回到顶部js)