mko*_*yak 19 iframe jquery dimensions
当window是iframe时,jQuery的scrollTop返回null.有没有人能够弄清楚如何获得iframe的scrollTop?
更多信息:
我的脚本在iframe本身运行,父窗口在另一个域上,所以我无法访问iframe的ID或类似的东西
Chr*_*cob 24
我在尝试在iframe中设置scrollTop时发现了这个问题...不完全是你的问题(你想要获取)但是这里的iframes解决方案对于那些也最终在这里试图设置的人来说.
如果您想滚动到页面顶部,这将无法在iframe中使用:
$("html,body").scrollTop(0);
Run Code Online (Sandbox Code Playgroud)
但是,这将有效:
document.getElementById("wrapper").scrollIntoView();
Run Code Online (Sandbox Code Playgroud)
或者与jQuery等效:
$("#wrapper")[0].scrollIntoView();
Run Code Online (Sandbox Code Playgroud)
对于此标记(包含在iframe中):
<html>
<body>
<div id="wrapper">
<!-- lots of content -->
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
Dou*_*ner 11
您可以scrollTop使用此设置进行设置:
$("html,body").scrollTop(25);
Run Code Online (Sandbox Code Playgroud)
所以你可以试试这样:
$("html,body").scrollTop();
Run Code Online (Sandbox Code Playgroud)
因为不同的浏览器设置了scrollTop不同的元素(body或html).
来自scrollTo插件:
但在某些浏览器中,这可能仍会失败.以下是Ariel Flesher的jQuery scrollTo插件源代码的相关部分:
// Hack, hack, hack :)
// Returns the real elements to scroll (supports window/iframes, documents and regular nodes)
$.fn._scrollable = function(){
return this.map(function(){
var elem = this,
isWin = !elem.nodeName || $.inArray( elem.nodeName.toLowerCase(), ['iframe','#document','html','body'] ) != -1;
if( ! isWin ) {
return elem;
}
var doc = (elem.contentWindow || elem).document || elem.ownerDocument || elem;
return $.browser.safari || doc.compatMode == 'BackCompat' ?
doc.body :
doc.documentElement;
});
};
Run Code Online (Sandbox Code Playgroud)
然后你可以运行:
$(window)._scrollable().scrollTop();
Run Code Online (Sandbox Code Playgroud)
确定iframe向下滚动的距离.
小智 5
好旧的javascript:
scrollTop = document.getElementById(IFRAME_ID).contentWindow.document.body.scrollTop;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
59774 次 |
| 最近记录: |