jQuery easing not working in my animate() call

青春壹個敷衍的年華 提交于 2019-12-24 05:31:15

问题


I have 4 circular buttons located towards the central area on my page. Hovering one of them makes it grow in size, but I want to add some easing/bouncing effect to both the growing and shrinking movements of these buttons.

However for some reason the easing part doesn't work. I did add the easing plugin to my page:

<script src='js/jquery.easing.1.3.js'></script> 

Here's the code for the buttons' behaviour:

$('.egg_button')
    .on('mouseenter', function(){
        var div = $(this);
        div.stop(true, true).animate({ 
            margin: -5,
            width: "+=10",
            height: "+=10",
            backgroundSize: "30px",
            specialEasing: {
                width: "easeOutBounce",
                height: "easeOutBounce"
            }
        }, 'fast');
    })
    .on('mouseleave', function(){
        var div = $(this);
        div.stop(true, true).animate({ 
            margin: 0,
            width: "-=10",
            height: "-=10",
            backgroundSize: "22px",
            specialEasing: {
                width: "easeOutBounce",
                height: "easeOutBounce"
            }
        }, 'fast');
    })

回答1:


You put the easing: specifier in the wrong position. It should be like this:

$(document).ready(function(){
    $(".egg_button").hover(
        function() {
            var div = $(this);
            div.stop(true, true).animate({
                margin: -5,
                width: "+=10",
                height: "+=10",
                backgroundSize: "30px",         // Instead of here ..
            }, {
                duration: 500, 
                queue:false, 
                easing: 'easeOutBounce'         // .. put it here
            });
        },
        function() {
            var div = $(this);
            div.stop(true, true).animate({
                margin: 0,
                width: "-=10",
                height: "-=10",
                backgroundSize: "22px",        // Instead of here ..
            }, {
                duration: 500, 
                queue:false, 
                easing: 'easeOutBounce'        // .. put it here
            });
        }
    );
});

Here is a jsFiddle example I prepared for you so you can tweak the settings to your liking:

DEMO

And don't forget to check out this easing cheatsheet which gives you a better impression of what each easing function exactly does. Good luck!



来源:https://stackoverflow.com/questions/26982313/jquery-easing-not-working-in-my-animate-call

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!