jQuery animate() 方法允许您创建自定义的动画。语法:
$(selector).animate({params},speed,callback);必需的 params 参数定义形成动画的 CSS 属性。
<!DOCTYPE html> <html> <head> <script src="../jquery/jquery-1.11.1.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ $("div").animate({left:"250px"}); }); }); </script> </head> <body> <button>开始动画</button> <p>把 div 元素向右移动,直到 left 属性等于 250 像素为止。</p> <div style="background:#98bf21;height:100px;width:100px;position:absolute;"></div> </body> </html>提示:默认地,所有 HTML 元素都有一个静态位置,且无法移动。
<!DOCTYPE html> <html> <head> <script src="../jquery/jquery-1.11.1.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ $("div").animate({ left:"250px", opacity:"0.5", height:"150px", width:"150px" }); }); }); </script> </head> <body> <button>开始动画</button> <div style="background:#98bf21;height:100px;width:100px;position:absolute;"></div> </body> </html>提示:可以用 animate() 方法来操作所有 CSS 属性吗?是的,几乎可以!
<!DOCTYPE html> <html> <head> <script src="../jquery/jquery-1.11.1.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ $("div").animate({ left:"250px", height:"+=150px", width:"+=150px" }); }); }); </script> </head> <body> <button>开始动画</button> <div style="background:#98bf21;height:100px;width:100px;position:absolute;"></div> </body> </html>
<!DOCTYPE html> <html> <head> <script src="../jquery/jquery-1.11.1.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ $("div").animate({ height:"toggle" }); }); }); </script> </head> <body> <button>开始动画</button> <div style="background:#98bf21;height:100px;width:100px;position:absolute;"></div> </body> </html>
<!DOCTYPE html> <html> <head> <script src="../jquery/jquery-1.11.1.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ var div=$("div"); div.animate({height:"300px",opacity:"0.4"},"slow"); div.animate({width:"300px",opacity:"0.8"},"slow"); div.animate({height:"100px",opacity:"0.4"},"slow"); div.animate({width:"100px",opacity:"0.8"},"slow"); }); }); </script> </head> <body> <button>开始动画</button> <div style="background:#98bf21;height:100px;width:100px;position:absolute;"></div> </body> </html>下面的例子把 <div> 元素移动到右边,然后增加文本的字号:
<!DOCTYPE html> <html> <head> <script src="../jquery/jquery-1.11.1.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ var div=$("div"); div.animate({left:"100px"},"slow"); div.animate({fontSize:"3em"},"slow"); }); }); </script> </head> <body> <button>开始动画</button> <div style="background:#98bf21;height:100px;width:200px;position:absolute;">HELLO</div> </body> </html>
评论