首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >停止中间位置各部分之间的光滑涡旋

停止中间位置各部分之间的光滑涡旋
EN

Stack Overflow用户
提问于 2018-09-21 11:22:11
回答 3查看 589关注 0票数 1

我有以下结构化代码。我有粘性的标题,以及我的页面中的几个部分。我这里很少有页面超链接。

正如您在片段中看到的,在第1节到第2节中有一个链接文本。

我从w3school中添加了一些jquery来平滑滚动。

问题

当单击超链接时,它滚动到第2节,并将第2节的起始点移到主体的顶部。因为我有一个粘性的标题,所以它隐藏了第2节的一些内容。

现在我想要的是:当滚动到第2节时,我希望这个部分在粘稠的标题之后开始,而不是从主体的顶部开始。

代码语言:javascript
复制
// Add smooth scrolling to all links
$("a").on('click', function(event) {

  // Make sure this.hash has a value before overriding default behavior
  if (this.hash !== "") {
    // Prevent default anchor click behavior
    event.preventDefault();

    // Store hash
    var hash = this.hash;

    // Using jQuery's animate() method to add smooth page scroll
    // The optional number (800) specifies the number of milliseconds it takes to scroll to the specified area
    $('html, body').animate({
      scrollTop: $(hash).offset().top
    }, 800, function() {

      // Add hash (#) to URL when done scrolling (default click behavior)
      window.location.hash = hash;
    });
  } // End if
});
代码语言:javascript
复制
header {
  background: red;
  width: 100%;
  height: 50px;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
}

#sec-1,
#sec-2 {
  width: 100%;
  height: 100vh;
}

#sec-1 {
  margin-top: 50px;
  background: green;
}

#sec-2 {
  background: blue;
}

#sec-2>p {
  background: #fff;
  margin: auto;
  width: 60%;
  height: 100px;
}
代码语言:javascript
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<header></header>

<section id="sec-1">
  <a href="#sec-2">Scroll to section 2</a>
</section>
<section id="sec-2">
  <p>This is section 2</p>
</section>

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-09-21 11:58:11

您的问题是javascript中的最后一行。

/ Add hash (#) to URL when done scrolling (default click behavior) window.location.hash = hash;

这实际上迫使您的URL跳转到原来的散列位置。您实际上并没有添加一个#,但是您强制窗口跳转到javascript变量hash (在本例中是section-2 )中定义的原始窗口。

代码语言:javascript
复制
// Add smooth scrolling to all links
$("a").on('click', function(event) {

  // Make sure this.hash has a value before overriding default behavior
  if (this.hash !== "") {
    // Prevent default anchor click behavior
    event.preventDefault();

    // Store hash
    var hash = this.hash;

    // Using jQuery's animate() method to add smooth page scroll
    // The optional number (800) specifies the number of milliseconds it takes to scroll to the specified area
    $('html, body').animate({
      scrollTop: $(hash).offset().top -70
    }, 800, function() {

    });
  } // End if
});

卷轴现在按预期工作。

https://jsfiddle.net/exuj6mro/18/

票数 5
EN

Stack Overflow用户

发布于 2018-09-21 11:57:56

你需要从卷轴中移除标题的高度;

代码语言:javascript
复制
$('html, body').animate({
  scrollTop: $(hash).offset().top - $('header').height()
}, 800, function() {

  // Add hash (#) to URL when done scrolling (default click behavior)
  window.location.hash = hash;
});
票数 1
EN

Stack Overflow用户

发布于 2018-09-21 12:03:18

首先,您需要从动画块内的scrollTop偏移量中减去标头的高度。

其次,当您使用window.location.hash时,它会引起实际的麻烦,当window.location.hash被触发时,页面再次滚动以寻找超链接(就像传统的超链接行为一样)。贝娄代码正在正常工作,希望它能解决你的问题。

代码语言:javascript
复制
// Add smooth scrolling to all links
$("a").on('click', function(event) {

  // Make sure this.hash has a value before overriding default behavior
  if (this.hash !== "") {
    // Prevent default anchor click behavior
    event.preventDefault();

    // Store hash
    var hash = this.hash;

    // Using jQuery's animate() method to add smooth page scroll
    // The optional number (800) specifies the number of milliseconds it takes to scroll to the specified area
    $('html, body').animate({
      scrollTop: parseInt($(hash).offset().top - parseInt($('header').height()))
    }, 800, function() {

      // Add hash (#) to URL when done scrolling (default click behavior)
      // window.location.hash = hash;
    });
  } // End if
});
代码语言:javascript
复制
header {
  background: red;
  width: 100%;
  height: 50px;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
}

#sec-1,
#sec-2 {
  width: 100%;
  height: 100vh;
}

#sec-1 {
  margin-top: 50px;
  background: green;
}

#sec-2 {
  background: blue;
}

#sec-2>p {
  background: #fff;
  margin: auto;
  width: 60%;
  height: 100px;
}
代码语言:javascript
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<header></header>

<section id="sec-1">
  <a href="#sec-2">Scroll to section 2</a>
</section>
<section id="sec-2">
  <p>This is section 2</p>
</section>

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52442661

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档