asp.net IFrame滚动条推到顶部..

leo*_*ora 2 html javascript asp.net iframe scroll

这是一个棘手的问题..

我有一个网页(称为PageA),它有一个标题,然后只包含一个iframe.让我们在iframe PageB中调用页面.PageB只是有一堆缩略图,但有很多,所以你必须向下滚动PageA才能查看它们.

当我向下滚动到页面B的底部并单击缩略图时,它看起来像是一个空白页面.实际发生的是它会调出图像,但由于只是图像的页面高度要短得多,因此滚动条会停留在同一位置而不会对其进行调整.我必须向上滚动到页面顶部才能查看图片.

无论如何当我点击iframe内页面上的链接时,外页滚动条会回到顶部

哎呀

tar*_*ate 6

在尝试各种方法后@mek,我发现的最佳解决方案是:

在外部页面中,定义一个滚动函数:

<script type="text/javascript">
  function gotop() {
    scroll(0,0);
  } 
</script>
Run Code Online (Sandbox Code Playgroud)

然后在定义iframe时,设置一个onload处理程序(每次加载iframe源时都会触发,即每当你导航到iframe中的新页面时)

<iframe id="myframe" 
    onload="try { gotop() } catch (e) {}" 
    src="http://yourframesource"
    width="100%" height="999"
    scrolling="auto" marginwidth="0" marginheight="0" 
    frameborder="0" vspace="0" hspace="0" >
</iframe>
Run Code Online (Sandbox Code Playgroud)

这种方法的好处是它意味着您不需要对iframe中包含的页面进行任何更改(iframe内容可以很乐意地在另一个域中 - 没有跨站点脚本问题).