简体   繁体   English

固定位置不适用于标题

[英]Fixed position not working for header

I am trying to make my header fixed so that it will scroll along with the page for both mobile and desktop. 我正在尝试固定标题,使其与移动设备和台式机页面一起滚动。 The thing is that it was working earlier but as I kept coding, it stopped working. 事情是,它早先起作用了,但是当我继续编码时,它停止了工作。 I have tried getting rid of codes to see if any of them was effecting it but that wasn't the case. 我尝试摆脱代码,看看是否有任何代码在影响它,但事实并非如此。

Here is the HTML code: 这是HTML代码:

<div class='wrapper'>
  <div class='sidebar'>
    <div class='title'>
    </div>
    <ul class='nav'>
      <li><a href="index.html">Home</a></li>
      <li><a href="#about">About Us</a></li>
      <li><a href="publications.html">Publications</a></li>
      <li><a href="conferences.html">Conferences</a></li>
      <li><a href="#contact">Contact Us</a></li>
      </ul>
  </div>

<div class='content isOpen'>
<header>
    <div class="mobile grid">
    <div class="unit unit-s-2-3 unit-m-1-3 unit-l-1-3">
        <a href="index.html"><img src="img/logo.svg" class="flex-logo"></a>
    </div>
    <div class="unit unit-s-1-3 unit-m-2-3 unit-m-2-3-1 unit-l-2-3">
        <a class='button'></a>
    </div>
    </div>

    <div class="grid desktop">
        <div class="unit unit-s-1 unit-m-1-3 unit-l-1-3">
            <div class="unit unit-s-1 unit-m-1-3 unit-l-1-3 left">
                <a href="index.html"><img src="img/logo-square.svg" class="flex-logo"></a>
            </div>

            <div class="unit unit-s-1 unit-m-2-3 unit-l-2-3 desktop">
                <h1>International Academy of Science, Engineering, and Techonology</h1>
            </div>
        </div>

        <div class="unit unit-s-1 unit-m-2-3 unit-l-2-3 desktop-nav">
            <nav>
                <ul>
                    <li><a href="index.html">Home</a></li>
                    <li><a href="#about">About Us</a></li>
                    <li><a href="publications.html">Publications</a></li>
                    <li><a href="conferences.html">Conferences</a></li>
                    <li><a href="#contact">Contact Us</a></li>
                </ul>
            </nav>
        </div>
    </div>
</header> <!-- END HEADER -->
</div></div>

Here is the CSS: 这是CSS:

header {
position: fixed;
width: 100%;
background-color: #FFFFFF;
z-index: 999;
box-shadow: 0 2px 4px rgba(0,0,0,0.2);
}
.wrapper {
  min-height: 100%;
  background-color: #AB1917;
}

.sidebar {
  position: absolute;
  width: 135px;
  background-color: #AB1917;
height: 100%;
right: 0;
}

.content {
  background: #FFF;
  box-shadow: 0 0 5px rgba(0,0,0,0.5);
  transform: translate3d(-135px,0,0);
  transition: transform .3s;
}

.content.isOpen {
  transform: translate3d(0px,0,0);
}

And here is the js: 这是js:

<script type="text/javascript">
$(document).ready(function() {
  $('.button').on('click', function() {
    $('.content').toggleClass('isOpen');
  });
});
</script>

I have tried specifying a height for the header and even using a sticky nav plugin to make it fixed; 我尝试为标头指定高度,甚至使用粘性导航插件将其固定; however none of those has worked. 但是这些都不起作用。 And I am not sure what to do anymore. 而且我不确定该怎么办。

If you want to view the issue on the website, you can see it here: www.international-aset.com/intaset 如果要在网站上查看问题,可以在以下位置查看: www.international-aset.com/intaset

Thanks! 谢谢!

Your issue is that you have a transform on your content element. 您的问题是您对内容元素进行了转换。

This is because the transform creates a new local coordinate system, this means that fixed positioning becomes fixed to the transformed element, rather than the viewport. 这是因为变换创建了新的局部坐标系,这意味着固定的位置变得固定于变换后的元素,而不是视口。

I can see you are using transform for you mobile nav, a few solutions would be 我可以看到您正在为您的移动导航使用transform,一些解决方案是

  • Use a negative margin instead of transform 使用负边距而不是变换
  • Absoultely position your parent element and use left -180px etc 绝对定位您的父元素并使用向左-180px等
  • Move your nav outside of the container, fix its position then add padding at the top of your page. 将导航栏移出容器,固定其位置,然后在页面顶部添加填充。

Hope one of those work for you 希望其中一项为您服务

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM