【问题标题】:jQuery .outerHeight() on an element returns incorrect size on orientation change元素上的 jQuery .outerHeight() 在方向更改时返回不正确的大小
【发布时间】:2020-10-10 04:26:23
【问题描述】:

我正在尝试在我的网站上获取顶部导航栏的 outerHeight 以进行一些计算,它在加载/重新加载网页时效果很好。但是,当我将 Chrome 开发人员工具中的屏幕方向更改为横向时,它会返回错误的高度。我的代码在获取 outerHeight 之前侦听调整大小更改事件,所以我不确定为什么它在网页的方向更改时返回错误的高度,但在加载/重新加载网页时返回正确的高度。我认为这可能与 resize 侦听器在方向更改后没有等待页面完全加载有关,但我不确定。

对于代码 sn-p,我尝试将 Bootstrap 4.5 添加到其中,但它一直返回错误。如果有人想在谁知道该怎么做中添加它,那将不胜感激,以便在运行代码 sn-p 时该页面更加准确。

body {
  background-position: center center;
  background-size: auto;
  position: absolute;
  overflow-x: hidden;
  overflow-y: scroll;
  width: 100%;
  height: 100%;
  font-family: 'Montserrat', sans-serif;
  color: white;
}

a {
  color: white;
}

a:hover {
  text-decoration: none;
  color: white;
}

#content-wrapper {
  overflow-y: scroll;
  -ms-overflow-style: none;
  overflow: -moz-scrollbars-none;
}

#content-wrapper::-webkit-scrollbar {
  display: none;
}

#top_navbar {
  padding: 0;
}

.navbar-brand {
  padding: 0;
}

#navbar_logo {
  width: 15vw;
  margin-right: 6vw;
  margin-left: 0 !important;
}

.nav-link {
  font-family: Arial, Helvetica, sans-serif;
  font-size: 2vw;
  -webkit-text-fill-color: white;
  -webkit-text-stroke-width: 1px;
  -webkit-text-stroke-color: #1b1b1b;
  letter-spacing: 1px;
}

.nav-link:hover {
  -webkit-text-fill-color: #1b1b1b;
  -webkit-text-stroke-width: 1px;
  -webkit-text-stroke-color: white;
}

.nav-item {
  margin-left: 3vw;
  margin-right: 3vw;
}

#bottom_footer,
#legal_documents {
  background-color: #1b1b1b;
  width: 100%;
  position: absolute;
  display: table;
  bottom: 0;
  text-align: center;
  padding-top: 1%;
  padding-bottom: .4%;
}

#legal_documents {
  bottom: unset;
  font-size: 10px;
  padding-bottom: .6% !important;
}

#social_buttons::selection {
  color: none;
  background: none;
}

#social_buttons::-moz-selection {
  color: none;
  background: none;
}

#social_buttons a {
  display: inline-block;
  margin: 0 15px;
  background-position: center center;
  background-repeat: no-repeat;
  background-size: contain;
}

#bottom_footer a:hover,
#legal_documents a:hover {
  opacity: 0.5;
}
<html lang="en">
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, shrink-to-fit=no">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.0/css/bootstrap.min.css" />
  <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.0/js/bootstrap.min.js"></script>
</head>
<body>
  <nav id="top_navbar" class="navbar navbar-expand-sm navbar-dark">
    <a class="navbar-brand" href="" title="Melo Relo">
      <img id="navbar_logo" src="">
    </a>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbar_items" aria-controls="navbar_items" aria-expanded="false" aria-label="Toggle navigation">
          <span class="navbar-toggler-icon"></span>
        </button>

    <div class="collapse navbar-collapse" id="navbar_items">
      <ul class="navbar-nav">
        <li class="nav-item">
          <a class="nav-link" href="">LABEL</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="">LABEL</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="">LABEL</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="">LABEL</a>
        </li>
      </ul>
    </div>
  </nav>

  <main id="content-wrapper" class="container-fluid">
  </main>

  <div id="bottom_footer">
    <div id="social_buttons">
      <a href="" class="facebook" title="Facebook" target="_blank"></a>
      <a href="" class="twitter" title="Twitter" target="_blank"></a>
      <a href="" class="instagram" title="Instagram" target="_blank"></a>
      <a href="" class="snapchat" title="Snapchat" target="_blank"></a>
      <a href="" class="youtube" title="YouTube" target="_blank"></a>
    </div>
    <div id="legal_documents">
      <a href="" title="Privacy Policy" target="_blank">PRIVACY POLICY</a> |
      <a href="" title="Terms and Conditions" target="_blank">TERMS AND CONDITIONS</a>
    </div>
  </div>
