简体   繁体   中英

javascript - restoring scroll position does not happen

function getScrollTop()
{
    if(typeof(window.pageYOffset) === 'number')
    {
        // DOM compliant, IE9+
        window.scrollY = window.pageYOffset;
    }
    else
    {
        // IE6-8 workaround
        if(document.body && document.body.scrollTop)
        {
            // IE quirks mode
            window.scrollY = document.body.scrollTop;
        }
            else if(document.documentElement && document.documentElement.scrollTop)
        {
            // IE6+ standards compliant mode
            window.scrollY = document.documentElement.scrollTop;
        }
    }
}

function getScrollLeft()
{
    if(typeof(window.pageXOffset) === 'number')
    {
        // DOM compliant, IE9+
        window.scrollX = window.pageXOffset;
    }
    else
    {
        // IE6-8 workaround
        if(document.body && document.body.scrollLeft)
        {
            // IE quirks mode
            window.scrollX = document.body.scrollLeft;
        }
        else if(document.documentElement && document.documentElement.scrollLeft)
        {
            // IE6+ standards compliant mode
            window.scrollX = document.documentElement.scrollLeft;
        }
    }
}

 // Summed-Up
function imgButtonClick()
{
    getScrollTop();
    getScrollLeft();
    /*Some simple hidden code*/
    window.scroll(window.scrollX, window.scrollY);
}

The getScrollTop() and getScrollLeft() functions are obtained from Internet - in a legal way, but the simple code addition does not work ( scroll(window.scrollX, window.scrollY); ). Mozilla's Developer Tools' Web Console (in Firefox) does not show errors. However, once filled with constants in place of scrollX and scrollY global variables - it does the simplified functioning by scrolling the page. The rest of the code, which had not been shown in this post - functions as asked. Other global variables on the page are also performing as asked.

If you are trying to animate a scrolling effect, you will need some additional code to animate the scrolling of the page. Here are some example functions to set the scroll location instantaneously:

function scrollToElement(element, scrollToTop)
{
    var element = document.getElementById(element);
    var elLoc = element.getBoundingClientRect();
    var topCoord = elLoc.top + (scrollToTop ? window.innerHeight : 0);
    console.log("About to scroll to: " + topCoord + ":" + elLoc.left);
    window.scroll(elLoc.left, topCoord);
}

function scrollBackToTop()
{
    window.scroll(0,0);   
}

If you are looking to animate your scrolling effect, check out these:

Click here for a javascript solution .

Click here for a css solution .

You can also accomplish this using one line of code anywhere in your application or project using jQuery with no plugins:

$('html, body').animate(
{
    scrollTop: $("#target-element").offset().top
}, 1000);

Taken from here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM