简体   繁体   中英

I am trying to make my page scroll slower when you press a button using Jquery?

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); 
}

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");
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM