Anchors not working when header's position is fixed

user9019807

I've made a header that has fixed position when I scroll. But when that happens the anchor tag stops working and all the effects as well

.buttons {
    margin-left: 460px;
}

.buttons .button a i {
    color: #fff;
    transition: .2s;
}

.buttons .button {
    float: left;
    margin-left: 10px;
    margin-top: 0px;
    transition: .8s;
    background-image: url(img/hexagon0.png);
    width: 50px;
    height: 50px;
    cursor: pointer;
}

.buttons .button a i:hover {
    color: #ff8000;
}

.buttons .button a i {
    margin-top: 15px;
}

.top-container {
    background-color: #f1f1f1;
    padding: 30px;
    text-align: center;
}

.header {
    padding: 10px 16px;
    background: #555;
    color: #f1f1f1;
    z-index: -1;
}

.content {
    padding: 16px;
    z-index: 1;
}

.sticky {
    position: fixed;
    top: 0;
    width: 100%;
}

.sticky + .content {
    padding-top: 102px;
}
div.hexagons {
    padding-left: 100px;
    margin-top: 100px;
}

.hex i {
    color: #fff;
    margin-top: 45px;
    transition: .5s ease;
}

.hex {
    cursor: pointer;
    width: 200px;
}

#hex1 {
    margin-left: -300px;
}

#hex2 {
    margin-left: 108px;
    margin-top: -172px;
}

#hex3 {
    margin-top: 60px;
    margin-left: -99px;
}

#hex4 {
    margin-left: 310px;
    margin-top: -172px;
}

#hex5 {
    margin-left: -510px;
    margin-top: -175px;
}

.hex:hover i {
    transform: translateY(-70px);
}

.hex .text p {
    margin-top: -50px;
    opacity: 0;
    transition: .5s;
    transform: translateY(50px);
    color: #fff;
}

.hex .text p#title {
    font-size: 15px;
}

.hex:hover .text p#title {
    font-size: 18px;
}

.hex .text p#content {
    margin-top: 0;
    font-size: 15px;
}

.hex:hover .text p#content {
    font-size: 15px;
}

.hex:hover .text p {
    transform: translateY(0);
    opacity: 1;
}

.hexagon {
    position: relative;
    width: 200px;
    height: 115.47px;
    background-color: #2e2e2e;
    margin: 57.74px 0;
    box-shadow: 0 0 20px rgba(0,0,0,0.6);
}

.hexagon:before,
.hexagon:after {
    content: "";
    position: absolute;
    z-index: 1;
    width: 141.42px;
    height: 141.42px;
    -webkit-transform: scaleY(0.5774) rotate(-45deg);
    -ms-transform: scaleY(0.5774) rotate(-45deg);
    transform: scaleY(0.5774) rotate(-45deg);
    background-color: inherit;
    left: 29.2893px;
    box-shadow: 0 0 20px rgba(0,0,0,0.6);
}

.hexagon:before {
    top: -70.7107px;
}

.hexagon:after {
    bottom: -70.7107px;
}

/*cover up extra shadows*/
.hexagon span {
    display: block;
    position: absolute;
    top:0px;
    left: 0;
    width:200px;
    height:115.4701px;
    z-index: 2;
    background: inherit;
}

.hex-img1 {
    margin-left: -300px;
    margin-top: -50px;
    z-index: -1;
}

.hexagon-img {
    position: relative;
    width: 800px;
    height: 461.88px;
    margin: 230.94px 0;
    background-image: url(img/2.jpg);
    background-size: auto 912.2134px;
    background-position: center;
    border-left: solid 5px #ff8000;
    border-right: solid 5px #ff8000;
}

.hexTop,
.hexBottom {
    position: absolute;
    z-index: 1;
    width: 565.69px;
    height: 565.69px;
    overflow: hidden;
    -webkit-transform: scaleY(0.5774) rotate(-45deg);
    -ms-transform: scaleY(0.5774) rotate(-45deg);
    transform: scaleY(0.5774) rotate(-45deg);
    background: inherit;
    left: 112.16px;
}

/*counter transform the bg image on the caps*/
.hexTop:after,
.hexBottom:after {
    content: "";
    position: absolute;
    width: 790.0000px;
    height: 456.1067126598044px;
    -webkit-transform:  rotate(45deg) scaleY(1.7321) translateY(-228.0534px);
    -ms-transform:      rotate(45deg) scaleY(1.7321) translateY(-228.0534px);
    transform:          rotate(45deg) scaleY(1.7321) translateY(-228.0534px);
    -webkit-transform-origin: 0 0;
    -ms-transform-origin: 0 0;
    transform-origin: 0 0;
    background: inherit;
}

