Native js gets and sets the vertical scroll height of the page

Recently, I want to use native js to achieve some interactive effects. The following code is a method to be used for returning to the top button: get and set the vertical scrolling height of the page. If you use jq, you can easily achieve it. If you use native js, you need to think more about the browser. compatibility:

/**
     * Get && set - page vertical scroll value
     * */
    function __getPageScrollY(top) {

        if (top || Number(top) == 0) { //Set the vertical scroll value
            if (self.pageYOffset) {
                self.pageYOffset = Number(top);
            }
            if (document.documentElement && document.documentElement.scrollTop) { // Explorer 6 Strict
                document.documentElement.scrollTop = Number(top);
            }
            if (document.body) {// all other Explorers
                document.body.scrollTop = Number(top);
            }
            return true;
        } else { //Get the vertical scroll value
            var yScroll;
            if (self.pageYOffset) {
                yScroll = self.pageYOffset;
            } else if (document.documentElement && document.documentElement.scrollTop) { // Explorer 6 Strict
                yScroll = document.documentElement.scrollTop;
            } else if (document.body) {// all other Explorers
                yScroll = document.body.scrollTop;
            }
            return yScroll;
        }

    };

transfer:

var scrollHeight = __getPageScrollY();//Get the height
    __getPageScrollY(1000);//Set the height


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325783331&siteId=291194637