【问题标题】:Load All Of My Pages At Top在顶部加载我的所有页面
【发布时间】:2017-06-25 01:42:33
【问题描述】:

我有一个网站,我的所有内容页面都在 iframe 内打开。

我遇到的一个问题是,当访问者打开我网站上的第二页时,它会在与他们所在的上一页相同的“滚动位置”打开。

我希望我的所有页面都在顶部打开。

注意:我花了几天时间在这个网站和其他网站上搜索,试图找到一些有用的东西。到目前为止,我发现的解决方案都没有奏效。

所以...我决定发布我的问题和一个非常精简的 3 页示例,看看我是否可以获得一些急需的帮助。

感谢任何帮助或建议。

这是“索引”(父)页面:

    <!DOCTYPE html>
    <html>
    <head>

    <style>
    body{margin:0;}
    .header{width:100%; height:60px; background-color:blue;}
    .header a{vertical-align:middle; line-height:60px; font-family:Arial; font-size:12pt; color:#ffffff;}
    #myiframe{width:100%; height:2000px;}
    .footer{width:100%; height:60px; background-color:blue; margin-bottom:20px;}
    .footer a{vertical-align:middle; line-height:60px; font-family:Arial; font-size:12pt; color:#ffffff;}
    </style>

    </head>
    <body>

    <div class="header"><a>This is the Header of the index (parent) page.</a></div>

    <iframe id="myiframe" name="myiframe" src="1stPage.html"></iframe>

    <div class="footer"><a>Footer</a></div>

    </body>
    </html>

这是第一个“内容”页面(包括最后失败的滚动代码):

    <!DOCTYPE html>
    <html>
    <head>

    <style>
    body{margin:0;}
    .text{font-family:Arial; font-size:12pt;}
    .link{margin-top:1900px;}
    </style>

    <script>
    $(document).ready(function(){ 
    $('html, body').scrollTop(0); 
    $(window).on('load', function() {
    setTimeout(function(){ $('html, body').scrollTop(0);}, 0);
    });
    });
    </script>

    </head>
    <body>

    <div class="text">This is the 1st page in iframe (..and this is 'page postition' "Top" or "0").</div>
    <div class="text">Please scroll down to the link to 2nd page.</div>

    <div class="link"><a class="text" href="2ndPage.html" target="myiframe">Click here to go to 2nd Page</a></div><br>
    <div class="text">This is the 1st page in iframe (..and this is 'page postition' "Bottom" or "X").</div>

    </body>
    </html>

最后...第二个“内容”页面(还包括最后失败的滚动代码):

    <!DOCTYPE html>
    <html>
    <head>

    <style>
    body{margin:0;}
    .text{font-family:Arial; font-size:12pt;}
    .link{margin-top:1900px;}
    </style>

    <script>
    $(document).ready(function(){ 
    $('html, body').scrollTop(0); 
    $(window).on('load', function() {
    setTimeout(function(){ $('html, body').scrollTop(0);}, 0);
    });
    });
    </script>

    </head>
    <body>

    <div class="text">This is the 2nd page in iframe (..and this is 'page postition' "Top" or "0").</div>
    <div class="text">Please scroll down to the link to 2nd page.</div>

    <div class="link"><a class="text" href="1stPage.html" target="myiframe">Click here to go back to the 1st Page</a></div><br>
    <div class="text">This is the 2nd page in iframe (..and this is 'page postition' "Bottom" or "X").</div>

    </body>
    </html>

【问题讨论】:

    标签: javascript jquery html scroll scrolltop


    【解决方案1】:

    您可以在 iFrame onload 事件中执行此操作,如下所示:

    <iframe id="myiframe" name="myiframe" src="1stPage.html" onload="scroll(0,0);"></iframe>
    

    【讨论】:

    • 谢谢!这就像一个魅力!现在我只需要弄清楚当我在其他网站上共享指向我的页面的链接时,如何让这些页面与它们的父页面一起打开。
    猜你喜欢
    • 1970-01-01
    • 2019-09-24
    • 1970-01-01
    • 1970-01-01
    • 2022-11-01
    • 2020-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多