jquery动画为什么延迟执行?

carlisliu 2013-02-19 11:33:08

<!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>

让焦点在文本框中获得、失去,重复几次,为什么动画效果开始执行的时间越来越慢?基本上点击几次,就延时几秒执行
...全文
280 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
carlisliu 2013-02-19
  • 打赏
  • 举报
回复
引用 4 楼 qqwswxdo 的回复:
引用 3 楼 carlisliu 的回复:引用 2 楼 mjhwy 的回复:你把focus和blur分开写试试 XML/HTML code?123456789101112131415161718192021222324252627282930313233343536373839<!DOCTYPE html><html><head> <title>Display P……
看来这位朋友对jquery掌握的很好,谢谢解答。。。
carlisliu 2013-02-19
  • 打赏
  • 举报
回复
引用 5 楼 mjhwy 的回复:
引用 3 楼 carlisliu 的回复:引用 2 楼 mjhwy 的回复:你把focus和blur分开写试试 XML/HTML code?123456789101112131415161718192021222324252627282930313233343536373839<!DOCTYPE html><html><head> <title>Display P……
是个不错的测试方法,结果确实像#4所说,谢谢。。。
mjhwy 2013-02-19
  • 打赏
  • 举报
回复
引用 4 楼 qqwswxdo 的回复:
引用 3 楼 carlisliu 的回复:引用 2 楼 mjhwy 的回复:你把focus和blur分开写试试 XML/HTML code?123456789101112131415161718192021222324252627282930313233343536373839<!DOCTYPE html><html><head> <title>Display P……
+1 到位~~~~
mjhwy 2013-02-19
  • 打赏
  • 举报
回复
引用 3 楼 carlisliu 的回复:
引用 2 楼 mjhwy 的回复:你把focus和blur分开写试试 XML/HTML code?123456789101112131415161718192021222324252627282930313233343536373839<!DOCTYPE html><html><head> <title>Display Page</title> <link ……
你在你的blur方法中加个alert("blur");执行以下看看 可以看到原因
PerterPon 2013-02-19
  • 打赏
  • 举报
回复
引用 3 楼 carlisliu 的回复:
引用 2 楼 mjhwy 的回复:你把focus和blur分开写试试 XML/HTML code?123456789101112131415161718192021222324252627282930313233343536373839<!DOCTYPE html><html><head> <title>Display Page</title> <link ……
jQuery有一个动画队列,用以维护对于此节点的动画操作,你的代码的意思是:每次focus的时候,都给节点元素绑定一个blur事件处理函数,而之前的方法并没有消失掉,还在队列里,所以多次focus之后,队列里的事件处理函数就有很多了,因为执行的函数多了,所以你的代码运行速度就慢了。
carlisliu 2013-02-19
  • 打赏
  • 举报
回复
引用 2 楼 mjhwy 的回复:
你把focus和blur分开写试试 XML/HTML code?123456789101112131415161718192021222324252627282930313233343536373839<!DOCTYPE html><html><head> <title>Display Page</title> <link href="style.css" ……
解决了,请问知道原因吗?
mjhwy 2013-02-19
  • 打赏
  • 举报
回复
你把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>
liulx22 2013-02-19
  • 打赏
  • 举报
回复
把代码改成这样就好了。

$("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);
			 });; 

87,910

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