1

我有来自jQuery animate的示例代码 ,我想让 div 基于计数器自动扩展,基于时间,随着计数器或时间的变化,div 变得更宽。我不知道如何使用变量来替换宽度:“70%”

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <meta name="generator" content="HTML Tidy for Windows (vers 14 February 2006), see www.w3.org">
<style type="text/css">
div {
background-color:#bca;width:100px;border:1px solid green;
}
</style>
<script src="http://code.jquery.com/jquery-latest.js" type="text/javascript">
</script>
    <title></title>
  </head>
  <body>
    <button id="go">» Run</button>
    <div id="block">
      Hello!
    </div>
<script type="text/javascript">
/* Using multiple unit types within one animation. */
 $("#go").click(function(){  $("#block").animate({    width: "70%",    opacity: 0.4,    marginLeft: "0.6in",    fontSize: "3em",    borderWidth: "10px"  }, 1500 );});
</script>
  </body>
</html>
4

2 回答 2

0

请示例代码以使用宽度变量:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <meta name="generator" content="HTML Tidy for Windows (vers 14 February 2006), see www.w3.org">
<style type="text/css">
div {
background-color:#bca;width:100px;border:1px solid green;
}
</style>
<script src="http://code.jquery.com/jquery-latest.js" type="text/javascript">
</script>
    <title></title>
  </head>
  <body>
    <button id="go">» Run</button>
    <div id="block">
      Hello!
    </div>
<script type="text/javascript">
  var someWidth = "70%";
/* Using multiple unit types within one animation. */
 $("#go").click(function(){  $("#block").animate({    width: someWidth,    opacity: 0.4,    marginLeft: "0.6in",    fontSize: "3em",    borderWidth: "10px"  }, 1500 );});
</script>
  </body>
</html>
于 2013-01-17T21:08:51.970 回答
0

+=您可以通过如下指定来使用相对动画

$("#go").click(function(){  
         $("#block").animate({   
                             width: "+=5%",   
                             opacity: 0.4,
                             marginLeft: "0.6in",  
                             fontSize: "3em",   
                             borderWidth: "10px"  
                            }, 1500 );
         });

5%这将在每次调用函数时扩展 div。调整它以满足您的需要。相反,如果您想animate在间隔基础上调用此函数。setInterval这将animate每 2000 毫秒调用一次。

setInterval( function(){

        $("#block").animate({   
                             width: "+=5%",   
                             opacity: 0.4,
                             marginLeft: "0.6in",  
                             fontSize: "3em",   
                             borderWidth: "10px"  

                    }, 1500 );               

             },2000);
于 2013-01-17T21:18:26.067 回答