.hexTop {
    top: -282.8427px;
    border-top: solid 7.0711px #ff8000;
    border-right: solid 7.0711px #ff8000;
}

.hexTop:after {
    background-position: center top;
}

.hexBottom {
    bottom: -282.8427px;
    border-bottom: solid 7.0711px #ff8000;
    border-left: solid 7.0711px #ff8000;
}

.hexBottom:after {
    background-position: center bottom;
}

.hexagon-img:after {
    content: "";
    position: absolute;
    top: 2.8868px;
    left: 0;
    width: 790.0000px;
    height: 456.1067px;
    z-index: 2;
    background: inherit;
}

.content {
    margin-top: 1000px;
}

.section-title {
    background-color: #ff8000;
    width: 300px;
    text-align: center;
    transform: rotateZ(30deg) translateY(-21px) translateX(200px);
}

.section-title p {
    font-size: 50px;
    color: #fff;
    z-index: 1;
    font-weight: bold;
}
<!DOCTYPE html>
<html lang="en" dir="ltr">
      <head>
            <meta charset="utf-8">
            <title></title>
            <link rel="stylesheet" href="style.css">
            <link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">
            <script src="https://code.jquery.com/jquery-3.3.1.js"></script>

      </head>
      <body>
          <center>
              <div class="hexagons">
                  <a href="#one"><div class="hex" id="hex1">
                      <div class="hexagon">
                          <span>
                              <i style="font-size: 30px;" class="fa fa-paint-brush" aria-hidden="true"></i>
                              <div class="text">
                                  <p id="title">Grafisk Design</p>
                                  <p id="content">Jag har jobbat i många år med Grafisk Design</p>
                              </div>
                          </span>
                      </div>
               </div>
               </a>

               <a href="#two"><div class="hex" id="hex2">
                   <div class="hexagon">
                       <span>
                           <i style="font-size: 30px;" class="fa fa-gamepad" aria-hidden="true"></i>
                           <div class="text">
                               <p id="title">Spel Skapande</p>
                               <p id="content">Jag älskar att jobba med speldesign och skapande</p>
                           </div>
                        </span>
                   </div>
                </div>
                </a>

                <a href="#three"><div class="hex" id="hex3">
                    <div class="hexagon">
                        <span>
                            <i style="font-size: 30px;" class="fa fa-music" aria-hidden="true"></i>
                            <div class="text">
                                <p id="title">Musik Skapande</p>
                                <p id="content">Jag brukar på min fritid <br> skapa musik</p>
                            </div>
                        </span>
                    </div>
                </div></a>

                <a href="#four"><div class="hex" id="hex4">
                    <div class="hexagon">
                        <span>
                            <i style="font-size: 30px;" class="fa fa-file-text-o" aria-hidden="true"></i>
                            <div class="text">
                                <p id="title">CV</p>
                                <p id="content">Här kan du se mitt CV</p>
                            </div>
                        </span>
                   </div>
              </div></a>

              <a href="#four">
                  <div class="hex" id="hex5">
                      <div class="hexagon">
                          <span>
                              <i style="font-size: 30px;" class="fa fa-video-camera" aria-hidden="true"></i>
                              <div class="text">
                                  <p id="title">Videos</p>
                                  <p id="content">Här postar jag min videos</p>
                              </div>
                          </span>
                        </div>
                  </div></a>
             </div>
       </center>
       <br><br><br><br><br><br><br><br><br><br>
            <div class="header" id="myHeader">
                  <div class="buttons">
                        <div class="button-hover">
                              <div class="button">
                                    <a href="youtube.com"><i style="font-size: 17px;" class="fa fa-paint-brush" aria-hidden="true"></i></a>
                              </div>
                        </div>
                        <div class="button">
                              <a href="#"><i style="font-size: 17px;" class="fa fa-gamepad" aria-hidden="true"></i></a>
                        </div>

                        <div class="button">
                              <a href="#"><i style="font-size: 17px;" class="fa fa-music" aria-hidden="true"></i></a>
                        </div>

                        <div class="button">
                              <a href="#"><i style="font-size: 17px;" class="fa fa-file-text-o" aria-hidden="true"></i></a>
                        </div>

                        <div class="button">
                              <a href="#"><i style="font-size: 17px;" class="fa fa-video-camera" aria-hidden="true"></i></a>
                        </div>
                  </div>
            </div>

      </div>
</div>

<div class="content">
      <div id="one">
            <div class="section-title">
                  <p>section 1</p>
            </div>
            <div class="hex-img1">
                  <div class="hexagon-img">
                        <div class="hexTop"></div>
                        <div class="hexBottom"></div>
                  </div>
            </div>
      </div>

      <div id="two">
            <p>section 2</p>
      </div>

      <div id="three">
            <p>section 3</p>
      </div>

      <div id="four">
            <p>section 4</p>
      </div>

      <div id="five">
            <p>section 5</p>
      </div>
</div>

<script>
window.onscroll = function() {myFunction()};

var header = document.getElementById("myHeader");
var sticky = header.offsetTop;

function myFunction() {
 if (window.pageYOffset >= sticky) {
    header.classList.add("sticky");
 } else {
    header.classList.remove("sticky");
 }
}
</script>

<script type="text/javascript">
$("a[href^='#']").click(function(e) {
      e.preventDefault();

      var position = $($(this).attr("href")).offset().top;

      $("body, html").animate({
            scrollTop: position
      } /* speed */ );
      });
</script>

</body>
</html>

So I need help to get this working on my site.

I want the header to appear when I scroll as a second menu apart from the one at the top. So it is easy to access wherever you are on the page.

Trupti
 <!-- Add z-index: 9 to element '.sticky' in your css -->

.buttons {
      margin-left: 460px;
}

.buttons .button a i {
      color: #fff;
      transition: .2s;
}

.buttons .button {
      float: left;
      margin-left: 10px;
      margin-top: 0px;
      transition: .8s;
      background-image: url(img/hexagon0.png);
      width: 50px;
      height: 50px;
      cursor: pointer;
}

.buttons .button a i:hover {
      color: #ff8000;
}

.buttons .button a i {
      margin-top: 15px;
}

.top-container {
  background-color: #f1f1f1;
  padding: 30px;
  text-align: center;
}

.header {
  padding: 10px 16px;
  background: #555;
  color: #f1f1f1;
  z-index: -1;
}

.content {
  padding: 16px;
  z-index: 1;
}

.sticky {
  position: fixed;
  top: 0;
  width: 100%;
  z-index:9;
}

.sticky + .content {
  padding-top: 102px;
}
div.hexagons {
      padding-left: 100px;
      margin-top: 100px;
}

.hex i {
      color: #fff;
      margin-top: 45px;
      transition: .5s ease;
}

.hex {
      cursor: pointer;
      width: 200px;
}

#hex1 {
      margin-left: -300px;
}

#hex2 {
      margin-left: 108px;
      margin-top: -172px;
}

#hex3 {
      margin-top: 60px;
      margin-left: -99px;
}

#hex4 {
      margin-left: 310px;
      margin-top: -172px;
}

#hex5 {
      margin-left: -510px;
      margin-top: -175px;
}

.hex:hover i {
      transform: translateY(-70px);
}

.hex .text p {
      margin-top: -50px;
      opacity: 0;
      transition: .5s;
      transform: translateY(50px);
      color: #fff;
}

.hex .text p#title {
      font-size: 15px;
}

.hex:hover .text p#title {
      font-size: 18px;
}

.hex .text p#content {
      margin-top: 0;
      font-size: 15px;
}

.hex:hover .text p#content {
      font-size: 15px;
}

.hex:hover .text p {
      transform: translateY(0);
      opacity: 1;
}

.hexagon {
  position: relative;
  width: 200px;
  height: 115.47px;
  background-color: #2e2e2e;
  margin: 57.74px 0;
  box-shadow: 0 0 20px rgba(0,0,0,0.6);
}

.hexagon:before,
.hexagon:after {
  content: "";
  position: absolute;
  z-index: 1;
  width: 141.42px;
  height: 141.42px;
  -webkit-transform: scaleY(0.5774) rotate(-45deg);
  -ms-transform: scaleY(0.5774) rotate(-45deg);
  transform: scaleY(0.5774) rotate(-45deg);
  background-color: inherit;
  left: 29.2893px;
  box-shadow: 0 0 20px rgba(0,0,0,0.6);
}

.hexagon:before {
  top: -70.7107px;
}

.hexagon:after {
  bottom: -70.7107px;
}

/*cover up extra shadows*/
.hexagon span {
  display: block;
  position: absolute;
  top:0px;
  left: 0;
  width:200px;
  height:115.4701px;
  z-index: 2;
  background: inherit;
}

.hex-img1 {
      margin-left: -300px;
      margin-top: -50px;
      z-index: -1;
}

.hexagon-img {
  position: relative;
  width: 800px;
  height: 461.88px;
  margin: 230.94px 0;
  background-image: url(img/2.jpg);
  background-size: auto 912.2134px;
  background-position: center;
  border-left: solid 5px #ff8000;
  border-right: solid 5px #ff8000;
}

.hexTop,
.hexBottom {
  position: absolute;
  z-index: 1;
  width: 565.69px;
  height: 565.69px;
  overflow: hidden;
  -webkit-transform: scaleY(0.5774) rotate(-45deg);
  -ms-transform: scaleY(0.5774) rotate(-45deg);
  transform: scaleY(0.5774) rotate(-45deg);
  background: inherit;
  left: 112.16px;
}

/*counter transform the bg image on the caps*/
.hexTop:after,
.hexBottom:after {
  content: "";
  position: absolute;
  width: 790.0000px;
  height: 456.1067126598044px;
  -webkit-transform:  rotate(45deg) scaleY(1.7321) translateY(-228.0534px);
  -ms-transform:      rotate(45deg) scaleY(1.7321) translateY(-228.0534px);
  transform:          rotate(45deg) scaleY(1.7321) translateY(-228.0534px);
  -webkit-transform-origin: 0 0;
  -ms-transform-origin: 0 0;
  transform-origin: 0 0;
  background: inherit;
}

.hexTop {
  top: -282.8427px;
  border-top: solid 7.0711px #ff8000;
  border-right: solid 7.0711px #ff8000;
}

.hexTop:after {
  background-position: center top;
}

.hexBottom {
  bottom: -282.8427px;
  border-bottom: solid 7.0711px #ff8000;
  border-left: solid 7.0711px #ff8000;
}

.hexBottom:after {
  background-position: center bottom;
}

.hexagon-img:after {
  content: "";
  position: absolute;
  top: 2.8868px;
  left: 0;
  width: 790.0000px;
  height: 456.1067px;
  z-index: 2;
  background: inherit;
}

.content {
      margin-top: 1000px;
}

.section-title {
      background-color: #ff8000;
      width: 300px;
      text-align: center;
      transform: rotateZ(30deg) translateY(-21px) translateX(200px);
}

.section-title p {
      font-size: 50px;
      color: #fff;
      z-index: 1;
      font-weight: bold;
}
<!DOCTYPE html>
<html lang="en" dir="ltr">
      <head>
            <meta charset="utf-8">
            <title></title>
            <link rel="stylesheet" href="style.css">
            <link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">
            <script src="https://code.jquery.com/jquery-3.3.1.js"></script>

      </head>
      <body>

            <center>

                  <div class="hexagons">
                        <a href="#one"><div class="hex" id="hex1">
                              <div class="hexagon">
                                    <span>
                                          <i style="font-size: 30px;" class="fa fa-paint-brush" aria-hidden="true"></i>
                                          <div class="text">
                                                <p id="title">Grafisk Design</p>
                                                <p id="content">Jag har jobbat i många år med Grafisk Design</p>
                                          </div>
                                    </span>
                              </div>
                        </div></a>

                        <a href="#two"><div class="hex" id="hex2">
                              <div class="hexagon">
                                    <span>
                                          <i style="font-size: 30px;" class="fa fa-gamepad" aria-hidden="true"></i>
                                          <div class="text">
                                                <p id="title">Spel Skapande</p>
                                                <p id="content">Jag älskar att jobba med speldesign och skapande</p>
                                          </div>
                                    </span>
                              </div>
                        </div></a>

                        <a href="#three"><div class="hex" id="hex3">
                              <div class="hexagon">
                                    <span>
                                          <i style="font-size: 30px;" class="fa fa-music" aria-hidden="true"></i>
                                          <div class="text">
                                                <p id="title">Musik Skapande</p>
                                                <p id="content">Jag brukar på min fritid <br> skapa musik</p>
                                          </div>
                                    </span>
                              </div>
                        </div></a>

                        <a href="#four"><div class="hex" id="hex4">
                              <div class="hexagon">
                                    <span>
                                          <i style="font-size: 30px;" class="fa fa-file-text-o" aria-hidden="true"></i>
                                          <div class="text">
                                                <p id="title">CV</p>
                                                <p id="content">Här kan du se mitt CV</p>
                                          </div>
                                    </span>
                              </div>
                        </div></a>

                        <a href="#four"><div class="hex" id="hex5">
                              <div class="hexagon">
                                    <span>
                                          <i style="font-size: 30px;" class="fa fa-video-camera" aria-hidden="true"></i>
                                          <div class="text">
                                                <p id="title">Videos</p>
                                                <p id="content">Här postar jag min videos</p>
                                          </div>
                                    </span>
                              </div>
                        </div></a>
                  </div>

            </center>
      <br><br><br><br><br><br><br><br><br><br>
            <div class="header" id="myHeader">
                  <div class="buttons">

                        <div class="button-hover">
                              <div class="button">
                                    <a href="youtube.com"><i style="font-size: 17px;" class="fa fa-paint-brush" aria-hidden="true"></i></a>
                              </div>
                        </div>

                        <div class="button">
                              <a href="#"><i style="font-size: 17px;" class="fa fa-gamepad" aria-hidden="true"></i></a>
                        </div>

                        <div class="button">
                              <a href="#"><i style="font-size: 17px;" class="fa fa-music" aria-hidden="true"></i></a>
                        </div>

                        <div class="button">
                              <a href="#"><i style="font-size: 17px;" class="fa fa-file-text-o" aria-hidden="true"></i></a>
                        </div>

                        <div class="button">
                              <a href="#"><i style="font-size: 17px;" class="fa fa-video-camera" aria-hidden="true"></i></a>
                        </div>
                  </div>
            </div>

      </div>
