Rumah  >  Artikel  >  hujung hadapan web  >  Jquery的animate()反应过慢的问题解决

Jquery的animate()反应过慢的问题解决

巴扎黑
巴扎黑asal
2017-06-30 11:22:352121semak imbas

一张网页中少不了回到顶部的按钮,点击页面会自动回到网页顶部,可以使用超链接的方式,但是瞬间就回到顶部的效果实在不太理想,比较好的效果是向上滚动持续大约0.5秒,这就需要用到动画效果了。

我选择使用jquery的animate()方法,回到顶部效果完美实现,但是另外的一个效果却不太理想。

我希望回到顶部按钮的高度在小于屏幕高度时消失,在大于其高度时在显现出来,使用 css  属性 ‘opacity’来控制,但是每一次都是往上拉到顶部了按钮还是不消失,好不容易消失了再拉到底部按钮又不出现,代码如下,css代码就不列出来了:

<!--index.html-->
    <p class="sideBar">
        <p class="goToTop"></p>
    </p>
//回到顶部
    $(".goToTop").on("click",function(){
        $("html,body").animate({scrollTop:0},&#39;slow&#39;);
    });// 控制按钮是否消失
    $(function(){
        var windowHeight = window.innerHeight;        var $goToTop;
        $(window).scroll(function(){
            $goToTop = $(".goToTop");           if(windowHeight < $goToTop.offset().top){
            $goToTop.animate({opacity : 1},200);
        }else{
            $goToTop.animate({opacity : 0},200);
            }
        });
    });

一直有问题,代码查了三四遍都没发现问题,但是效果就是出错,后来实在没办法去google了一下,一下子就找到了答案

  • StackOverflow:Jquery slow reaction time

well.. you are calling the scroll listener which occurs evry moment while you are scrolling. but you also play an animation which is relatevly slow to scroll. when you call the scroll listener by scrolling, you create multiple nimations calls which try to play all at once (and that is why it slows down the ui).

就是说,鼠标滚动一次就引发一次animate()方法的执行,而每次执行是需要时间的,对于我的代码是一次执行0.2s,虽然一次时间不长,但是好多好多次叠加在一起就造成了执行不同animate()方法的延时

  • 解决方法

One solution would be to call .stop() before you call .animate

也就是这样修改animate()方法:

    //在animate()方法之前添加stop()函数
    $goToTop.stop().animate({opacity : 1},200);

问题解决!!!

一张网页中少不了回到顶部的按钮,点击页面会自动回到网页顶部,可以使用超链接的方式,但是瞬间就回到顶部的效果实在不太理想,比较好的效果是向上滚动持续大约0.5秒,这就需要用到动画效果了。

我选择使用jquery的animate()方法,回到顶部效果完美实现,但是另外的一个效果却不太理想。

我希望回到顶部按钮的高度在小于屏幕高度时消失,在大于其高度时在显现出来,使用 css  属性 ‘opacity’来控制,但是每一次都是往上拉到顶部了按钮还是不消失,好不容易消失了再拉到底部按钮又不出现,代码如下,css代码就不列出来了:

<!--index.html-->
    <p class="sideBar">
        <p class="goToTop"></p>
    </p>
//回到顶部
    $(".goToTop").on("click",function(){
        $("html,body").animate({scrollTop:0},&#39;slow&#39;);
    });// 控制按钮是否消失
    $(function(){
        var windowHeight = window.innerHeight;        var $goToTop;
        $(window).scroll(function(){
            $goToTop = $(".goToTop");           if(windowHeight < $goToTop.offset().top){
            $goToTop.animate({opacity : 1},200);
        }else{
            $goToTop.animate({opacity : 0},200);
            }
        });
    });

一直有问题,代码查了三四遍都没发现问题,但是效果就是出错,后来实在没办法去google了一下,一下子就找到了答案

  • StackOverflow:Jquery slow reaction time

well.. you are calling the scroll listener which occurs evry moment while you are scrolling. but you also play an animation which is relatevly slow to scroll. when you call the scroll listener by scrolling, you create multiple nimations calls which try to play all at once (and that is why it slows down the ui).

就是说,鼠标滚动一次就引发一次animate()方法的执行,而每次执行是需要时间的,对于我的代码是一次执行0.2s,虽然一次时间不长,但是好多好多次叠加在一起就造成了执行不同animate()方法的延时

  • 解决方法

One solution would be to call .stop() before you call .animate

也就是这样修改animate()方法:

    //在animate()方法之前添加stop()函数
    $goToTop.stop().animate({opacity : 1},200);

问题解决!!!

Atas ialah kandungan terperinci Jquery的animate()反应过慢的问题解决. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel sebelumnya:jQuery.animate() 函数使用实例详解Artikel seterusnya:jquery之stop()的用