用jQuery动画编写匿名函数 [英] Writing anonymous function in jQuery animation

查看:64
本文介绍了用jQuery动画编写匿名函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助我在动画中编写匿名函数,我已经给出了尝试过的代码,这是 DEMO ,我想知道如何在动画中编写匿名函数,就像我在Css按钮单击中所做的那样,在此先感谢

Please help me to write the anonymous function in animation, I have given the code which I have tried, Here is the DEMO, I want to know how to write the anonymous function in animation like what I did in the Css button click, thank in advance

<html>
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> 
        <style type="text/css">
           #example{width:100px;height:100px;background: #F00;}
        </style>
        <script>
            $(document).ready(function(){
                $('#check').click(function(){
                    $('#example').css({width:function(){
                            console.log($(this).width());
                            return $(this).width()=="0"? "100":"0";
                    }});
                });
                $('#animate').click(function(){
                    $('#example').animate({width:function(){
                            console.log($(this).width());
                            return $(this).width()=="0"? "100":"0";
                    }});
                });
            });
        </script>
    </head>
    <body>
        <div id="example">            

        </div>
        <input type="button" value="Css" id="check"/>
        <input type="button" value="Animate" id="animate"/>
    </body>
</html>

推荐答案

演示

$('#animate').click(function () {
    $('#example').animate({
        width: (function ($self) {
            console.log($self.width());
            return $self.width() == "0" ? "100" : "0";
        })($('#example'))
    });
});

这篇关于用jQuery动画编写匿名函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