最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Jumping to sections of the page using only Jquery - Stack Overflow

programmeradmin2浏览0评论

I've just started to code, and am learning all there is to know about JQuery. I have a navbar with 'About' and 'What is this' anchors. The specific content for the respective anchors are in different sections of the page. If I click an anchor, say 'About', I want to jump to the 'About-content' section of the webpage. I have read of plugins which do this, but is there any way to do this simply using JQuery? I tried doing what was told in an answer to a similar question but it doesn't work.

HTML

<ul class="nav navbar-nav">
  <li class="About"> <a href="#"> About </a></li>
  <li class="How-it-works"> <a href="#"> How it works </a></li>

</ul>
......
......

<div class="About-content">
  <div class="container">
    <div class="row">
      <div class="col-xs-12">
        <p> text text text </p>
      </div>
    </div>
  </div>
</div>

JQuery

$(document).ready(function () {
    $('.About').click(function (event) {
        event.preventDefault();

        $('body, html').animate({
            scrollTop: $(".About-content").offset.top
        }, 600);

    })
}

I've just started to code, and am learning all there is to know about JQuery. I have a navbar with 'About' and 'What is this' anchors. The specific content for the respective anchors are in different sections of the page. If I click an anchor, say 'About', I want to jump to the 'About-content' section of the webpage. I have read of plugins which do this, but is there any way to do this simply using JQuery? I tried doing what was told in an answer to a similar question but it doesn't work.

HTML

<ul class="nav navbar-nav">
  <li class="About"> <a href="#"> About </a></li>
  <li class="How-it-works"> <a href="#"> How it works </a></li>

</ul>
......
......

<div class="About-content">
  <div class="container">
    <div class="row">
      <div class="col-xs-12">
        <p> text text text </p>
      </div>
    </div>
  </div>
</div>

JQuery

$(document).ready(function () {
    $('.About').click(function (event) {
        event.preventDefault();

        $('body, html').animate({
            scrollTop: $(".About-content").offset.top
        }, 600);

    })
}
Share Improve this question edited Mar 2, 2016 at 5:07 Sender 6,85812 gold badges49 silver badges69 bronze badges asked Mar 2, 2016 at 4:35 keshinpointkeshinpoint 1,0013 gold badges14 silver badges25 bronze badges 2
  • Define "it doesn't work," how does it not work? What goes wrong? Are there any error messages in your browser console? – David Thomas Commented Mar 2, 2016 at 4:38
  • 1 jsfiddle/lollero/ZVdRt – Anandh Sp Commented Mar 2, 2016 at 4:43
Add a ment  | 

4 Answers 4

Reset to default 2

Use the id attribute to your target div:

<div id="About-content">
    <div class="container">
        <div class="row">
            <div class="col-xs-12">
                <p>text text text</p>
            </div>
        </div>
    </div>
</div>

Set Nav link to the Div id:

<ul class="nav navbar-nav">
    <li class="About"> <a href="#About-content"> About </a>
    </li>
    <li class="How-it-works"> <a href="#"> How it works </a>
    </li>
</ul>

Use the following simple jquery for smooth scroll:

$(document).ready(function() {
    $(".About a").click(function() {
        $('html, body').animate({
            scrollTop: $("#About-content").offset().top
        }, 2000);
    });
});
  • Change your target .About-content to an #About-content
  • Change the href to #About-content
  • Remove e.preventDefault()

It should function without jQ/JS but if you add easing to your animation then it'd be useful.

$(document).ready(function() {
  $('.About').click(function(event) {
    $('body, html').animate({
      scrollTop: $("#About-content").offset().top
    }, 600);

  });
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="nav navbar-nav">
  <li class="About"> <a href="#About-content"> About </a>
  </li>
  <li class="How-it-works"> <a href="#"> How it works </a>
  </li>

</ul>
<div style="height: 1000px;"></div>
<div id="About-content">
  <div class="container">
    <div class="row">
      <div class="col-xs-12">
        <p>text text text</p>
      </div>
    </div>
  </div>
</div>

Say you have an Nav like this ,

<ul class="nav navbar-nav">
                    <li class="About"> <a href="#About-content"> About </a></li>
                    <li class="How-it-works"> <a href="#HowItWorks"> How it works </a> </li>   
</ul>

And assuming a div like what you have,

Some Content Here
  $(document).ready(function(){     
      $(".navbar-nav a").on('click', function(event) {  
      event.preventDefault(); 
      var hash = this.hash;  
      $('html, body').animate({
         scrollTop: $(hash).offset().top
      }, 1000, function(){
         window.location.hash = hash;
        });
      });
    })

Will animate to the respective areas. Here hash holds the values after # from your <a>tags.

IMHO, its better to bind the click event on the .nav instead of binding with separate <a>'s.

Well i don't know about jquery. but you can do it more simply by bootstrap. Like this. It is called bootstrap scrollspy. If you like the answer please rate it. :)

<meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="http://maxcdn.bootstrapcdn./bootstrap/3.3.6/css/bootstrap.min.css">
  <script src="https://ajax.googleapis./ajax/libs/jquery/1.12.0/jquery.min.js"></script>
  <script src="http://maxcdn.bootstrapcdn./bootstrap/3.3.6/js/bootstrap.min.js"></script>
 
  body {
      position: relative; 
  }
  #section1 {padding-top:50px;height:500px;color: #fff; background-color: #1E88E5;}
  #section2 {padding-top:50px;height:500px;color: #fff; background-color: #673ab7;}
  #section3 {padding-top:50px;height:500px;color: #fff; background-color: #ff9800;}
  #section41 {padding-top:50px;height:500px;color: #fff; background-color: #00bcd4;}
  #section42 {padding-top:50px;height:500px;color: #fff; background-color: #009688;}
  <div class="container-fluid">
    <div class="navbar-header">

    
    </div>
    <div>
      <div class="collapse navbar-collapse" id="myNavbar">
        <ul class="nav navbar-nav">
          <li><a href="#section1">Section 1</a></li>
          <li><a href="#section2">Section 2</a></li>
          <li><a href="#section3">Section 3</a></li>

           
     
    
  
    

<div id="section1" class="container-fluid">
  <h1>Section 1</h1>
 
</div>
<div id="section2" class="container-fluid">
  <h1>Section 2</h1>

</div>
<div id="section3" class="container-fluid">
  <h1>Section 3</h1>
 
</div>

发布评论

评论列表(0)

  1. 暂无评论