【问题标题】:iOS document.body.scrollTop always 0 in iframeiOS document.body.scrollTop 在 iframe 中始终为 0
【发布时间】:2017-01-12 02:04:43
【问题描述】:

iframe 页面内编写代码(不是其父级。代码必须在 iframe src 页面中)

正在尝试检测 scrollTop..

我尝试过document.body.scrollTop,它适用于所有其他浏览器。

在 i-things 中的值始终为零。 RIP 史蒂夫·乔布斯

如何获取 scrollTop 以在 ipad 中滚动 iframe?他们都以 i 开头,为什么苹果让它这么难?您认为他们会喜欢 i-frames 并尝试为这些 i-frames 打上商标吗?

【问题讨论】:

    标签: javascript ios iframe


    【解决方案1】:

    我找到的唯一解决方案是在 iframe 的父容器中使用 window.pageYOffset 并使用 postMessage 将其发送到 iframe。

    这是我的解决方案:

    滚动顶部的 iframe 请求->

    this.sendMessage({'type': "getMarginTop", 'source': selector});
    

    容器监听消息并使用 -> pageYOffset 进行响应

    window.addEventListener('message',  function (e) {
            myiframe.contentWindow.postMessage({"type":"getMarginTop-success", "top": window.pageYOffset}, '*'); 
    });
    

    iframe 监听消息->

    window.addEventListener('message',  function (e) {
            if (e.data.type == "getMarginTop-success"){
                //here the scrollTop data e.data.top
            }
    }, false);
    

    希望能帮到人

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-22
      • 2019-02-15
      • 2023-03-10
      • 2014-01-09
      • 1970-01-01
      • 2011-08-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多