jQuery如何创建自定义的动画

发布时间:2022-02-22 10:49:40 作者:小新
来源:亿速云 阅读:120

这篇文章主要为大家展示了“jQuery如何创建自定义的动画”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“jQuery如何创建自定义的动画”这篇文章吧。

jQuery 效果 - 动画
jQuery animate() 方法允许您创建自定义的动画。语法:

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

必需的 params 参数定义形成动画的 CSS 属性。
可选的 speed 参数规定效果的时长。它可以取以下值:"slow"、"fast" 或毫秒。
可选的 callback 参数是动画完成后所执行的函数名称。

<!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 元素都有一个静态位置,且无法移动。
提示:如需对位置进行操作,要把元素的 position 属性设置为 relative、fixed 或 absolute!

jQuery animate() - 操作多个属性
请注意,生成动画的过程中可同时使用多个属性:

<!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 属性吗?是的,几乎可以!
    不过,当使用 animate() 时,必须使用 Camel 标记法书写所有的属性名,比如:必须使用 paddingLeft 而不是 padding-left,使用 marginRight 而不是 margin-right,等等。
    同时,色彩动画并不包含在核心 jQuery 库中。如果需要生成颜色动画,您需要从 jQuery.com 下载 Color Animations 插件。

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

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


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

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


jQuery animate() - 使用队列功能
默认地,jQuery 提供针对动画的队列功能。这意味着如果您编写多个 animate() 调用:
jQuery 会创建包含这些方法调用的“内部”队列,然后逐一运行这些 animate 调用。
下面的实例中,我们利用队列功能执行不同的动画:

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

以上是“jQuery如何创建自定义的动画”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

推荐阅读:
  1. JQuery动画
  2. JQuery学习笔记-JQuery的动画效果

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

jquery

上一篇:PHP中echo、print_r()和var_dump()怎么用

下一篇:jQuery如何使用stop()方法停止动画

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》