简体   繁体   English

关闭弹出窗口后将页面返回到原始 scrollTop 位置

[英]Return page to original scrollTop position after closing popup

I have a button which fixes the position of the page content then pushes it left while a form slides in from the right.我有一个按钮,用于固定页面内容的位置,然后将其向左推,同时表单从右侧滑入。

This is working fine except for one thing.这工作正常,除了一件事。 When the user has scrolled down the page slightly and clicks on the button it successfully fixes the position.当用户稍微向下滚动页面并单击按钮时,它成功地固定了位置。 However when they then close this form, the page does not return to the original position... it takes the user back to the top of the page.但是,当他们关闭此表单时,页面不会返回到原始位置......它会将用户带回页面顶部。 I've posted the jquery below which might help to explain the problem better.我在下面发布了 jquery,它可能有助于更好地解释问题。

There's also a jsfiddle example here...这里还有一个 jsfiddle 示例...

http://jsfiddle.net/CMbBC/ http://jsfiddle.net/CMbBC/

    var pageContents;
    var currentscrollpos;
    $(document).ready(function() {
        $("a#testbutton").live('click', function() {
            var currentscrollpos = $(window).scrollTop();
            var pageContents = $("body").html();
            $("body").html("");
            $("<div class='pageContainer'>" + pageContents + "</div>").prependTo("body");
            $(".pageContainer").css({'position':'fixed','top':currentscrollpos*-1});
            $("html, body").animate({ scrollTop: 0 }, 0);
            $("<div class='blackout'></div>").appendTo("body");
            $(".blackout").css("opacity",0.8).fadeIn('slow', function() {
                $("<div class='popupPanel'><a class='closeme'>close</a></div>").appendTo("body");
                $(".popupPanel").animate({
                    right: "0px"
                }, 500, function() {
                    $(".popupPanel").css("position","absolute")
                });
                $(".pageContainer").animate({
                    left: "-200px"
                }, 500, function() {
                });
                $("a#testbutton").append(currentscrollpos)
            });
            return false;
        });

        $('.closeme').live('click', function() {
            var pageContents = $(".pageContainer").html();
            $(".popupPanel").css("position","fixed").animate({
                right: "-200px"
            }, 500, function() {

            });
            $(".pageContainer").animate({
                left: "0px"
            }, 500, function() {
                $(".blackout").fadeOut('slow', function() {
                    $(".blackout").remove();
                    $("body").html(pageContents);   
                    $("html, body").animate({ scrollTop: currentscrollpos }, 0);
                });
            });
        });
    });

You are using a local variable because you are using var :您使用的是局部变量,因为您使用的是var

var currentscrollpos = $(window).scrollTop();

Instead, drop the var so that it's accessible inside the .closeme click function.相反,删除var以便它可以在.closeme单击函数中访问。 You already used var at the very beginning.您一开始就已经使用了var So:所以:

currentscrollpos = $(window).scrollTop();

That way, the variable at the very beginning will be set, which can be accessed by both functions.这样,最开始的变量将被设置,这两个函数都可以访问。 Currently, you are declaring another variable inside the a#testbutton click function, leaving the original variable untouched.当前,您在a#testbutton单击函数中声明了另一个变量,而保持原始变量不变。

http://jsfiddle.net/pimvdb/CMbBC/2/ . http://jsfiddle.net/pimvdb/CMbBC/2/

You can use this code to return to the position of the page that is closed from the browser您可以使用此代码返回到从浏览器关闭的页面的位置

 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script> var currentposition; $(document).ready(function(){ $(window).scroll(function(){ var scrollPos = $(document).scrollTop(); console.log("scrollPos:"+scrollPos); localStorage.setItem("key",scrollPos); }); currentposition=localStorage.getItem("key"); window.scrollTo(0,currentposition); alert("currentposition:"+currentposition); }); </script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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