Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用javascript水平滚动iframe_Javascript_Jquery_Iframe - Fatal编程技术网

使用javascript水平滚动iframe

使用javascript水平滚动iframe,javascript,jquery,iframe,Javascript,Jquery,Iframe,我有一个iframe,我想用一些参数水平和垂直滚动。看起来我可以毫无问题地垂直滚动页面,但我不能水平滚动 以下是一些代码示例: HTML: <iframe id="html-region" src="some local url" style="width:1000px;height:1000px;overflow:scroll" scrolling="yes"></iframe> 提前谢谢你 你就快到了: $("#html-region").contents().fi

我有一个iframe,我想用一些参数水平和垂直滚动。看起来我可以毫无问题地垂直滚动页面,但我不能水平滚动

以下是一些代码示例:

HTML:

<iframe id="html-region" src="some local url" style="width:1000px;height:1000px;overflow:scroll" scrolling="yes"></iframe>
提前谢谢你

你就快到了:

$("#html-region").contents().find("body").scrollLeft(200);
需要记住的几件事:

  • 跨来源:如果iframe的内容html与承载iframe的页面位于同一来源,则只能访问iframe的内容html:查看
    由于您的url是“某个本地url”,因此不会有任何问题。

  • 您需要等待iframe的内容完全加载,然后才能滚动它。我通常只是等待整个窗口的内容被
    $(window.load(function(){
    块)完全加载

  • 与其获取所有iframe的列表并选择列表中的第一个,不如通过iframe的id对其进行寻址。您不知道将来会发生什么,您可能会在该iframe之前添加另一个iframe,您可能会被迫更改代码。最好避免这种情况

综上所述,下面是一个工作示例:

//block that will be executed once the iframe's content is fully loaded
$(window).load(function(){
    //get the body element inside of the iframe
    var $myFrameContent = $("#html-region").contents().find("body");
    //adjust the scrollLeft/Top
    $myFrameContent.scrollTop(200);
    $myFrameContent.scrollLeft(50);
});

这里有一个

谢谢你的完整回答!遗憾的是,它似乎对我不起作用。当我直接从控制台设置scrollTop值时,它起作用,但scrollLeft不起作用。此外,我不能在你的小提琴中滚动?如果可以帮助的话,我正在使用Chrome 37(开发频道).hmm…我使用的是chrome 35 normal,fiddle没有滚动条,因为为了不受跨源限制地访问其内容,我在其中加载了一个JSFIDLE,它删除了滚动条。但是,您可以使用鼠标中键滚动,只需单击任意位置并拖动。关于ScrollLeft不工作,我遇到当我尝试直接滚动iframe内容而不是其中的
正文
元素时,您是否确定已将scrollLeft应用于正文元素?是的,就是这样!我尝试滚动iframe,但它不起作用,但滚动正文确实起作用。非常感谢!
//block that will be executed once the iframe's content is fully loaded
$(window).load(function(){
    //get the body element inside of the iframe
    var $myFrameContent = $("#html-region").contents().find("body");
    //adjust the scrollLeft/Top
    $myFrameContent.scrollTop(200);
    $myFrameContent.scrollLeft(50);
});