小码哥的IT人生

首页 > JS > jQuery

jQuery 效果 - animate() 方法 详解

jQuery 2022-06-01 14:57:49小码哥的IT人生shichen

jQuery 效果 - animate() 方法

实例

改变 "div" 元素的高度:

$(".btn1").click(function(){
  $("#box").animate({height:"300px"});
});

完整实例:

<html>
<head>
<script type="text/javascript" src="https://apps.bdimg.com/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function()
  {
  $(".btn1").click(function(){
    $("#box").animate({height:"300px"});
  });
  $(".btn2").click(function(){
    $("#box").animate({height:"100px"});
  });
});
</script>
</head>
<body>
<div id="box" style="background:#98bf21;height:100px;width:100px;margin:6px;">
</div>
<button class="btn1">Animate</button>
<button class="btn2">Reset</button>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

定义和用法

animate() 方法执行 CSS 属性集的自定义动画。

该方法通过CSS样式将元素从一个状态改变为另一个状态。CSS属性值是逐渐改变的,这样就可以创建动画效果。

只有数字值可创建动画(比如 "margin:30px")。字符串值无法创建动画(比如 "background-color:red")。

注释:使用 "+=" 或 "-=" 来创建相对动画(relative animations)。

语法 1

$(selector).animate(styles,speed,easing,callback)
参数 描述
styles

必需。规定产生动画效果的 CSS 样式和值。

可能的 CSS 样式值(提供实例):

注释:CSS 样式使用 DOM 名称(比如 "fontSize")来设置,而非 CSS 名称(比如 "font-size")。

speed

可选。规定动画的速度。默认是 "normal"。

可能的值:

  • 毫秒 (比如 1500)
  • "slow"
  • "normal"
  • "fast"
easing

可选。规定在不同的动画点中设置动画速度的 easing 函数。

内置的 easing 函数:

扩展插件中提供更多 easing 函数。

  • swing
  • linear
callback

可选。animate 函数执行完之后,要执行的函数。

如需学习更多有关 callback 的内容,请访问我们的 jQuery Callback 这一章。

语法 2

$(selector).animate(styles,options)
参数 描述
styles 必需。规定产生动画效果的 CSS 样式和值(同上)。
options

可选。规定动画的额外选项。

可能的值:

  • speed - 设置动画的速度
  • easing - 规定要使用的 easing 函数
  • callback - 规定动画完成之后要执行的函数
  • step - 规定动画的每一步完成之后要执行的函数
  • queue - 布尔值。指示是否在效果队列中放置动画。如果为 false,则动画将立即开始
  • specialEasing - 来自 styles 参数的一个或多个 CSS 属性的映射,以及它们的对应 easing 函数

【backgroundPosition】完整实例:

<html>
<head>
<script type="text/javascript" src="https://apps.bdimg.com/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $(".btn1").click(function(){
    $("body").animate({backgroundPosition:"40% 40%"},0);
  });
  $(".btn2").click(function(){
    $("body").animate({backgroundPosition:"0% 0%"},0);
  });
});
</script>
</head>
<body style="background-image:url(/i/eg_background.jpg);background-repeat:no-repeat;background-attachment:fixed;">
<p>This is a paragraph.</p>
<button class="btn1">Animate</button>
<button class="btn2">Reset</button>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

【borderWidth】完整实例:

<html>
<head>
<script type="text/javascript" src="https://apps.bdimg.com/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $(".btn1").click(function(){
  $("p").animate({borderWidth:"10px"});
  });
  $(".btn2").click(function(){
  $("p").animate({borderWidth:"1px"});
  });
});
</script>
</head>
<body>
<p style="border:1px solid black">This is a paragraph.</p>
<button class="btn1">Animate</button>
<button class="btn2">Reset</button>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

【borderBottomWidth】完整实例:

