javascript - 滚动事件监听器在 iFrame 中的 iOS 上不起作用

标签 javascript jquery html ios iframe

我需要认真的帮助。 我使用滚动事件监听器编写了一个网站,根据用户滚动的深度为一些图形设置动画。

在桌面和 iOS 上运行良好。 唯一的问题是该站点需要嵌入到 iFrame 中。 仍然适用于桌面,但不适用于 iOS。

我举了一个例子来说明我的意思。

先在桌面上查看此页面,然后在 iOS 上查看。 http://www.webzeile.com/iframescroll/index.html

固定元素(Scroll o meter)在 iOS 中不固定,我没有机会在此处获取 scrollTop

主页(这里我没机会修改iframe属性)

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>index</title>
  <style type="text/css" media="screen">
html, body {
overflow-y:auto;
}
</style>
</head>
<body>

<div style="-webkit-overflow-scrolling: touch !important; overflow:auto; width:800px; height:500px; margin:0px auto;">

<iframe id="iframe" scrolling="auto" webkitallowfullscreen frameborder="0" style="width:100%; height:100%;" src="iframe.html"></iframe>

</div>

iframe 内容

    <!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8">
    <title>index</title>
    <script src="http://code.jquery.com/jquery-latest.min.js"></script>
    <style type="text/css" media="screen">
html, body {
overflow-y:auto;
margin:0px;
padding:0px;
}

    .div1, .div2 {
margin:0px;
padding:0px;

        height:700px;
    margin:0px;
        position:relative;
        width:100%;
    }
    .div1 {
        background:green;
    }
    .div2 {
        background:orange;
        top:100%;
    }

    #scrollindicator {
        color:#fff;
        position:fixed;
        right:20px;
        top:20px;
        z-index:1000;
        width:150px;
        height:25px;
    }
    </style>
</head>
<body>
<div class="div1">
    <h1>Content 1</h1>
</div>

    <div class="div2">
        <h1>Content 2</h1>
        <a href="#" onclick="alert($(window).scrollTop()); return false;">Alert scroll top</a>
        <a href="#" onclick="alert(window.pageYOffset); return false;">Alert Y offset</a>

    </div>


    <div id="scrollindicator">
       Scroll o meter: 0
    </div>

    <script type="text/javascript">
        $(window).scroll(function () {
            $("#scrollindicator").html("Scroll o meter: "+$(window).scrollTop()); 
        });
    </script>
</body>
</html>

什么都试过了。无法让滚动处理程序在 iOS 上工作。有什么想法吗?

最佳答案

我看到您最终放弃了 IFrame,但我今天在遇到类似问题时发现了这个问题。我试图检测用户何时从与主页不同的域的 IFrame 内部滚动到页面底部。这是我想出的解决方案:

var totalY = 0;

function scroll_handler(e){
    var t = $(window).scrollTop();
    var h = $(window).height();
    var el = $('#bottomdiv');  //This is an element at the bottom of the page
    var bot = el.offset().top + (el.height()) - 400; //I wanted a 400px buffer

    if (e.type == "touchend") {
        totalY += e.originalEvent.changedTouches[0].clientY;

        if ( !t ) {
            t = totalY;
        }

    }

    if ((t + h) >= bot) {
        //Do Stuff when they get to the bottom of the page
    }
}

if ( 'ontouchstart' in window ) {
    $("body").on("touchend",scroll_handler);
} else {
    $(window).scroll(scroll_handler);
}

它并不完美,但它可以工作 - 它在用户向下滑动页面时获取触摸事件的 Y 位置并将其添加到 totalY。如果用户快速滑动并且到达底部时根本没有触摸屏幕,则它会失败。但是,它适用于我的情况。

我希望这可以帮助其他正在寻找类似问题的人。

关于javascript - 滚动事件监听器在 iFrame 中的 iOS 上不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28135785/

相关文章:

css - 为什么我的图像重叠?

javascript - 设置文本/Val 在单击/聚焦之前不出现在文本输入中 (jQuery)

javascript - Node js : TypeError: Object #<Object> has no method 'existsSync'

javascript - 使用 JQuery 为标签赋值?

javascript - CSS 和 &lt;input type ='file' > - 讨论

html - 是否可以使用 Zend_Pdf 将 HTML 转换为 pdf?

javascript - 引用Javascript声明中的数组

javascript - 如何打破 vue.js 中的 v-for 循环?

jquery - 添加 JQuery 菜单

javascript - 尝试在 foreach 循环中将变量传递到对象的方法构造函数时出现范围问题