窗口滚动(),scrollTo(),scrollBy()无法在弹出窗口中工作

时间:2013-10-03 20:58:57

标签: javascript jquery

我正在创建一个新的弹出窗口,我想将页面滚动到特定位置,我尝试了我提到的所有三种方法,但没有一种方法不起作用。

这是我的代码

var w = window.open('','TEST','width='+divWidth+',height='+divHeight+'');

w.onload = function() {
    w.scroll(200,300);
};

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

Gokhan,我的猜测是窗口的主体是空的,产生的内容大小为零。由于内容的尺寸为0宽度乘以0高度,因此窗口的视口有效地折叠为空 - 基本上将scroll()(和朋友)减少为noop。

我把一个小小的小提琴放在一起,证明有足够的内容可以解决这个问题。 http://jsfiddle.net/blangenfeld/SXnA8/3/

更新的答案

好的,现在我知道您正在使用同源页面中的内容创建弹出窗口。尝试为弹出窗口定义onload并在那里滚动。

<html>
    <head>
        <style type="text/css">
            /* Just ensuring the size of the content for demo purposes */
            body { min-width: 500px; min-height: 500px;}
        </style>
        <script type="text/javascript">
            function showPopUp(url) {
                popUp = window.open(url, "_blank", "width=300, height=300");
                /* This works if the URL doesn't violate same-origin policy */
                popUp.onload = function() {
                    this.scrollTo(100, 100);
                }
            };
        </script>
    </head>
    <body>
        <h1>Pop-up test</h1>

        <a href="#" target="_blank" onclick="showPopUp('popup.html');">
            Click for a same-domain pop-up
        </a>
        &nbsp;|&nbsp;
        <a href="#" target="_blank" onclick="showPopUp('http://www.google.com');">
            Click for a google.com pop-up
        </a>
    </body>
</html>
相关问题