简体   繁体   English

jQuery的动画速度不改变

[英]jquery animate speed not changing

I am using a button click event to scroll down a whole page. 我正在使用按钮单击事件来向下滚动整个页面。 As I changed the duration parameter of jquery animate() function, the speed didn't seem to change. 当我更改了jQuery animate()函数的duration参数时,速度似乎没有变化。 What did I do wrong? 我做错了什么?

Here is my code: 这是我的代码:

    $("#arrowdown").click (function(){
        $("html, body").animate(
        {
            scrollTop: $("#second-page").offset().top
        }, 
        100//no matter how I changed here, the speed haven't change at all.
        );
    })

I wrote some code very similar to this a while back, and while you didn't say the #arrowdown item was a link, I'm betting it is and its pointing to #second-page (href = '#second-page') which if that's the case its jumping right down the page, no animation at all. 不久前,我写了一些与此代码非常相似的代码,虽然您没有说#arrowdown项目是一个链接,但我敢打赌它是它的,并且它指向#second-page(href ='#second-page' )(如果是这种情况,则它会在页面上跳下,根本没有动画。

Try this: 尝试这个:

$("#arrowdown").click (function(e){
    e.preventDefault();
    $("html, body").animate(
    {
        scrollTop: $("#second-page").offset().top
    }, 
    1000
    );
})

The big changes are the function(e) and e.preventDefault(); 最大的变化是function(e)e.preventDefault(); which will prevent the default action from overriding what you want to happen. 这将防止默认操作覆盖您要发生的事情。

Ok One reason maybe your text is not long enough, I have proramatically assigned a select dropdown select a speed and then click down arrow it takes up the speed as desired 好的,原因可能是您的文本不够长,我按比例分配了一个选择下拉列表,然后选择一个速度,然后单击向下箭头,它会按照需要占用速度

 $('select').on('change', function(){ $("html, body").scrollTop(0); var v = parseInt($(this).val()); $("#arrowdown").off('click').on('click', function(){ $("html,body").animate( {'scrollTop': $("#second-page").offset().top}, v ); }); }).trigger('change'); //just to make text long //ignore var x = $('div.dum').text(); for(var i=0; i<5; i++){ x+=x; }$('div.dum').text(x) 
 #botton{ width:200px;height:40px; background:#ccc; position:fixed; bottom:0; right:0; padding:5px; } #botton > Button, #botton > select{float:left;} 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class=dum> "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum." </div> <span id="second-page">Hello second</span> <div id=botton> Select a speed in seconds<select> <option value=100>100</option> <option value=500>500</option> <option value=1000>1000</option> <option value=2000>2000</option> </select> <button id="arrowdown">Click Down Arrow</button> </div> 

100=0.1 second, try 811 and/or {scrollTop:$(document).height()} 100 = 0.1秒,请尝试811和/或{scrollTop:$(document).height()}

$("#arrowdown").click (function(){
    $("html, body").animate(
    {
        scrollTop:$(document).height()
    }, 
    811
    );
})

try below code - 尝试下面的代码-

$("#arrowdown").click (function(){
        $("html body").animate(
        {
            scrollTop: $("#second-page").offset().top
        }, 
        100//no matter how I changed here, the speed haven't change at all.
        );
    })

You dont neet to put comma between html body . 您不必在html body之间加上逗号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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