<html>
<head>
<script type="text/javascript" src="https://apps.bdimg.com/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $(".btn1").click(function(){
  $("p").animate({borderBottomWidth:"10px"});
  });
  $(".btn2").click(function(){
  $("p").animate({borderBottomWidth:"1px"});
  });
});
</script>
</head>
<body>
<p style="border:1px solid black">This is a paragraph.</p>
<button class="btn1">Animate</button>
<button class="btn2">Reset</button>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

【borderLeftWidth】完整实例:

<html>
<head>
<script type="text/javascript" src="https://apps.bdimg.com/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $(".btn1").click(function(){
  $("p").animate({borderLeftWidth:"10px"});
  });
  $(".btn2").click(function(){
  $("p").animate({borderLeftWidth:"1px"});
  });
});
</script>
</head>
<body>
<p style="border:1px solid black">This is a paragraph.</p>
<button class="btn1">Animate</button>
<button class="btn2">Reset</button>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

【borderRightWidth】完整实例:

<html>
<head>
<script type="text/javascript" src="https://apps.bdimg.com/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $(".btn1").click(function(){
  $("p").animate({borderRightWidth:"10px"});
  });
  $(".btn2").click(function(){
  $("p").animate({borderRightWidth:"1px"});
  });
});
</script>
</head>
<body>
<p style="border:1px solid black">This is a paragraph.</p>
<button class="btn1">Animate</button>
<button class="btn2">Reset</button>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

【borderTopWidth】完整实例:

<html>
<head>
<script type="text/javascript" src="https://apps.bdimg.com/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $(".btn1").click(function(){
  $("p").animate({borderTopWidth:"10px"});
  });
  $(".btn2").click(function(){
  $("p").animate({borderTopWidth:"1px"});
  });
});
</script>
</head>
<body>
<p style="border:1px solid black">This is a paragraph.</p>
<button class="btn1">Animate</button>
<button class="btn2">Reset</button>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

【borderSpacing】完整实例:

<html>
<head>
<script type="text/javascript" src="https://apps.bdimg.com/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function()
  {
  $(".btn1").click(function(){
    $("table").animate({borderSpacing:"10px"},"slow");
  });
  $(".btn2").click(function(){
    $("table").animate({borderSpacing:"2px"},"slow");
  });
});
</script>
<style>
table{border:1px solid black;}
td{border:1px solid black;}
</style>
</head>
<body>
<table>
<tr>
<td>Peter</td>
<td>Griffin</td>
</tr>
<tr>
<td>Lois</td>
<td>Griffin</td>
</tr>
</table>
<button class="btn1">Animate</button>
<button class="btn2">Reset</button>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

【margin】完整实例:

<html>
<head>
<script type="text/javascript" src="https://apps.bdimg.com/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $(".btn1").click(function(){
  $("p").animate({margin:"100px"});
  });
  $(".btn2").click(function(){
  $("p").animate({margin:""});
  });
});
</script>
</head>
<body>
<p style="background-color:yellow">This is a paragraph.</p>
<button class="btn1">Animate</button>
<button class="btn2">Reset</button>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

【marginBottom】完整实例:

<html>
<head>
<script type="text/javascript" src="https://apps.bdimg.com/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $(".btn1").click(function(){
  $("p").animate({marginBottom:"100px"});
  });
  $(".btn2").click(function(){
  $("p").animate({marginBottom:""});
  });
});
</script>
</head>
<body>
<p style="background-color:yellow">This is a paragraph.</p>
<button class="btn1">Animate</button>
<button class="btn2">Reset</button>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

【marginLeft】完整实例:

<html>
<head>
<script type="text/javascript" src="https://apps.bdimg.com/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $(".btn1").click(function(){
  $("p").animate({marginLeft:"100px"});
  });
  $(".btn2").click(function(){
  $("p").animate({marginLeft:""});
  });
});
</script>
</head>
<body>
<p style="background-color:yellow">This is a paragraph.</p>
<button class="btn1">Animate</button>
<button class="btn2">Reset</button>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

【marginRight】完整实例:

