Writing anonymous function in jQuery animation

為{幸葍}努か 提交于 2021-02-05 11:49:53

问题


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>

回答1:


Demo

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


来源:https://stackoverflow.com/questions/17232179/writing-anonymous-function-in-jquery-animation

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