<!DOCTYPE html>
<html>
<head>
    <title>Display Page</title>
    <link href="style.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" src="jquery-1.9.1.js"></script>
</head>
<body>
    <div id="transform" style="width: 300px;height:60px;background: blue;">
        <input type="text" style="width: 240px;left: 30px;top:20px;position: relative;">
    </div>
    <script type="text/javascript">
        $("input[type='text']").focus(function () {
            var elem = $('#transform');
            elem.animate({
                width:"500px",
                height:"200px"
            }, 1000);
            $(this).animate({
                width:"420",
                left:"40"
            }, 1000).blur(function () {
                        elem.animate({
                            width:"300px",
                            height:"60px"
                        }, 1000);
                        $(this).animate({
                            width:'240px',
                            left:'30px'
                        }, 1000);
                    });
        });
    </script>
</body>
</html>让焦点在文本框中获得、失去,重复几次,为什么动画效果开始执行的时间越来越慢?基本上点击几次,就延时几秒执行jquery

解决方案 »

  1.   

    把代码改成这样就好了。$("input[type='text']").focus(function () { 
          var elem = $('#transform'); 
          elem.animate({ 
          width:"500px", 
          height:"200px" 
          }, 1000); 
          $(this).animate({  
          width:"420",
          left:"40" 
          }, 1000);
          }).blur(function () {
          var elem = $('#transform'); 
     elem.animate({
     width:"300px",
     height:"60px" 
     }, 1000); 
     $(this).animate({
     width:'240px',
     left:'30px'
     }, 1000);
     });; 
      

  2.   

    你把focus和blur分开写试试<!DOCTYPE html>
    <html>
    <head>
        <title>Display Page</title>
        <link href="style.css" rel="stylesheet" type="text/css" />
        <script type="text/javascript" src="jquery-1.9.1.js"></script>
    </head>
    <body>
        <div id="transform" style="width: 300px;height:60px;background: blue;">
            <input type="text" style="width: 240px;left: 30px;top:20px;position: relative;">
        </div>
        <script type="text/javascript">
            $("input[type='text']").focus(function () {
                var elem = $('#transform');
                elem.animate({
                    width:"500px",
                    height:"200px"
                }, 1000);
                $(this).animate({
                    width:"420",
                    left:"40"
                }, 1000);        });
            
                   $("input[type='text']").blur(function () {
                        var elem = $('#transform');
                            elem.animate({
                                width:"300px",
                                height:"60px"
                            }, 1000);
                            $(this).animate({
                                width:'240px',
                                left:'30px'
                            }, 1000);
                        });
        </script>
    </body>
    </html>
      

  3.   

    jQuery有一个动画队列,用以维护对于此节点的动画操作,你的代码的意思是:每次focus的时候,都给节点元素绑定一个blur事件处理函数,而之前的方法并没有消失掉,还在队列里,所以多次focus之后,队列里的事件处理函数就有很多了,因为执行的函数多了,所以你的代码运行速度就慢了。
      

  4.   

    你在你的blur方法中加个alert("blur");执行以下看看
    可以看到原因
      

  5.   

    看来这位朋友对jquery掌握的很好,谢谢解答