• jQuery 效果


    jQuery animate() 方法允许您创建自定义的动画。


    jQuery 动画 - animate() 方法

    jQuery animate() 方法用于创建自定义动画。

    语法:

    $(selector).animate({params},speed,callback);

    必需的 params 参数定义形成动画的 CSS 属性。

    可选的 speed 参数规定效果的时长。它可以取以下值:"slow"、"fast" 或毫秒。

    可选的 callback 参数是动画完成后所执行的函数名称。

    下面的例子演示 animate() 方法的简单应用;它把 <div> 元素移动到左边,直到 left 属性等于 250 像素为止:

    实例

     1 <!DOCTYPE html>
     2 <html>
     3 <head>
     4 <script src="/jquery/jquery-1.11.1.min.js">
     5 </script>
     6 <script> 
     7 $(document).ready(function(){
     8   $("button").click(function(){
     9     $("div").animate({left:'250px'});
    10   });
    11 });
    12 </script> 
    13 </head>
    14  
    15 <body>
    16 <button>开始动画</button>
    17 <p>默认情况下,所有 HTML 元素的位置都是静态的,并且无法移动。如需对位置进行操作,记得首先把元素的 CSS position 属性设置为 relative、fixed 或 absolute。</p>
    18 <div style="background:#98bf21;height:100px;100px;position:absolute;">
    19 </div>
    20 
    21 </body>
    22 </html>
    View Code

    查看结果:

    移动后位置:

    提示:默认地,所有 HTML 元素都有一个静态位置,且无法移动。

    如需对位置进行操作,要记得首先把元素的 CSS position 属性设置为 relative、fixed 或 absolute!


    jQuery animate() - 操作多个属性

    请注意,生成动画的过程中可同时使用多个属性:

    实例

     1 <!DOCTYPE html>
     2 <html>
     3 <head>
     4 <script src="/jquery/jquery-1.11.1.min.js">
     5 </script>
     6 <script> 
     7 $(document).ready(function(){
     8   $("button").click(function(){
     9     $("div").animate({
    10       left:'250px',
    11       opacity:'0.5',
    12       height:'150px',
    13       '150px'
    14     });
    15   });
    16 });
    17 </script> 
    18 </head>
    19  
    20 <body>
    21 
    22 <button>开始动画</button>
    23 <p>默认情况下,所有 HTML 元素的位置都是静态的,并且无法移动。如需对位置进行操作,记得首先把元素的 CSS position 属性设置为 relative、fixed 或 absolute。</p>
    24 <div style="background:#98bf21;height:100px;100px;position:absolute;">
    25 </div>
    26 
    27 </body>
    28 </html>
    View Code

    查看结果:

    提示:可以用 animate() 方法来操作所有 CSS 属性吗?

    是的,几乎可以!

    不过,需要记住一件重要的事情:当使用 animate() 时,必须使用 Camel 标记法书写所有的属性名,比如,必须使用 paddingLeft 而不是 padding-left,使用 marginRight 而不是 margin-right,等等。

    同时,色彩动画并不包含在核心 jQuery 库中。

    如果需要生成颜色动画,您需要从 jQuery.com 下载 Color Animations 插件。


    jQuery animate() - 使用相对值

    也可以定义相对值(该值相对于元素的当前值)。需要在值的前面加上 += 或 -=:

    实例

     1 <!DOCTYPE html>
     2 <html>
     3 <head>
     4 <script src="/jquery/jquery-1.11.1.min.js"></script>
     5 <script> 
     6 $(document).ready(function(){
     7   $("button").click(function(){
     8     $("div").animate({
     9       left:'250px',
    10       height:'+=150px',
    11       '+=150px'
    12     });
    13   });
    14 });
    15 </script> 
    16 </head>
    17  
    18 <body>
    19 
    20 <button>开始动画</button>
    21 <p>默认情况下,所有 HTML 元素的位置都是静态的,并且无法移动。如需对位置进行操作,记得首先把元素的 CSS position 属性设置为 relative、fixed 或 absolute。</p>
    22 <div style="background:#98bf21;height:100px;100px;position:absolute;">
    23 </div>
    24 
    25 </body>
    26 </html>
    View Code

    查看结果:


    jQuery animate() - 使用预定义的值

    您甚至可以把属性的动画值设置为 "show"、"hide" 或 "toggle":

    实例

     1 <!DOCTYPE html>
     2 <html>
     3 <head>
     4 <script src="/jquery/jquery-1.11.1.min.js"></script>
     5 <script> 
     6 $(document).ready(function(){
     7   $("button").click(function(){
     8     $("div").animate({
     9       height:'toggle'
    10     });
    11   });
    12 });
    13 </script> 
    14 </head>
    15  
    16 <body>
    17 
    18 <button>开始动画</button>
    19 <p>默认情况下,所有 HTML 元素的位置都是静态的,并且无法移动。如需对位置进行操作,记得首先把元素的 CSS position 属性设置为 relative、fixed 或 absolute。</p>
    20 <div style="background:#98bf21;height:100px;100px;position:absolute;">
    21 </div>
    22 
    23 </body>
    24 </html>
    View Code

    查看结果:

    点击之后:消失

    再次点击:出现


    jQuery animate() - 使用队列功能

    默认地,jQuery 提供针对动画的队列功能。

    这意味着如果您在彼此之后编写多个 animate() 调用,jQuery 会创建包含这些方法调用的“内部”队列。然后逐一运行这些 animate 调用。

    实例 1

    隐藏,如果您希望在彼此之后执行不同的动画,那么我们要利用队列功能:

     1 <!DOCTYPE html>
     2 <html>
     3 <head>
     4 <script src="/jquery/jquery-1.11.1.min.js"></script>
     5 <script> 
     6 $(document).ready(function(){
     7   $("button").click(function(){
     8     var div=$("div");
     9     div.animate({height:'300px',opacity:'0.4'},"slow");
    10     div.animate({'300px',opacity:'0.8'},"slow");
    11     div.animate({height:'100px',opacity:'0.4'},"slow");
    12     div.animate({'100px',opacity:'0.8'},"slow");
    13   });
    14 });
    15 </script> 
    16 </head>
    17  
    18 <body>
    19 
    20 <button>开始动画</button>
    21 <p>默认情况下,所有 HTML 元素的位置都是静态的,并且无法移动。如需对位置进行操作,记得首先把元素的 CSS position 属性设置为 relative、fixed 或 absolute。</p>
    22 <div style="background:#98bf21;height:100px;100px;position:absolute;">
    23 </div>
    24 
    25 </body>
    26 </html>
    View Code

    查看结果:

    默认状态:

    动画效果变成300px*300px:

    恢复原状:

    实例 2

    下面的例子把 <div> 元素移动到右边,然后增加文本的字号:

     1 <!DOCTYPE html>
     2 <html>
     3 <head>
     4 <script src="/jquery/jquery-1.11.1.min.js"></script>
     5 <script> 
     6 $(document).ready(function(){
     7   $("button").click(function(){
     8     var div=$("div");  
     9     div.animate({left:'100px'},"slow");
    10     div.animate({fontSize:'3em'},"slow");
    11   });
    12 });
    13 </script> 
    14 </head>
    15 
    16 <body>
    17 
    18 <button>开始动画</button>
    19 <p>默认情况下,所有 HTML 元素的位置都是静态的,并且无法移动。如需对位置进行操作,记得首先把元素的 CSS position 属性设置为 relative、fixed 或 absolute。</p>
    20 <div style="background:#98bf21;height:100px;200px;position:absolute;">HELLO</div>
    21 
    22 </body>
    23 </html>
    View Code

    查看结果:

    默认状态:

    点击之后:

  • 相关阅读:
    求二叉树的层次遍历
    求二叉树的先序遍历
    数据结构实验之二叉树一:树的同构
    list列表操作
    汉罗塔问题
    expected an indented block
    42步进阶学习—让你成为优秀的Java大数据科学家!
    MVC 3.0错误 HTTP 404您正在查找的资源(或者它的一个依赖项)可能已被移除,或其名称已更改,或暂时不可用。请检查以下 URL 并确保其拼写正确。
    判断浏览器版本
    VisualStudio2012编辑器错误
  • 原文地址:https://www.cnblogs.com/sihuiming/p/5336646.html
Copyright © 2020-2023  润新知