触发返回顶部iFrame中的功能

时间:2013-01-24 16:33:09

标签: javascript jquery html

我一直在寻找解决方案,并且无法弄明白。尝试了很多我见过但没有看到过的方法。

以下是我想做的事情:

我的主页面侧面有导航菜单,所有内容都加载到iFrame上。我不知道iFrame内容何时向下滚动以启用或禁用“返回顶部”按钮,并在单击按钮时将iFrame内容发送回顶部。

<script type="text/javascript">
    $(function() {
    $(window).scroll(function() {
        if($(this).scrollTop() != 0) {
            $('#toTop').fadeIn();   
        } else {
            $('#toTop').fadeOut();
        }
    });

    $('#toTop').click(function() {
        $('body,html').animate({scrollTop:0},800);
    }); 
    });
</script>

我找到了这段代码,但它可以在你当前的页面上运行。我已经尝试过document.parent,parent,trigger(),但没有看到任何工作。我可以将这些代码粘贴到将加载到iFrame上的所有页面上,但我想要的是在内容顶部的静态按钮菜单上放置返回顶部按钮,以便可以从任何位置看到它你发短信。

谢谢!

2 个答案:

答案 0 :(得分:0)

您是否尝试在iframe头中插入此脚本?顺便说一句,它的parent.document不是document.parent。

答案 1 :(得分:0)

$(window).scroll(function() {
  if($(window).scrollTop() + $(window).height() == $(document).height()) {
   button = document.getElementById('topButton'); 
   button.style.display = "block";
 }
});

function toTop(){
document.getElementById('iframeContainer').scrollTop = 0;
}

然后隐藏按钮。一旦到达底部,它应该改变要阻止的样式并使其可见。使用新显示的按钮调用toTop()函数。