代码之家  ›  专栏  ›  技术社区  ›  Jamie Sexton

在页面加载时强制内部iframe内容滚动到顶部

  •  1
  • Jamie Sexton  · 技术社区  · 7 年前

    我网站的所有“内容页”都在我的索引页面的iframe内打开。

      <iframe id="iframe_A" name="iframe_A" onload="scroll(0,0);" src="Page1.html"></iframe>
    

    到目前为止,一切顺利。

    然而,在我的网站中,我也有一个“子网站”(它也打开了iframe\u a的内部),但它有自己的iframe[iframe\ u B]。

    为了更好地说明我的问题,我加入了一个非常精简的网站模型(6页,包括“索引页”),以表示我使用的布局。

    索引页:

    <!DOCTYPE html>
    <html>
    <head>
    <style>
    body{margin:0;}
    .header{width:100%; height:60px; background-color:blue;}
    .header a{padding-left:20px; vertical-align:middle; line-height:60px; font-family:Arial; font-size:12pt; color:#ffffff;}
    #iframe_A{width:100%; height:1700px;}
    .footer{width:100%; height:60px; background-color:blue; margin-bottom:20px;}
    .footer a{padding-left:20px; vertical-align:middle; line-height:60px; font-family:Arial; font-size:12pt; color:#ffffff;}
    </style>
    </head>
    <body>
    
    <div class="header"><a>Index Header&nbsp;&nbsp;</a>
    <a href="Page1.html" target="iframe_A">Page 1</a>&nbsp;&nbsp;
    <a href="Page2.html" target="iframe_A">Page 2</a>&nbsp;&nbsp;
    <a href="Page3.html" target="iframe_A">Page 3</a>
    </div>
    
    <iframe id="iframe_A" name="iframe_A" src="Page1.html" scrolling="no" onload="scroll(0,0);"></iframe>
    
    <div class="footer"><a>Index Footer&nbsp;&nbsp;</a>
    <a href="Page1.html" target="iframe_A">Page 1</a>&nbsp;&nbsp;
    <a href="Page2.html" target="iframe_A">Page 2</a>&nbsp;&nbsp;
    <a href="Page3.html" target="iframe_A">Page 3</a>
    </div>
    
    </body>
    </html>
    

    第1页

    <!DOCTYPE html>
    <html>
    <head>
    <style>
    body{margin:0px 0px 0px 20px;}
    .font{font-family:Arial, Helvetica, sans-serif; font-size:12pt;}
    .top{margin-top:10px;}
    .bottom{margin-top:1640px;}
    </style>
    </head>
    <body>
    <div class="top font">This is Page 1 in iframe_A (Top).</div>
    <div class="bottom font">This is Page 1 in iframe_A (Bottom).</div>
    </body>
    </html>
    

    <!DOCTYPE html>
    <html>
    <head>
    <style>
    body{margin:0px 0px 0px 20px;}
    .font{font-family:Arial, Helvetica, sans-serif; font-size:12pt;}
    .top{margin-top:10px;}
    .bottom{margin-top:1640px;}
    </style>
    </head>
    <body>
    <div class="top font">This is Page 2 in iframe_A (Top).</div>
    <div class="bottom font">This is Page 2 in iframe_A (Bottom).</div>
    </body>
    </html>
    

    第3页

    <!DOCTYPE html>
    <html>
    <head>
    <style>
    body{margin:0px;}
    .header{width:100%; height:60px; background-color:red;}
    .header a{padding-left:20px; vertical-align:middle; line-height:60px; font-family:Arial; font-size:12pt; color:#ffffff;}
    #iframe_B{width:100%; height:1580px; }
    .footer{width:100%; height:60px; background-color:red;}
    .footer a{padding-left:20px; vertical-align:middle; line-height:60px; font-family:Arial; font-size:12pt; color:#ffffff;}
    </style>
    </head>
    <body>
    <div class="header"><a>Page 3 (Subsite) Header</a>
    <a href="Page4.html" target="iframe_B">Page 4</a>
    <a href="Page5.html" target="iframe_B">Page 5</a>
    </div>
    <iframe id="iframe_B" name="iframe_B" src="Page4.html" scrolling="no">
    </iframe>
    <div class="footer"><a>Page 3 (Subsite) Footer&nbsp;&nbsp;</a>
    <a href="Page4.html" target="iframe_B">Page 4</a>
    <a href="Page5.html" target="iframe_B">Page 5</a>
    </div>
    </body>
    </html>
    

    第4页

    <!DOCTYPE html>
    <html>
    <head>
    <style>
    body{margin:0px 0px 0px 20px;}
    .font{font-family:Arial, Helvetica, sans-serif; font-size:12pt;}
    .top{margin-top:10px;}
    .bottom{margin-top:1520px;}
    </style>
    </head>
    <body>
    <div class="top font">This is Page 4, in iframe_B (Top).</div>
    <div class="bottom font">This is Page 4, in iframe_B (Bottom).</div>
    </body>
    </html>
    

    <!DOCTYPE html>
    <html>
    <head>
    <style>
    body{margin:0px 0px 0px 20px;}
    .font{font-family:Arial, Helvetica, sans-serif; font-size:12pt;}
    .top{margin-top:10px;}
    .bottom{margin-top:1520px;}
    </style>
    </head>
    <body>
    <div class="top font">This is Page 5, in iframe_B (Top).</div>
    <div class="bottom font">This is Page 5, in iframe_B (Bottom).</div>
    </body>
    </html>
    

    在这个模型中……无论您在上一页的页面位置是什么,iframe\u A中的第1、2和3页总是在页面顶部打开……这是应该的。

    我试图让内容页4和5(在第3页的“iframe\u B”中打开)每次也在顶部打开。

    1 回复  |  直到 7 年前
        1
  •  2
  •   Amit Shah    7 年前

    空载 您的 html标记:

    <iframe id="iframe_B" name="iframe_B" src="Page4.html" scrolling="no" onload="window.top.scrollTo(0,0);">
    

    onload="window.top.scrollTo(0,0);" //Refers to the top main window (page)
    onload="window.parent.scrollTo(0,0);" //Refers to the immediate parent window (page)
    onload="window.parent.parent.scrollTo(0,0);" //Refers to the parent of parent window (page)
    

    我刚刚试了一下,前两个加载事件都很有魅力。