<html>
<head>
<script type="text/javascript" src="https://apps.bdimg.com/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $(".btn1").click(function(){
  $("p").animate({marginRight:"100px"});
  });
  $(".btn2").click(function(){
  $("p").animate({marginRight:""});
  });
});
</script>
</head>
<body>
<p style="background-color:yellow">This is a paragraph.</p>
<button class="btn1">Animate</button>
<button class="btn2">Reset</button>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

【marginTop】完整实例:

<html>
<head>
<script type="text/javascript" src="https://apps.bdimg.com/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $(".btn1").click(function(){
  $("p").animate({marginTop:"100px"});
  });
  $(".btn2").click(function(){
  $("p").animate({marginTop:""});
  });
});
</script>
</head>
<body>
<p style="background-color:yellow">This is a paragraph.</p>
<button class="btn1">Animate</button>
<button class="btn2">Reset</button>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

【outlineWidth】完整实例:

<html>
<head>
<script type="text/javascript" src="https://apps.bdimg.com/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $(".btn1").click(function(){
  $("p").animate({outlineWidth:"10px"});
  });
  $(".btn2").click(function(){
  $("p").animate({outlineWidth:"1px"});
  });
});
</script>
</head>
<body>
<p style="outline:1px solid black">This is a paragraph.</p>
<button class="btn1">Animate</button>
<button class="btn2">Reset</button>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

【padding】完整实例:

<html>
<head>
<script type="text/javascript" src="https://apps.bdimg.com/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $(".btn1").click(function(){
  $("p").animate({padding:"100px"});
  });
  $(".btn2").click(function(){
  $("p").animate({padding:""});
  });
});
</script>
</head>
<body>
<p style="background-color:yellow">This is a paragraph.</p>
<button class="btn1">Animate</button>
<button class="btn2">Reset</button>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

【paddingBottom】完整实例:

<html>
<head>
<script type="text/javascript" src="https://apps.bdimg.com/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $(".btn1").click(function(){
  $("p").animate({paddingBottom:"100px"});
  });
  $(".btn2").click(function(){
  $("p").animate({paddingBottom:""});
  });
});
</script>
</head>
<body>
<p style="background-color:yellow">This is a paragraph.</p>
<button class="btn1">Animate</button>
<button class="btn2">Reset</button>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

【paddingLeft】完整实例:

<html>
<head>
<script type="text/javascript" src="https://apps.bdimg.com/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $(".btn1").click(function(){
  $("p").animate({paddingLeft:"100px"});
  });
  $(".btn2").click(function(){
  $("p").animate({paddingLeft:""});
  });
});
</script>
</head>
<body>
<p style="background-color:yellow">This is a paragraph.</p>
<button class="btn1">Animate</button>
<button class="btn2">Reset</button>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

【paddingRight】完整实例:

<html>
<head>
<script type="text/javascript" src="https://apps.bdimg.com/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $(".btn1").click(function(){
  $("p").animate({paddingRight:"100px"});
  });
  $(".btn2").click(function(){
  $("p").animate({paddingRight:""});
  });
});
</script>
</head>
<body>
<p style="background-color:yellow">This is a paragraph.</p>
<button class="btn1">Animate</button>
<button class="btn2">Reset</button>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

【paddingTop】完整实例:

<html>
<head>
<script type="text/javascript" src="https://apps.bdimg.com/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $(".btn1").click(function(){
  $("p").animate({paddingTop:"100px"});
  });
  $(".btn2").click(function(){
  $("p").animate({paddingTop:""});
  });
});
</script>
</head>
<body>
<p style="background-color:yellow">This is a paragraph.</p>
<button class="btn1">Animate</button>
<button class="btn2">Reset</button>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

【height】完整实例:

<html>
<head>
<script type="text/javascript" src="https://apps.bdimg.com/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $(".btn1").click(function(){
  $("p").animate({height:"50%"});
  });
  $(".btn2").click(function(){
  $("p").animate({height:"20px"});
  });
});
</script>
</head>
<body>
<p style="background-color:yellow;height:20px;">This is a paragraph.</p>
<button class="btn1">Animate</button>
<button class="btn2">Reset</button>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

