I am trying to make my page scroll slower when you

2019-09-09 05:03发布

问题:

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

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