0

I am very new to JQuery and I can make the page go to the top and bottom like I want to slow the movement down a bit. So how to I slow the scroll speed down? This is what I have so far:

document.getElementById('sur2').onclick = function () {
    document.getElementById('pt1').style.display='block';
    document.getElementById('pt2').style.display='none';
    window.scroll(0,0);
}


document.getElementById('sur1').onclick = function () {
    document.getElementById('pt1').style.display='none';
    document.getElementById('pt2').style.display='block';
    window.scroll(0,5000); 
}
farmerm3
  • 341
  • 1
  • 4
  • 13
  • 2
    That's plain javascript, no jQuery. :) jQuery has specific methods which will allow you to do it using less code, but it is good to understand what's working under the hood, like you are, though. – Fergus In London Oct 10 '13 at 17:12
  • Thanks for correcting me I am still confused no matter how much I've read. I guess my question is how do I add the slower scrolling to what I have now? – farmerm3 Oct 10 '13 at 17:52

1 Answers1

1

If you are planning to add jQuery then this answer will be of some use, because you can specify how far from the top you want to scroll, i believe.

Slow down scroll to top event by jQuery animate

$('a[href=#top]').click(function(){
    $('html, body').animate({scrollTop:0}, 'slow');
});

Take a look at the jQuery API at http://api.jquery.com/animate/

There is also this scrollTo plugin

http://flesler.com/jquery/scrollTo/

UPDATE:

You can just use jQuery because adding your own animations is going to be more work.

Try this jsfiddle http://jsfiddle.net/eVJvH/2/, I know the animate scroll works but inside the frame on the fiddle its not, try it yourself on your own page and see if it works, where the word "slow" is used, you can adjust this to different values and numbers to control how slow you want it to be, look on the animate jquery api page for more information on how to use it:

html:

<div id="sur1" class="red block"></div>
<div id="sur2" class="blue block"></div>
<div id="pt1" class="green block hide"></div>
<div id="pt2" class="yellow block hide"></div>

css:

.block {
    height: 50px;
    width: 50px;
}

.red {
    background: red;
}

.blue {
    background: blue;
}

.green {
    background: green;
}

.yellow {
    background: yellow;
}

.hide {
    display: none;
}

javascript:

$(function() {
    $("#sur2").on("click", function () {
        $("#pt1").toggleClass("hide");
        $("#pt2").addClass("hide");
        $("html, body").animate({scrollTop: "0px" }, "slow");
    });


    $("#sur1").on("click", function () {
        $("#pt2").toggleClass("hide");
        $("#pt1").addClass("hide");
        $("html, body").animate({scrollTop: "5000px" }, "slow");
    });
});
Community
  • 1
  • 1
Pricey
  • 5,799
  • 12
  • 60
  • 84
  • Thanks for correcting me I am still confused no matter how much I've read. I guess my question is how do I add the slower scrolling to what I have now? – farmerm3 Oct 10 '13 at 17:54