【width】完整实例:

<html>
<head>
<script type="text/javascript" src="https://apps.bdimg.com/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $(".btn1").click(function(){
  $("p").animate({width:"50%"});
  });
  $(".btn2").click(function(){
  $("p").animate({width:"100%"});
  });
});
</script>
</head>
<body>
<p style="background-color:yellow;">This is a paragraph.</p>
<button class="btn1">Animate</button>
<button class="btn2">Reset</button>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

【maxHeight】完整实例:

<html>
<head>
<script type="text/javascript" src="https://apps.bdimg.com/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $(".btn1").click(function(){
  $("p").animate({maxHeight:"100px"});
  });
  $(".btn2").click(function(){
  $("p").animate({maxHeight:"200px"});
  });
});
</script>
</head>
<body>
<p style="background-color:yellow;height:200px;">This is a paragraph.</p>
<button class="btn1">Animate</button>
<button class="btn2">Reset</button>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

【maxWidth】完整实例:

<html>
<head>
<script type="text/javascript" src="https://apps.bdimg.com/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $(".btn1").click(function(){
  $("p").animate({maxWidth:"50%"});
  });
  $(".btn2").click(function(){
  $("p").animate({maxWidth:"100%"});
  });
});
</script>
</head>
<body>
<p style="background-color:yellow;">This is a paragraph.</p>
<button class="btn1">Animate</button>
<button class="btn2">Reset</button>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

【minHeight】完整实例:

<html>
<head>
<script type="text/javascript" src="https://apps.bdimg.com/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $(".btn1").click(function(){
  $("p").animate({minHeight:"100px"});
  });
  $(".btn2").click(function(){
  $("p").animate({minHeight:""});
  });
});
</script>
</head>
<body>
<p style="background-color:yellow;">This is a paragraph.</p>
<button class="btn1">Animate</button>
<button class="btn2">Reset</button>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

【minWidth】完整实例:

<html>
<head>
<script type="text/javascript" src="https://apps.bdimg.com/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $(".btn1").click(function(){
  $("p").animate({minWidth:"200px"});
  });
  $(".btn2").click(function(){
  $("p").animate({minWidth:""});
  });
});
</script>
</head>
<body>
<p style="background-color:yellow;width:100px;">This is a paragraph.</p>
<button class="btn1">Animate</button>
<button class="btn2">Reset</button>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

【font】完整实例:

<html>
<head>
<script type="text/javascript" src="https://apps.bdimg.com/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $(".btn1").click(function(){
  $("p").animate({font:"30px arial,sans-serif"});
  });
  $(".btn2").click(function(){
  $("p").animate({font:"15px arial,sans-serif"});
  });
});
</script>
</head>
<body>
<p style="background-color:yellow;font:15px arial,sans-serif;">This is a paragraph.</p>
<button class="btn1">Animate</button>
<button class="btn2">Reset</button>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

【fontSize】完整实例:

<html>
<head>
<script type="text/javascript" src="https://apps.bdimg.com/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $(".btn1").click(function(){
  $("p").animate({fontSize:"3em"});
  });
  $(".btn2").click(function(){
  $("p").animate({fontSize:"1em"});
  });
});
</script>
</head>
<body>
<p style="background-color:yellow;">This is a paragraph.</p>
<button class="btn1">Animate</button>
<button class="btn2">Reset</button>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

【bottom】完整实例:

<html>
<head>
<script type="text/javascript" src="https://apps.bdimg.com/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $(".btn1").click(function(){
  $("p").animate({bottom:"-200px"});
  });
  $(".btn2").click(function(){
  $("p").animate({bottom:"-70px"});
  });
});
</script>
</head>
<body>
<button class="btn1">Animate</button>
<button class="btn2">Reset</button>
<div style="position:relative">
<p style="background-color:yellow;width:100px;position:absolute">This is a paragraph.</p>
</div>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

