使用jQuery平滑滚动导航会导致奇怪的行为

时间:2018-10-14 11:37:36

标签: javascript jquery navigation scrolltop

我在我的网站上有一个导航,该导航由jQuery驱动,可以平滑滚动到目标ID,但是它的行为非常奇怪(Chrome,Firefox):

  • 有时它并不会完全移动到目标ID
  • 如果您再次单击目标,它将滚动回到标题

Here是一个gif,表明我的网站上存在这种奇怪的行为。


我制作了simplistic version个网站,以便您可以自己进行测试,在那里您可以体验相同的weird behavior

我尽可能地做到了相同。 HTML,类,ID和jQuery 完全相同。我还给导航设置了fixed的位置,就像我网站上的位置一样。

有问题的jQuery代码:

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

简而言之,导致这种奇怪行为的代码有什么问题?我是否为jQuery使用了错误的方法,如果是,是否有更好的方法?我不介意使用图书馆,但我不想。

3 个答案:

答案 0 :(得分:1)

这是工作版本。显然,这是由于overflow-x:hidden的CSS规则中的body造成的。参见this question

删除了该规则,现在看起来非常好(或者更喜欢下面的代码库,因为SO的整版版本可能会出现一些问题):

$("#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); }
  @media only 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; }
  @media only screen and (max-width: 25em) {
    .header-title {
      text-align: center;
      margin-left: 3px; } }

.header-sub {
  color: #262626;
  font-size: 2rem; }
  @media only 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; }

@media only 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: 1rem 0;
    transition: transform .1s ease-in-out; } }
  @media only screen and (min-width: 50em) and (max-width: 68.75em) {
    .navigation__link {
      font-size: 2rem; } }
  @media only 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; } }

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

@media only 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; } }

@media only 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.5rem 0;
    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.5rem 3rem;
    border-radius: 50px;
    background-image: linear-gradient(120deg, transparent 0%, transparent 50%, #fff 50%);
    background-size: 250%;
    transition: all ease-in-out .5s, transform ease-in-out .25s; } }
  @media only screen and (max-width: 50em) and (max-width: 37.5em) {
    .navigation__link:link, .navigation__link:visited {
      font-size: 2.5rem; } }
  @media only screen and (max-width: 50em) and (max-width: 25em) {
    .navigation__link:link, .navigation__link:visited {
      font-size: 2rem; } }

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

@media only 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: 0 6px 3rem rgba(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; }

@media only 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); }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- Header -->
<header class="header" id="header">
	<input class="navigation__checkbox" id="nav-toggle" type="checkbox">

	<label class="navigation__button" for="nav-toggle">
		<span class="navigation__icon"></span>
	</label>

	<div class="navigation__background"></div>

	<div class="company">
		<h2 class="header-title">Company</h2>
		<h3 class="header-sub">Hambuger menu shown under 800px width</h3>
	</div>

	<nav class="navigation__nav">
		<ul class="navigation__list">
			<li class="navigation__item"><a class="navigation__link scroll" id="scrollOne" href="#one">One</a></li>
			<li class="navigation__item"><a class="navigation__link scroll" id="scrollTwo" href="#two">Two</a></li>
			<li class="navigation__item"><a class="navigation__link scroll" id="scrollThree" href="#three">Three</a></li>
			<li class="navigation__item"><a class="navigation__link scroll" id="scrollFour" href="#four">Four</a></li>
		</ul>
	</nav>
</header>

<!-- About -->
<section class="section-one" id="one">
	<h2 class="header-title">One</h2>
</section>

<!-- Quote -->
<section class="section-two" id="two">
	<h2 class="header-title">Two</h2>
</section>

<!-- Reviews -->
<section class="section-three" id="three">
	<h2 class="header-title">Three</h2>
</section>

<!-- Footer -->
<section class="section-four" id="four">
	<h2 class="header-title">Four</h2>
</section>

Codepen version

不相关:我还建议在每个.stop()调用之前添加.animate(),以便每个新动画都停止上一个动画。

答案 1 :(得分:0)

offset().top值在页面滚动时改变。因此,您需要在开始时保存这些值,并且还应在窗口大小更改时重新分配这些值。

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

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

答案 2 :(得分:0)

问题是您在height:100%上有body,因此偏移量返回错误的值(正确,但不是您期望的值)。

height:automin-height:100%赋予body,它将可以正常工作。

查看更新的笔:https://codepen.io/anon/pen/qJPPeL?editors=1010