Jquery教程
Jquery动效
Jquery HTML/CSS
Jquery 事件
jquery常用

JQuery animate()

JQuery animate()方法为您提供了一种创建自定义动画的方法。
语法:
$(selector).animate({params}, speed, callback);
此处, params 参数定义了要设置动画的CSS属性。
speed参数是可选参数,用于指定效果的持续时间。可以将其设置为"slow","fast"或毫秒。
callback 参数也是可选的,它是动画完成后执行的功能。
让我们以一个简单的示例来看动画效果。
<!DOCTYPE html>
<html>
<head>
<script src="/ajax/libs/JQuery/1.11.2/JQuery.min.js"></script>
<script>
$(document).ready
(function(){
    $("button").click(function(){
        $("div").animate({left: '450px'});
    });
});
</script>
</head>
<body>
<button>Start Animation</button>
<p>A simple animation example:</p>
<div style="background:#98bf21;height:100px;width:100px;position:absolute;"></div> </body>
</html>
输出:

一个简单的动画示例:

注意: 所有HTML元素的默认位置均为静态。如果要操纵它们的位置,请将CSS position属性设置为元素的相对,固定或绝对。

使用多个属性的JQuery animate()方法

您可以同时使用多个属性进行动画处理。
<!DOCTYPE html>
<html>
<head>
<script src="/ajax/libs/JQuery/1.11.2/JQuery.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>Start Animation</button>
<div style="background:#125f21;height:100px;width:100px;position:absolute;"></div> </body>
</html>
输出:

使用相对值的JQuery animate()方法

还可以通过在值前面加上+ =或-=来定义相对值(相对于元素的当前值).
<!DOCTYPE html>
<html>
<head>
<script src="/ajax/libs/JQuery/1.11.2/JQuery.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()方法

还可以将属性的动画值指定为" show"," hide"或" toggle"。
在此示例中,我们使用" toggle"值表示高度,这表示它将显示/隐藏所选元素。
<!DOCTYPE html>
<html>
<head>
<script src="/ajax/libs/JQuery/1.11.2/JQuery.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彩色动画

您还可以为颜色之间的元素的属性设置动画。
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>JQuery UI Effects - Animate demo</title>
  <link rel="stylesheet" href="http://code.JQuery.com/ui/1.11.4/themes/smoothness/JQuery-ui.css">
  <script src="http://code.JQuery.com/JQuery-1.10.2.js"></script>
  <script src="http://code.JQuery.com/ui/1.11.4/JQuery-ui.js"></script>
  <style>
    .toggler { width: 500px; height: 200px; position: relative; }
    #button { padding: .5em 1em; text-decoration: none; }
    #effect { width: 240px; height: 135px; padding: 0.4em; position: relative; background: #fff; }
    #effect h3 { margin: 0; padding: 0.4em; text-align: center; }
  </style>
  <script>
  $(function() {
    var state = true;
    $("#button").click(function() {
      if ( state ) {
        $("#effect").animate({
          backgroundColor: "#aa0000",
          color: "#fff",
          width: 500
        }, 1000 );
      } else {
        $("#effect").animate({
          backgroundColor: "#fff",
          color: "#000",
          width: 240
        }, 1000 );
      }
      state = !state;
    });
  });
  </script>
</head>
<body>
<div class="toggler">
  <div id="effect" class="ui-widget-content ui-corner-all">
    <h3 class="ui-widget-header ui-corner-all">Animate</h3>
    <p>lidihuo.com is the best tutorial website to learn Java and other programming languages.</p>
  </div>
</div>
 <button id="button" class="ui-state-default ui-corner-all">Toggle Effect</button>
</body>
</html>
输出:

Animate

lidihuo.com is the best tutorial website to learn Java and other programming languages.

昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4