javascript - iOS Safari 键盘出现不会触发 window.resize 事件

标签 javascript ios css

我有一个需要响应键盘外观的布局。

  • 安卓(6+) - 当键盘出现时 - window.resize触发器。
  • iOS(版本 10+) - window.resize不触发。

  • 我准备了一个小演示,展示了内部高度如何响应键盘外观。您可以点击输入以调出键盘。

    // select the paragraph element
    const innerHeightParagraph = document.getElementById("inner-height")
    
    // set the innerHeight for the first time.
    innerHeightParagraph.innerText = window.innerHeight
    
    // register resize event.
    window.addEventListener('resize', function() {
      innerHeightParagraph.innerText= window.innerHeight
    })
    p {
      font-size:60px; margin:0px;
    
    
    }
    <h1>Current InnerHeight</h1>
    <p id="inner-height"></p>
    <input></input>


    iOS 的建议解决方法是什么?

    最佳答案

    window.visualViewport.addEventListener可以使用,只要显示/隐藏键盘就会触发

      if (window.visualViewport) {
        window.visualViewport.addEventListener("resize", () => {
          console.log("visualViewport RESIZE IS TRIGGERED");
        });
      }
    

    关于javascript - iOS Safari 键盘出现不会触发 window.resize 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49373265/

    相关文章:

    css - 为什么我的 Web 组件 CSS 不显示?我没有使用 shadowDOM

    execCommand 中 'paste as plain text` 的 Javascript 技巧

    JavaScript:将 <div> 元素移到列中 2 个元素之后的右侧

    javascript - 使用 JS 关闭多个模态框

    ios - 使用 iOS 和 Django 访问共享数据库

    javascript - 固定页脚和 outerheight(true) 返回值

    Javascript - 从字符串中删除特定字母

    ios - 无法在 currentRunLoop only MainRunLoop 上安排 NSStream

    ios - 按键的最后一个字符对字典中的元素进行分组 [iOS Swift 5]

    internet-explorer-9 - IE9 中的 CSS3 框阴影模糊