S
S
Sherbatiy2015-09-07 23:24:28
JavaScript
Sherbatiy, 2015-09-07 23:24:28

jQuery smooth scrolling - what's wrong with me?

Hello!
I can't figure out why smooth scrolling doesn't work. I don’t understand js, but I found a ready-made code, it is quite popular with search engines.
Having done everything according to the instructions, I got only an html anchor - it goes to the right place without animation. ..index.php#g-feature is added to the address bar. I
assume that the problem is in the script itself, but I don’t know what exactly needs to be changed.

I include the script between the head tags:

<script type="text/javascript">
$(document).ready(function() {
   $('a[href^="#"]').click(function () { 
     elementClick = $(this).attr("href");
     destination = $(elementClick).offset().top;
     if($.browser.safari){
       $('body').animate( { scrollTop: destination }, 1100 );
     }else{
       $('html').animate( { scrollTop: destination }, 1100 );
     }
     return false;
   });
 });
</script>


Next, the button-link to the anchor:

<a class="g-menu-item-container" href="#g-feature">
<span class="g-menu-item-content">
<span class="g-menu-item-title">Преимущества</span>
</span>
</a>


And the anchor itself:

<section id="g-feature">
....
</section>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
0
0leg5ergeev, 2015-09-07
@Sherbatiy

$(document).ready(function(){
         	$('a[href*=#]').bind("click", function(e){
         		var anchor = $(this);
         		$('html, body').stop().animate({
         			scrollTop: $(anchor.attr('href')).offset().top
         		}, 1000);
         		e.preventDefault();
         	});
         	return false;
         });

S
Sherbatiy, 2015-09-08
@Sherbatiy

Thank you very much! Corrected the script, everything works.
And yet, I did not connect jQuery itself - after connecting it worked.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question