</body>

</html>

【问题讨论】:

    标签: javascript jquery screen-orientation orientation-changes outerheight


    【解决方案1】:
    1. &lt;meta name="viewport" content="width=device-width, initial-scale=1"&gt; 添加到您的&lt;head&gt; 标签。这将有助于使用正确的 CSS 比例和根据设备宽度浏览您的网站。更多信息请点击此处https://developer.mozilla.org/en-US/docs/Mozilla/Mobile/Viewport_meta_tag

    2. 您可以添加特殊的window.addEventListener("orientationchange", function() { ... }); 监听器。这个名字说明了一切。更多信息请点击此处https://developer.mozilla.org/en-US/docs/Web/API/Window/orientationchange_event

    $(window).on('resize', function() {
      window_width = $(window).width() + (window.innerWidth - document.documentElement.clientWidth);
      resizeContainer(window_width);
    });
    
    window.addEventListener("orientationchange", function() {
      window_width = $(window).width() + (window.innerWidth - document.documentElement.clientWidth);
      resizeContainer(window_width);
    });
    
    function resizeContainer(window_width) {
      let container_height;
      let navbar_height;
      let window_height = $(window).height();
      let footer_height = $("#bottom_footer").innerHeight();
    
      if (window_width <= 575) {
        navbar_height = $("#top_navbar").outerHeight(true) + 10;
        $('#content-wrapper').css("margin-top", navbar_height);
        $("#navbar_logo").attr("src", "/static/images/title-logo-mobile.jpg");
        $("#navbar_logo").css({
          "width": "28vw",
          "margin-left": "0"
        });
      } else {
        navbar_height = $("#top_navbar").outerHeight(true);
        console.log(navbar_height);
        $('#content-wrapper').css("margin-top", 0);
        $("#navbar_logo").attr("src", "/static/images/title-logo.jpg");
        $("#navbar_logo").css({
          "width": "15vw",
          "margin-left": "3vw"
        });
      }
    
      container_height = window_height - navbar_height - footer_height;
      $("#content-wrapper").css("height", container_height);
    }
    body {
      background-position: center center;
      background-size: auto;
      position: absolute;
      overflow-x: hidden;
      overflow-y: hidden;
      width: 100%;
      height: 100%;
      font-family: 'Montserrat', sans-serif;
      color: white;
    }
    
    a {
      color: white;
    }
    
    a:hover {
      text-decoration: none;
      color: white;
    }
    
    #content-wrapper {
      overflow-y: scroll;
      -ms-overflow-style: none;
      overflow: -moz-scrollbars-none;
    }
    
    #content-wrapper::-webkit-scrollbar {
      display: none;
    }
    
    #top_navbar {
      padding: 0;
    }
    
    .navbar-brand {
      padding: 0;
    }
    
    #navbar_logo {
      width: 15vw;
      margin-right: 6vw;
      margin-left: 0 !important;
    }
    
    .nav-link {
      font-family: Arial, Helvetica, sans-serif;
      font-size: 2vw;
      -webkit-text-fill-color: white;
      -webkit-text-stroke-width: 1px;
      -webkit-text-stroke-color: #1b1b1b;
      letter-spacing: 1px;
    }
    
    .nav-link:hover {
      -webkit-text-fill-color: #1b1b1b;
      -webkit-text-stroke-width: 1px;
      -webkit-text-stroke-color: white;
    }
    
    .nav-item {
      margin-left: 3vw;
      margin-right: 3vw;
    }
    
    #bottom_footer,
    #legal_documents {
      background-color: #1b1b1b;
      width: 100%;
      position: fixed;
      display: table;
      bottom: 0;
      text-align: center;
      padding-top: 1%;
      padding-bottom: .4%;
    }
    
    #legal_documents {
      bottom: unset;
      font-size: 10px;
      padding-bottom: .6% !important;
    }
    
    #social_buttons::selection {
      color: none;
      background: none;
    }
    
    #social_buttons::-moz-selection {
      color: none;
      background: none;
    }
    
    #social_buttons a {
      display: inline-block;
      margin: 0 15px;
      background-position: center center;
      background-repeat: no-repeat;
      background-size: contain;
    }
    
    #bottom_footer a:hover,
    #legal_documents a:hover {
      opacity: 0.5;
    }
    <html lang="en">
    
    <head>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
      <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.0/css/bootstrap.min.css" />
      <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.0/js/bootstrap.min.js"></script>
      <meta name="viewport" content="width=device-width, initial-scale=1">
    </head>
    
    <body>
      <nav id="top_navbar" class="navbar navbar-expand-sm navbar-dark">
        <a class="navbar-brand" href="" title="Melo Relo">
          <img id="navbar_logo" src="/static/images/title-logo.jpg">
        </a>
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbar_items" aria-controls="navbar_items" aria-expanded="false" aria-label="Toggle navigation">
          <span class="navbar-toggler-icon"></span>
        </button>
    
        <div class="collapse navbar-collapse" id="navbar_items">
          <ul class="navbar-nav">
            <li class="nav-item">
              <a class="nav-link" href="">LABEL</a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="">LABEL</a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="">LABEL</a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="">LABEL</a>
            </li>
          </ul>
        </div>
      </nav>
    
      <main id="content-wrapper" class="container-fluid">
      </main>
    
      <div id="bottom_footer">
        <div id="social_buttons">
          <a href="" class="facebook" title="Facebook" target="_blank"></a>
          <a href="" class="twitter" title="Twitter" target="_blank"></a>
          <a href="" class="instagram" title="Instagram" target="_blank"></a>
          <a href="" class="snapchat" title="Snapchat" target="_blank"></a>
          <a href="" class="youtube" title="YouTube" target="_blank"></a>
        </div>
        <div id="legal_documents">
          <a href="" title="Privacy Policy" target="_blank">PRIVACY POLICY</a> |
          <a href="" title="Terms and Conditions" target="_blank">TERMS AND CONDITIONS</a>
        </div>
      </div>
    
    
    </body>
    
    </html>

    【讨论】:

    • 浏览了我的答案。将 $(window).on("orientationchange"... 替换为 window.addEventListener("orientationchange"...。现在工作正常。
    • 我在 chrome 开发工具中注意到的一点是,resize 事件是在方向更改时触发的,所以我认为没有必要有两个监听器。另外,如果是这样,那么我看不到我需要修复什么,因为调整大小侦听器已经被触发,我仍然对为什么窗口没有为 .outerHeight() 返回正确的高度值感到困惑。我更新了我的代码 sn-p。
    【解决方案2】:

    我发现了这个问题。将屏幕方向更改为横向时 jQuery 高度方法返回错误高度计算的原因是顶部导航栏进行了某些更改,并且这些更改是在获取顶部导航栏高度后进行的。这些更改应该在获得顶部导航栏高度之前进行。对顶部导航栏所做的更改包括#navbar-logo 的大小和实际图像,并将#content-wrapper's margin-top 设置为0。交换代码并等待图像加载后,每个方向都返回了正确的高度改变。

      if (window_width <= 575) {
        $("#navbar_logo").attr("src", "/static/images/title-logo-mobile.jpg");
      } else {
        $("#navbar_logo").attr("src", "/static/images/title-logo.jpg");
      }
    
      $("#navbar_logo").on("load", function() {
        if (window_width <= 575) {
          $("#navbar_logo").css({"width":"28vw", "margin-left":"0"});
          navbar_height = $("#top_navbar").outerHeight(true) + 10;
          $('#content-wrapper').css("margin-top", navbar_height);
        } else {
          $("#navbar_logo").css({"width":"15vw", "margin-left":"3vw"});
          navbar_height = $("#top_navbar").outerHeight(true);
          $('#content-wrapper').css("margin-top", 0);
        }
        container_height = window_height - navbar_height - footer_height;
        $("#content-wrapper").css("height", container_height);
      });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-04
      • 1970-01-01
      • 1970-01-01
      • 2015-03-21
      • 1970-01-01
      • 2013-05-01
      • 1970-01-01
      • 2011-03-23
      相关资源
      最近更新 更多