0

Hi everyone i want make a nav with scroll but i have this error:

Uncaught SyntaxError: Unexpected number

 $('.nav-link').on('click',function(e){
            e.preventDefault();
            scrollTo($(this).attr('href') 200 );
        })


  });
Mark
  • 21
  • 3

2 Answers2

0

Correct syntax of scrollTo is:

window.scrollTo(xpos,ypos)

Your code should be:

 $('.nav-link').on('click',function(e){
            e.preventDefault();
            scrollTo(300, 200 ); // as per correct syntax

        }); 

But, if you want to scroll to any element, read this answer: jQuery scroll to element

Also, as you have mentioned that you are using scrollTo.js plugin, Your code should be:

   $('.nav-link').on('click',function(e){
                e.preventDefault();
                $('#<ID_OF_ELEMENT>').ScrollTo();     

            }); 
Community
  • 1
  • 1
Apul Gupta
  • 2,967
  • 3
  • 19
  • 29
0

You seem to be trying to use jquery.scrollTo albeit incorrectly.

First make you sure include jQuery (1.8 or above) and also jquery.scrollTo (2.1.0), something like this:

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="//cdn.jsdelivr.net/jquery.scrollto/2.1.0/jquery.scrollTo.min.js"></script>

then add this:

<script type="text/javascript">
$(document).ready(function() {
  $('.nav-link').click(function(e) {
    e.preventDefault();
    $(window).stop(true).scrollTo(this.hash, {duration:200, interrupt:true});
  });
});
</script>

That works. 200 seems too fast but you can tweak it to your liking.

Ariel Flesler
  • 612
  • 1
  • 4
  • 13