Javascript维护滚动位置

时间:2015-07-06 18:13:26

标签: javascript jquery

我尝试使用此代码刷新网页并保持其滚动位置:

function refreshPage() {
                var page_y = document.getElementsByTagName("body")[0].scrollTop;
                window.location.href = window.location.href.split('?')[0] + '?page_y=' + page_y;
            }
            window.onload = function() {
                setTimeout(refreshPage, 35000);
                if ( window.location.href.indexOf('page_y') != -1 ) {
                    var match = window.location.href.split('?')[1].split("&")[0].split("=");
                    document.getElementsByTagName("body")[0].scrollTop = match[1];
                }
            }

虽然这成功地添加了?page_y = scrollposition并且滚动位置准确,并且我可以成功地将匹配和匹配[1]打印到控制台,但唯一的问题是它不会滚动页面。

修改

显然,脚本在我的脚本之前加载以生成网页的内容,我不太清楚为什么。在下面发布完整的代码:

    <script>
        $(window).load(function(){ 
            $.getJSON("sun.json", function(json1) {
                $.each(json1, function(key, data) {
                    document.body.innerHTML += 
                                                                    "<div id='" + data.video + "' class='caption' data-source='" + data.video + "' data-title='" + data.title + "' data-desc='" + data.description + "' onclick='parent.changeVideo(dataset.source, dataset.title, dataset.desc); reloadImg()'>" +
                                                                        "<img class='thumbnail' src='" + data.thumb + "' alt='" + data.title + "'>" +
                                                                        "<div class='caption-text'>" +
                                                                            "<b class='caption-title'>" + data.title + "</b>" +
                                                                            data.description +
                                                                        "</div>" +
                                                                    "</div>" +
                                                                    "<hr>"
                    console.log("This should be first");
                    $(".caption").hover(function(){
                        $(this).find(".caption-text").fadeIn(400);
                    }, 
                    function(){
                        $(this).find(".caption-text").fadeOut(400);
                    });
                });
            });
        });

        var scroll = $(window).scrollTop();
        // yada
        $("html").scrollTop(scroll);

        function changeVid() {
            document.querySelector("#current-video_html5_api").src = data.video
            console.log(data.video);
        }
    </script>

    <script>
        function refreshPage() {
            var page_y = document.getElementsByTagName("body")[0].scrollTop;
            window.location.href = window.location.href.split('?')[0] + '?page_y=' + page_y;
        }
        var match = window.location.href.split('?')[1].split("&")[0].split("=");

       window.onload = function() {
            setTimeout(refreshPage, 35000);
            if ( window.location.href.indexOf('page_y') != -1 ) {

                //document.getElementsByTagName("body")[0].scrollTop = match[1];
                window.scrollTo(0, match[1]);
                console.log(match[1]);
                console.log("This should come second");
            }
        }
    </script>

2 个答案:

答案 0 :(得分:1)

您可以使用window.scrollTo:

function refreshPage() {
            var page_y = document.getElementsByTagName("body")[0].scrollTop;
            window.location.href = window.location.href.split('?')[0] + '?page_y=' + page_y;
        }
        window.onload = function() {
            setTimeout(refreshPage, 35000);
            if ( window.location.href.indexOf('page_y') != -1 ) {
                var match = window.location.href.split('?')[1].split("&")[0].split("=");
                window.scrollTo(0, match[1]);
            }
        }

答案 1 :(得分:0)

您需要将scrollTo用作函数 - 而不是属性。我相信scrollTop(作为一个函数)仅在库中可用(如jquery)。这里有一些关于如何使用window.scrollto()的文档...... https://developer.mozilla.org/en-US/docs/Web/API/Window/scrollTo