【left】完整实例:

<html>
<head>
<script type="text/javascript" src="https://apps.bdimg.com/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $(".btn1").click(function(){
  $("p").animate({left:"100px"});
  });
  $(".btn2").click(function(){
  $("p").animate({left:""});
  });
});
</script>
</head>
<body>
<button class="btn1">Animate</button>
<button class="btn2">Reset</button>
<div style="position:relative">
<p style="background-color:yellow;width:100px;position:absolute">This is a paragraph.</p>
</div>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

【right】完整实例:

<html>
<head>
<script type="text/javascript" src="https://apps.bdimg.com/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $(".btn1").click(function(){
  $("p").animate({right:"50%"});
  });
  $(".btn2").click(function(){
  $("p").animate({right:""});
  });
});
</script>
</head>
<body>
<button class="btn1">Animate</button>
<button class="btn2">Reset</button>
<div style="position:relative">
<p style="background-color:yellow;width:100px;position:absolute;right:0px;">This is a paragraph.</p>
</div>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

【top】完整实例:

<html>
<head>
<script type="text/javascript" src="https://apps.bdimg.com/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $(".btn1").click(function(){
  $("p").animate({top:"100px"});
  });
  $(".btn2").click(function(){
  $("p").animate({top:"0px"});
  });
});
</script>
</head>
<body>
<button class="btn1">Animate</button>
<button class="btn2">Reset</button>
<div style="position:relative">
<p style="background-color:yellow;width:100px;position:absolute">This is a paragraph.</p>
</div>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

【letterSpacing】完整实例:

<html>
<head>
<script type="text/javascript" src="https://apps.bdimg.com/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $(".btn1").click(function(){
  $("p").animate({letterSpacing:"10px"});
  });
  $(".btn2").click(function(){
  $("p").animate({letterSpacing:""});
  });
});
</script>
</head>
<body>
<p>This is a paragraph.</p>
<button class="btn1">Animate</button>
<button class="btn2">Reset</button>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

【wordSpacing】完整实例:

<html>
<head>
<script type="text/javascript" src="https://apps.bdimg.com/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $(".btn1").click(function(){
  $("p").animate({wordSpacing:"10px"});
  });
  $(".btn2").click(function(){
  $("p").animate({wordSpacing:""});
  });
});
</script>
</head>
<body>
<p>This is a paragraph.</p>
<button class="btn1">Animate</button>
<button class="btn2">Reset</button>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

【lineHeight】完整实例:

<html>
<head>
<script type="text/javascript" src="https://apps.bdimg.com/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $(".btn1").click(function(){
  $("p").animate({lineHeight:"3em"});
  });
  $(".btn2").click(function(){
  $("p").animate({lineHeight:"1em"});
  });
});
</script>
</head>
<body>
<p>This is a paragraph. This is some more text. This is some more text. This is some more text. This is some more text. This is some more text. This is some more text. This is some more text. This is some more text. This is some more text. This is some more text. This is some more text. This is some more text. This is some more text.</p>
<button class="btn1">Animate</button>
<button class="btn2">Reset</button>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

【textIndent】完整实例:

<html>
<head>
<script type="text/javascript" src="https://apps.bdimg.com/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $(".btn1").click(function(){
  $("p").animate({textIndent:"30px"});
  });
  $(".btn2").click(function(){
  $("p").animate({textIndent:""});
  });
});
</script>
</head>
<body>
<p>This is a paragraph. This is some more text. This is some more text. This is some more text. This is some more text. This is some more text. This is some more text. This is some more text. This is some more text. This is some more text. This is some more text. This is some more text. This is some more text. This is some more text.</p>
<button class="btn1">Animate</button>
<button class="btn2">Reset</button>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

版权所有 © 小码哥的IT人生
Copyright © phpcodeweb All Rights Reserved
ICP备案号:苏ICP备17019232号-2  

苏公网安备 32030202000762号

© 2021-2024