How do you scroll an iframe from within using jquery?

送分小仙女□ 提交于 2019-12-24 04:01:34

问题


I'm using a shadowbox which generates an iframe to display product details on a page. Because the details page can be pretty long, the client would like a "More" button that scrolls the page down (apparently the scrollbar on the right of the iframe isn't enough).

Here's the code that I've tried in order to get the iframe to scroll:

$(document).ready(function()
{
$(".moreButton img").click(function() {
    scrollbottom();
});
});

function scrollbottom() {
var x = 250; // this number is a temporary placeholder
var t = 500;
$("iframe").animate({ scrollTop: x }, t);
}

I've also tried using body instead of iframe but to no avail. Any ideas? Thanks!


回答1:


Like this: (Tested)

$("iframe").contents().children().animate({ scrollTop: x }, t);



回答2:


This ended up working:

$('html,body').animate({ scrollTop: x }, t);



回答3:


In Chrome I had to specifically select the body element:

$('#my-iframe').contents().find('body').animate({scrollTop:90},500);




回答4:


Create a function in the parent javascript like this:

function scrollToPoint(top) {
    $("html, body").animate({ scrollTop: top }, "slow")
}

and call that function within the iframe like this:

window.parent.scrollToPoint(top);

It should work in chrome, not tested in firefox yet



来源:https://stackoverflow.com/questions/2858731/how-do-you-scroll-an-iframe-from-within-using-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!