【发布时间】:2010-11-14 14:26:11
【问题描述】:
我使用 JavaScript 动态加载 iframe。加载后,如何使其向下滚动特定数量的像素(即,在 iframe 中的页面加载后,如何使 iframe 自行滚动到页面的指定区域?)
【问题讨论】:
标签: javascript iframe scroll
我使用 JavaScript 动态加载 iframe。加载后,如何使其向下滚动特定数量的像素(即,在 iframe 中的页面加载后,如何使 iframe 自行滚动到页面的指定区域?)
【问题讨论】:
标签: javascript iframe scroll
编写滚动时的主要问题与将整个文档嵌入到页面中有关,请记住,Iframe 将是主文档中的整页(头部和全部),因此,在实际滚动之前,您需要获取内部文档,而不仅仅是容器,因此您实际上可以滚动到。
我们添加了对sendure兼容性的验证,contentDocument和windows的区别可以在here找到
Havign 这个,最终的代码是:
var $iframe = document.getElementByID('myIfreme');
var childDocument = iframe.contentDocument ? iframe.contentDocument : iframe.contentWindow.document;
childDocument.documentElement.scrollTop = 0;
【讨论】:
受纳尔逊评论的启发,我做了这个。
关于在源自外部域的文档上使用 .ScrollTo( ) 的 javascript 同源策略的解决方法。
对此非常简单的解决方法是创建一个虚拟 HTML 页面,在其中托管外部网站,然后在加载后在该页面上调用 .ScrollTo(x,y)。那么你唯一需要做的就是有一个框架或一个 iframe 来打开这个网站。
还有很多其他方法可以做到这一点,这是迄今为止最简单的方法。
*注意高度必须很大才能容纳滚动条的最大值。
--home.html
<html>
<head>
<title>Home</title>
</head>
<frameset rows="*,170">
<frame src=body.htm noresize=yes frameborder=0 marginheight=0 marginwidth=0 scrolling="no">
<frame src="weather.htm" noresize=yes frameborder=0 marginheight=0 marginwidth=0 scrolling="no">
</frameset>
</html>
--weather.html
<html>
<head>
<title>Weather</title>
</head>
<body onLoad="window.scrollTo(0,170)">
<iframe id="iframe" src="http://forecast.weather.gov/MapClick.php?CityName=Las+Vegas&state=NV&site=VEF&textField1=36.175&textField2=-115.136&e=0" height=1000 width=100% frameborder=0 marginheight=0 marginwidth=0 scrolling=no>
</iframe>
</body>
</html>
【讨论】:
我在 iPad 上的 iframe 中使用任何类型的 javascript“scrollTo”函数时也遇到了麻烦。终于找到了解决问题的“旧”解决方案,只需哈希到锚点即可。
在我的情况下,在 ajax 返回后,我的错误消息被设置为显示在 iframe 的顶部,但如果用户以公认的长表单向下滚动,则提交将退出并且错误显示为“首屏” .此外,假设用户确实向下滚动,顶层页面从 0,0 滚动出去并且也被隐藏了。
我加了
<a name="ptop"></a>
到我的 iframe 文档的顶部和
<a name="atop"></a>
到我的顶级页面的顶部
然后
$(document).ready(function(){
$("form").bind("ajax:complete",
function() {
location.hash = "#";
top.location.hash = "#";
setTimeout('location.hash="#ptop"',150);
setTimeout('top.location.hash="#atop"',350);
}
)
});
在 iframe 中。
您必须在首页之前对 iframe 进行哈希处理,否则只有 iframe 会滚动,而顶部将保持隐藏状态,但由于超时间隔,它会有点“跳跃”。我想整个标签将允许各种“scrollTo”点。
【讨论】:
受Nelson's 和Chris' cmets 的启发,我找到了一种使用div 和iframe 解决同源策略的方法:
HTML:
<div id='div_iframe'><iframe id='frame' src='...'></iframe></div>
CSS:
#div_iframe {
border-style: inset;
border-color: grey;
overflow: scroll;
height: 500px;
width: 90%
}
#frame {
width: 100%;
height: 1000%; /* 10x the div height to embrace the whole page */
}
现在假设我想通过滚动到该位置来跳过 iframe 页面的前 438 个(垂直)像素。
JS 解决方案:
document.getElementById('div_iframe').scrollTop = 438
jQuery解决方案:
$('#div_iframe').scrollTop(438)
CSS 解决方案:
#frame { margin-top: -438px }
(单独使用每种解决方案就足够了,CSS 的效果略有不同,因为您无法向上滚动以查看 iframe 页面的顶部。)
【讨论】:
CSS
.amazon-rating {
width: 55px;
height: 12px;
overflow: hidden;
}
.rating-stars {
left: -18px;
top: -102px;
position: relative;
}
HAML
.amazon-rating
%iframe.rating-stars{src: $item->ratingURL, seamless: 'seamless', frameborder: 0, scrolling: 'no'}
【讨论】:
或者,您可以在 iframe 上设置一个 margin-top...有点 hack,但到目前为止在 FF 中有效。
#frame {
margin-top:200px;
}
【讨论】:
一个jQuery解决方案:
$("#frame1").ready( function() {
$("#frame1").contents().scrollTop( $("#frame1").contents().scrollTop() + 10 );
});
【讨论】:
【讨论】:
使用框架内容的scrollTop 属性将内容的垂直滚动偏移设置为特定数量的像素(例如100):
<iframe src="foo.html" onload="this.contentWindow.document.documentElement.scrollTop=100"></iframe>
【讨论】: