Skip to content Skip to sidebar Skip to footer

Navigation With Jquery Smooth Scroll Causes Weird Behavior

I have a navigation on my website that is powered by jQuery to smooth-scroll to the target ID, but it is acting very strange (Chrome, Firefox): Sometimes it doesn't actually move

Solution 1:

Here's a working version. Apparently this is due to the overflow-x:hidden from body's CSS rule. See this question.

Removed that rule, now it seems perfectly fine (or prefer the codepen below that, since SO's fullpage version is bugging out a bit):

$("#scrollOne").click(function() {
  $('html,body').animate({
    scrollTop: $("#one").offset().top
  }, 750);
});

$("#scrollTwo").click(function(e) {
  $('html,body').animate({
    scrollTop: $("#two").offset().top
  }, 750);
});

$("#scrollThree").click(function() {
  $('html,body').animate({
    scrollTop: $("#three").offset().top
  }, 750);
});

$("#scrollFour").click(function() {
  $('html,body').animate({
    scrollTop: $("#four").offset().top
  }, 750);
});
@import url("https://fonts.googleapis.com/css?family=Muli:400,600,700");
@keyframes nav-item {
  from {
    transform: translateY(-100px) translateX(-20px); }
  to {
    transform: translateY(0) translateX(0); } }

@keyframes nav-item-alt {
  from {
    opacity: 0; }
  to {
    opacity: 1; } }

*,
*::before,
*::after {
  margin: 0;
  padding: 0;
  outline: none;
  box-sizing: inherit; }

html {
  box-sizing: border-box;
  font-size: 62.5%; }

html, body {
  width: 100%;
  height: 100%;
  font-family: Muli, sans-serif;
  color: #656565;
  letter-spacing: 1px;
}

body {
  font-size: 1.6rem;
  background-image: linear-gradient(to bottom, rgba(255, 255, 255, 0.5), rgba(255, 255, 255, 0.5)), url(../img/bg-pattern.png); }
  @mediaonly screen and (max-width: 25em) {
    body {
      background-image: linear-gradient(to bottom, rgba(255, 255, 255, 0.5), rgba(255, 255, 255, 0.5)), url(../img/bg-pattern-small.png); } }

.header-title {
  color: #262626;
  font-size: 4rem;
  margin-bottom: 1rem;
  margin-left: -3px; }
  @mediaonly screen and (max-width: 25em) {
    .header-title {
      text-align: center;
      margin-left: 3px; } }

.header-sub {
  color: #262626;
  font-size: 2rem; }
  @mediaonly screen and (max-width: 50em) {
    .header-sub {
      display: none; } }

.header {
  position: relative;
  width: 100%;
  height: 100vh;
  background-color: steelblue;
  padding: 1.5rem;
  display: grid;
  grid-template-rows: min-content min-content 1fr;
  grid-template-columns: repeat(2, minmax(min-content 1fr));
  grid-template-areas: "logo nav"". lang""cta cta"; }

.section-one {
	width: 100%;
	height: 100vh;
  padding: 3rem;
	background-color: gold;
}

.section-two {
	width: 100%;
	height: 100vh;
  padding: 3rem;
	background-color: tomato;
}

.section-three {
	width: 100%;
	height: 100vh;
  padding: 3rem;
	background-color: limegreen;
}

.section-four {
	width: 100%;
	height: 100vh;
  padding: 3rem;
	background-color: orange;
}

.navigation__checkbox {
  display: none; }

.navigation__button {
  display: none; }

@mediaonly screen and (min-width: 50em) {
  .navigation__nav {
    display: inline-block;
    justify-self: end;
    grid-area: nav; }
  .navigation__list {
    display: flex;
    flex-direction: row;
    list-style: none; }
  .navigation__link {
    position: relative;
    display: inline-block;
    color: #262626;
    font-size: 2rem;
    font-weight: 600;
    text-decoration: none;
    text-transform: uppercase;
    padding: 1rem0;
    transition: transform .1s ease-in-out; } }
  @mediaonly screen and (min-width: 50em) and (max-width: 68.75em) {
    .navigation__link {
      font-size: 2rem; } }
  @mediaonly screen and (min-width: 50em) and (min-width: 50em) {
    .navigation__link::before {
      content: '';
      position: absolute;
      bottom: 0;
      left: 0;
      width: 100%;
      height: 2px;
      background-color: #262626;
      transition: transform .1s ease-in-out;
      transform: scaleX(0);
      transform-origin: right; }
    .navigation__link:hover {
      transform: translateY(-2px); }
    .navigation__link:hover::before {
      transform: scaleX(1);
      transform-origin: left;
      transition: transform .2s ease-in-out; } }

@mediaonly screen and (min-width: 50em) {
  .navigation__item {
    margin-bottom: 1rem0; }
    .navigation__item:not(:last-child) {
      margin-right: 3rem; } }
    @mediaonly screen and (min-width: 50em) and (max-width: 62.5em) {
      .navigation__item:not(:last-child) {
        margin-right: 2rem; } }
    @mediaonly screen and (min-width: 50em) and (max-width: 56.25em) {
      .navigation__item:not(:last-child) {
        margin-right: 1rem; } }

@mediaonly screen and (min-width: 50em) {
    .navigation__item:nth-child(1) {
      animation: nav-item .5s backwards ease-in-out 0s; }
    .navigation__item:nth-child(2) {
      animation: nav-item .5s backwards ease-in-out .2s; }
    .navigation__item:nth-child(3) {
      animation: nav-item .5s backwards ease-in-out .4s; }
    .navigation__item:nth-child(4) {
      animation: nav-item .5s backwards ease-in-out .6s; } }

@mediaonly screen and (max-width: 50em) {
  .navigation__nav {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    visibility: hidden;
    opacity: 0; }
  .navigation__list {
    list-style: none;
    text-align: center; }
  .navigation__item {
    margin: 1.5rem0;
    opacity: 0; }
  .navigation__link:link, .navigation__link:visited {
    width: auto;
    display: inline-block;
    color: #fff;
    font-size: 3rem;
    font-weight: 300;
    text-decoration: none;
    text-transform: uppercase;
    padding: 1.5rem3rem;
    border-radius: 50px;
    background-image: linear-gradient(120deg, transparent 0%, transparent 50%, #fff50%);
    background-size: 250%;
    transition: all ease-in-out .5s, transform ease-in-out .25s; } }
  @mediaonly screen and (max-width: 50em) and (max-width: 37.5em) {
    .navigation__link:link, .navigation__link:visited {
      font-size: 2.5rem; } }
  @mediaonly screen and (max-width: 50em) and (max-width: 25em) {
    .navigation__link:link, .navigation__link:visited {
      font-size: 2rem; } }

@mediaonly screen and (max-width: 50em) {
  .navigation__link:hover, .navigation__link:active {
    color: #FFE20C;
    background-position: 100%;
    transform: translateX(-1rem); } }
  @mediaonly screen and (max-width: 50em) and (max-width: 25em) {
    .navigation__link:hover, .navigation__link:active {
      transform: none; } }

@mediaonly screen and (max-width: 50em) {
  .navigation__button {
    grid-area: nav;
    justify-self: end;
    display: block;
    position: fixed;
    top: 1.5rem;
    right: calc(1.5rem);
    width: 6rem;
    height: 6rem;
    border-radius: 50%;
    background-color: #fff;
    box-shadow: 06px3remrgba(38, 38, 38, 0.2);
    text-align: center;
    cursor: pointer;
    z-index: 300; }
    .navigation__button:active {
      transform: scale(0.9); }
  .navigation__background {
    width: 3rem;
    height: 3rem;
    position: fixed;
    top: 3rem;
    right: calc(3rem);
    border-radius: 50%;
    background-image: radial-gradient(#FFE20C, #FF200C);
    opacity: 0;
    z-index: 100;
    transition: transform 0s .75s,
 opacity .5s linear .25s; } }

.navigation__checkbox:checked ~ .navigation__background {
  transform: scale(100);
  opacity: .8;
  transition: transform 1.5s,
 opacity .5s; }

.navigation__checkbox:checked ~ .navigation__nav {
  visibility: visible;
  opacity: 1;
  transition: opacity 1s .5s;
  z-index: 200; }

@mediaonly screen and (max-width: 50em) {
  .navigation__checkbox:checked ~ .navigation__nav.navigation__list {
    visibility: visible;
    opacity: 1; }
  .navigation__checkbox:checked ~ .navigation__nav.navigation__item:nth-child(1) {
    animation: nav-item-alt .25s forwards ease-in-out 0s; }
  .navigation__checkbox:checked ~ .navigation__nav.navigation__item:nth-child(2) {
    animation: nav-item-alt .25s forwards ease-in-out .2s; }
  .navigation__checkbox:checked ~ .navigation__nav.navigation__item:nth-child(3) {
    animation: nav-item-alt .25s forwards ease-in-out .4s; }
  .navigation__checkbox:checked ~ .navigation__nav.navigation__item:nth-child(4) {
    animation: nav-item-alt .25s forwards ease-in-out .6s; }
  .navigation__checkbox:checked ~ .navigation__language {
    display: flex; } }

.navigation__icon {
  position: relative;
  margin-top: 3rem; }
  .navigation__icon, .navigation__icon::before, .navigation__icon::after {
    display: inline-block;
    width: 3rem;
    height: 2px;
    background-color: #656565;
    transition: all .25s; }
  .navigation__icon::before, .navigation__icon::after {
    content: '';
    position: absolute;
    left: 0; }
  .navigation__icon::before {
    transform: translateY(-1rem); }
  .navigation__icon::after {
    transform: translateY(1rem); }

.navigation__button:hover.navigation__icon {
  transform: scale(0.8); }

.navigation__checkbox:checked + .navigation__button.navigation__icon {
  background: transparent; }

.navigation__checkbox:checked + .navigation__button.navigation__icon::before {
  top: 0;
  transform: rotate(-135deg); }

.navigation__checkbox:checked + .navigation__button.navigation__icon::after {
  top: 0;
  transform: rotate(135deg); }

.navigation__checkbox:checked + .navigation__button:hover.navigation__icon {
  transform: scale(0.8); }
<scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><!-- Header --><headerclass="header"id="header"><inputclass="navigation__checkbox"id="nav-toggle"type="checkbox"><labelclass="navigation__button"for="nav-toggle"><spanclass="navigation__icon"></span></label><divclass="navigation__background"></div><divclass="company"><h2class="header-title">Company</h2><h3class="header-sub">Hambuger menu shown under 800px width</h3></div><navclass="navigation__nav"><ulclass="navigation__list"><liclass="navigation__item"><aclass="navigation__link scroll"id="scrollOne"href="#one">One</a></li><liclass="navigation__item"><aclass="navigation__link scroll"id="scrollTwo"href="#two">Two</a></li><liclass="navigation__item"><aclass="navigation__link scroll"id="scrollThree"href="#three">Three</a></li><liclass="navigation__item"><aclass="navigation__link scroll"id="scrollFour"href="#four">Four</a></li></ul></nav></header><!-- About --><sectionclass="section-one"id="one"><h2class="header-title">One</h2></section><!-- Quote --><sectionclass="section-two"id="two"><h2class="header-title">Two</h2></section><!-- Reviews --><sectionclass="section-three"id="three"><h2class="header-title">Three</h2></section><!-- Footer --><sectionclass="section-four"id="four"><h2class="header-title">Four</h2></section>

Codepen version

Unrelated: I would also suggest adding .stop() before each .animate() call so every new animation stops the previous one.

Solution 2:

offset().top values change when page scrolled. so, you need to save those values at the beginning, also you should re-assign those values when window size changes.

var oneTop = $("#one").offset().top;
    var twoTop = $("#two").offset().top;
    var threeTop = $("#three").offset().top;
    var fourTop = $("#four").offset().top;

code: https://codepen.io/peker-ercan/pen/wYrrWV?editors=1010

Solution 3:

The problem is that you have height:100% on the body, so the offset returns wrong values (correct, but not what you expect).

Give height:auto and min-height:100% to body and it will work correctly.

See updated pen: https://codepen.io/anon/pen/qJPPeL?editors=1010

Post a Comment for "Navigation With Jquery Smooth Scroll Causes Weird Behavior"