Scroll with Jquery

I am creating a website, and I want to make a scroll effect on the pages where when clicking on the menu item the page scrolls to where the related information is.

Would be more or less equal to this, but a little simpler just with the same scroll.

Could anyone help me?

Author: Felipe Avelar, 2014-05-26

4 answers

You need to have in the menu some indication of the target for the scroll.

Can be the id of the element stored in a property data, an href/ancora, or another. If you use an anchor (<a>) you may need to use .preventDefault() to prevent the link from being followed (in some cases you even want the link to stay in the url, so you shouldn't use it).

After you have to intercept the click you can use jQuery .anymate () to scroll, fetching the position of this element as target.

HTML example

<div class="menu">
    <div data-destino="parte1">Parte 1</div>
    <div data-destino="parte2">Parte 2</div>
</div>
<div id="parte1"></div>
<div id="parte2"></div>

JQuery example

$('.menu div').on('click', function () {
    var destino = '#' + $(this).data('destino');
    var posicaoDestino = $(destino).position().top;
    $('html, body').stop().animate({
        scrollTop: posicaoDestino
    }, 2000);
});

Example: http://jsfiddle.net/zc966/

 5
Author: Sergio, 2014-05-26 18:31:01

Using the href of the links:

<a href="#Id_da_div"> link </a>

Ex:

<a href="#contato">Fale Conosco</a>
<script>
$('a[href*=#]').click(function() {
    var destino = $(this).attr('href');
    $("html, body").animate({scrollTop: $(destino).position().top}, 600);
    return false;
});
</script>

To change the speed of the scroll, change the last parameter from 600 to the desired value. The value is in milliseconds, or "slow", "mid", "fast".

 1
Author: Wpbarcelos, 2014-05-26 19:58:30

Hi, you can use scrollTop in JQuery.

Try This (working)

$("#elemento1").click(function(){
    $('html, body').animate({ 'scrollTop' : $("#elemento2").position().top }, 1);
});
 0
Author: Alan PS, 2014-05-26 18:39:22

You can use ANCHOR.

Link to be clicked

<a href="#vemPraCa">Clique aqui para dar Scroll</a>

Place where the Scroll goes

<a name="vemPraCa"></a>
 0
Author: PauloHDSousa, 2016-05-19 11:10:38