Scrolling is not working

Asked

Viewed 46 times

0

I have a problem on a site I’m creating, the scrolling effect I did on Js is not working properly, I put a console.log() to know if at least the event is running and is, the only problem is that scroll does not happen. If you can help me graduate.

MENU

<ul class="navbar-menu">
   <li><a class="scrolling" href="#home">MENU 1</a></li>
   <li><a class="scrolling" href="#portfolio">MENU 2</a></li>
   <li><a class="scrolling" href="#videos">MENU 3</a></li>
   <li><a class="scrolling" href="#contact">MENU 4</a></li>
</ul>

SCRIPT

<script type="text/javascript">
   $(document).ready(function () {
       $('.scrolling').click(function (e) {
       var linkHref = $(this).attr('href');
       console.log($(linkHref).offset().top);
       $('html, body').animate({
           scrollTop: $(linkHref).offset().top
       }, 1000);
       e.preventDefault();
      });
  });

P.S.: My version of jquery is 3.1.1.

If you need more information please let me know.

Hug.

  • Post the rest of the html please

  • I played your code on this one fiddle Is working correctly. Make sure you have set the ids correctly in the elements you want to achieve.

1 answer

0


You probably changed the oveflow:; of body, html or added a new overflow to an element within the body, see that your code works well here:

$(document).ready(function () {
    $('.scrolling').click(function (e) {
        var linkHref = $(this).attr('href');

        $('html, body').animate({
            scrollTop: $(linkHref).offset().top
        }, 1000);

        e.preventDefault();
    });
});
.box {
    margin: 10px;
    height: 240px;
    background: #f00;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<ul class="navbar-menu">
    <li><a class="scrolling" href="#home">MENU 1</a></li>
    <li><a class="scrolling" href="#portfolio">MENU 2</a></li>
    <li><a class="scrolling" href="#videos">MENU 3</a></li>
    <li><a class="scrolling" href="#contact">MENU 4</a></li>
</ul>

<div id="home" class="box"></div>
<div id="portfolio" class="box"></div>
<div id="videos" class="box"></div>
<div id="contact" class="box"></div>

If an element inside body has a scroll of its own you must change the html,body selector, like this:

$(document).ready(function () {
    $('.scrolling').click(function (e) {
        var linkHref = $(this).attr('href');

        $('#main').animate({ //TROCADO
            scrollTop: $(linkHref).offset().top
        }, 1000);

        e.preventDefault();
    });
});
.box {
    margin: 10px;
    height: 240px;
    background: #00f;
}
html, body {
   margin: 0;
   padding: 0;
   height: 100%;
   overflow: hidden;
}
#main {
   height: 100%;
   overflow: auto;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="main">
    <ul class="navbar-menu">
        <li><a class="scrolling" href="#home">MENU 1</a></li>
        <li><a class="scrolling" href="#portfolio">MENU 2</a></li>
        <li><a class="scrolling" href="#videos">MENU 3</a></li>
        <li><a class="scrolling" href="#contact">MENU 4</a></li>
    </ul>

    <div id="home" class="box"></div>
    <div id="portfolio" class="box"></div>
    <div id="videos" class="box"></div>
    <div id="contact" class="box"></div>
</div>

  • 1

    Good afternoon William. You are absolutely correct I added (I don’t know why an overflow:auto no body/html). I removed this and everything is fine now! Thanks again buddy, hug!

  • @Jhonathansilva for nothing ;)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.