</div>

<div class="content">
      <div id="one">
            <div class="section-title">
                  <p>section 1</p>
            </div>
            <div class="hex-img1">
                  <div class="hexagon-img">
                        <div class="hexTop"></div>
                        <div class="hexBottom"></div>
                  </div>
            </div>
      </div>

      <div id="two">
            <p>section 2</p>
      </div>

      <div id="three">
            <p>section 3</p>
      </div>

      <div id="four">
            <p>section 4</p>
      </div>

      <div id="five">
            <p>section 5</p>
      </div>
</div>

<script>
window.onscroll = function() {myFunction()};

var header = document.getElementById("myHeader");
var sticky = header.offsetTop;

function myFunction() {
 if (window.pageYOffset >= sticky) {
    header.classList.add("sticky");
 } else {
    header.classList.remove("sticky");
 }
}
</script>

<script type="text/javascript">
$("a[href^='#']").click(function(e) {
      e.preventDefault();

      var position = $($(this).attr("href")).offset().top;

      $("body, html").animate({
            scrollTop: position
      } /* speed */ );
      });
</script>

      </body>
</html>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Fixed position not working for header

From Dev

"position:fixed" Not Working For Header Menu

From Dev

Fixed header influences anchors

From Dev

Header is disappearing when the position is fixed

From Dev

CSS position:fixed is working for the header but not for the footer

From Dev

Horizontal scrolling not working when position absolute/fixed

From Dev

fixed position header overlapping

From Dev

When navigating to #id, element is hidden under fixed position header

From Dev

Header in fixed position is overlapping with container div when the window is reduced in width

From Dev

Fixed Header not properly working

From Dev

Fixed Position not working in Chrome

From Dev

Position: Fixed; Not working properly

From Dev

Position fixed not working on Tumblr

From Dev

Scroll to anchor position with fixed header

From Dev

Fixed position not working in Safari 7

From Dev

Position fixed not working in mobile browser

From Dev

position:fixed not working in Google Chrome

From Dev

Position fixed not working on Windows Safari

From Dev

Fixed position not working on ios devices

From Dev

position fixed on bootstrap 'affix' not working

From Dev

onclick event not working with position:fixed

From Dev

CSS Position Fixed not working in Chrome

From Dev

Jquery .animate not working for fixed position

From Dev

Background position fixed not working in firefox

From Dev

Fixed position not working on ios devices

From Dev

Fixed position and float not working correctly

From Dev

bootstrap nav and fixed position not working

From Dev

Menu bar with position fixed not working

From Dev

CSS - Calc not working with position fixed

Related Related

HotTag

Archive