简体   繁体   English

平滑滚动到

[英]Smooth scroll to

what I'm trying to is by clicking a button it will scroll smoothly down to the next section, without changing the background image. 我要尝试的是单击一个按钮,它可以平滑地向下滚动到下一部分,而无需更改背景图像。 Similar to this; 与此类似; http://www.dada-data.net/en/ http://www.dada-data.net/en/

I've been able to make a click event that scrolls to a new page, but can't seem to get the sections to scroll up on the same background. 我已经能够创建一个可滚动到新页面的click事件,但是似乎无法使这些部分在同一背景上向上滚动。

Thanks in advance for any help! 在此先感谢您的帮助!

HTML 的HTML

<section id="section01" class="demo">

  <div class="hero">
    <img src="/Users/jbeez/desktop/portfolio/JMB_B.png" class="jmb">
  </div>

  <a href="section02"><span></span>Click</a>

</section>

<!-- end of section 1 -->

<section id="section02" class="demo">
  <div>
    <h1>Hello world</h1>
  </div>
</section>

JAVASCRIPT JAVASCRIPT

$(function() {
  $('a[href*=#]').on('click', function(e) {
    e.preventDefault();
    $('html, body').animate({ scrollTop: $($(this).attr('href')).offset().top}, 500, 'linear');
  });
});

CSS 的CSS

 #section01 { background: url(/Users/jbeez/desktop/portfolio/background.png) center center / cover no-repeat;}
    #section02 { background: url(/Users/jbeez/desktop/portfolio/background.png) center center / cover no-repeat;}
    #section03 { background: url(/Users/jbeez/desktop/portfolio/background.png) center center / cover no-repeat;}

.demo a {
      position: absolute;
      bottom: 20px;
      left: 50%;
      z-index: 2;
      display: inline-block;
      -webkit-transform: translate(0, -50%);
      transform: translate(0, -50%);
      color: #fff;
      font : normal 400 20px/1 'Josefin Sans', sans-serif;
      letter-spacing: .1em;
      text-decoration: none;
      transition: opacity .3s;
    }
    .demo a:hover {
      opacity: .5;
    }

    #section01 a {
      padding-top: 60px;
    }
    #section01 a span {
      position: absolute;
      top: 0;
      left: 50%;
      width: 24px;
      height: 24px;
      margin-left: -12px;
      border-left: 1px solid #fff;
      border-bottom: 1px solid #fff;
      -webkit-transform: rotate(-45deg);
      transform: rotate(-45deg);
      box-sizing: border-box;
    }



    #section02 a {
      padding-top: 60px;
    }
    #section02 a span {
      position: absolute;
      top: 0;
      left: 50%;
      width: 46px;
      height: 46px;
      margin-left: -23px;
      border: 1px solid #fff;
      border-radius: 100%;
      box-sizing: border-box;
    }
    #section02 a span::after {
      position: absolute;
      top: 50%;
      left: 50%;
      content: '';
      width: 16px;
      height: 16px;
      margin: -12px 0 0 -8px;
      border-left: 1px solid #fff;
      border-bottom: 1px solid #fff;
      -webkit-transform: rotate(-45deg);
      transform: rotate(-45deg);
      box-sizing: border-box;
    }

Try to change your href, like this : 尝试更改您的href,如下所示:

<a href="#section02"><span></span>Click</a>

$(".your element").click(function() {
    $('html, body').animate({
        scrollTop: $(this).attr("href").offset().top
    }, 2000);
});

尝试使用哈希:

  <a href="#section02"><span></span>Click</a>

Like the others are saying... The # is terribly missing. 就像其他人在说... #严重缺失。

<a href="#section02"><span></span>Click</a>

Then, in the attribute selector you used, there is some quotes missing around this # you're trying to target. 然后,在您使用的属性选择器中,您试图定位的#周围缺少一些引号。

$("a[href*='#']")

CodePen of the code fixed. 固定代码的CodePen

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

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