当前位置:首页 > 技术文章 > 正文内容

jQuery 自定义动画——animate

arlanguage1周前 (04-25)技术文章5

自定义动画

使用animate()方法

animate(params,[duration],[easing],[callback])

其中params为希望进行变换的CSS属性列表,以及希望变化到的最终值;

需要特别指出,params中的变量遵循camel(驼峰式)命名方式,例如:paddingLeft不能写成padding_left

params只能是CSS中用数值表示的属性,例如width、top、opacity等,像backgroundColor这样的属性不被animate()支持;

属性值必须加引号,例如:width: "90%",用逗号隔开

duration为持续的时间,三种预定速度之一的字符串("slow","normal", or "fast")或表示动画时长的毫秒数值(如:1000)

fast: 快速的

easing为可选参数,通常供动画插件使用,用来控制变化工程的节奏,jQuery中只提供了linear和swing两个值

jQuery 代码:

点击按钮后div元素的几个不同属性一同变化

// 在一个动画中同时应用三种类型的效果

$("#go").click(function(){
    $("#block").animate({
        width:"90%",
        height:"100%",
        fontSize:"10em",
        borderWidth:"10px"
    }, 1000 );
});

HTML 代码:

<button id="go"> Run</button>
<div id="block">Hello!</div>

相对变化的自定义动画

在params的CSS属性列表中,jQuery还允许使用"+="或者"-="来表示相对变化(多次变化)

<html>
<head>
<title>animate()方法</title>
<style type="text/css">
<!--
body{
background:url(bg2.jpg);
}
div{
position:absolute; /* 绝对定位 */
left:90px; top:50px;
}
input{
border:1px solid #000033;
}
-->
</style>
<script language="javascript" src="jquery.min.js"></script>
<script language="javascript">
$(function(){
    $("input:first").click(function(){
    $("#block").animate({
    left: "-=80px" //相对左移
    },300);
    });
    $("input:last").click(function(){
    $("#block").animate({
    left: "+=80px" //相对右移
    },300);
    });
});
</script>
</head>
<body>
<input type="button" value="<<左"> <input type="button" value="右>>">
<div id="block"><img src="05.jpg"></div>
</body>
</html>

让指定元素左右移动

jQuery 代码:

$("#right").click(function(){
$(".block").animate({left: '+50px'}, "slow"); //+50px只能移动一次
});
$("#left").click(function(){
$(".block").animate({left: '-50px'}, "slow"); //-50px只能移动一次
});

HTML 代码:

<button id="left"><<</button> <button id="right">>></button>
<div class="block"></div>

另外在CSS属性列表params中,还可以将属性的最终值设置为"show"、"hide"、"toggle"

在600毫秒内切换段落的高度和透明度

jQuery 代码:

$("p").animate({
height: 'toggle', opacity: 'toggle'
}, "slow");


用500毫秒将段落移到left为50的地方并且完全清晰显示出来(透明度为1)

jQuery 代码:

$("p").animate({
left: 50, opacity: 'show'
}, 500);


一个使用"easein"函数提供不同动画样式的例子。只有使用了插件来提供这个"easein"函数,这个参数才起作用。

jQuery 代码:

$("p").animate({
opacity: 'show'
}, "slow", "easein");


animate()方法还有另外一种形式

animate(params,options)

其中params与第一种形式完全相同,options为动画的可选参数列表,主要包括duration、easing、callback、queue等。

其中duration、easing、callback与第1种形式完全一样,queue为布尔值,表示当有多个animate()组成jQuery链时,当前

animate()与紧接着的下一个animate()是按顺序执行(true,默认值),还是同时触发(false);

动画效果的触发程序

jQuery代码:

$(function(){
    $("input:eq(0)").click(function(){
    //第一个animate与第二个animate同时执行,然后再执行第三个
    $("#block1").animate({width:"90%"},{queue:false,duration:1500})
    .animate({fontSize:"24px"},1000)
    .animate({borderRightWidth:"20px"},1000);
    });
    $("input:eq(1)").click(function(){
    //依次执行三个animate
    $("#block2").animate({width:"90%"},1500)
    .animate({fontSize:"24px"}, 1000)
    .animate({borderRightWidth:"20px"}, 1000);
    });
    $("input:eq(2)").click(function(){
    $("input:eq(0)").click(); //触发单击事件,等同于$("input:eq(0)").trigger("click");
    $("input:eq(1)").click(); //触发单击事件,等同于$("input:eq(1)").trigger("click");
    });
    $("input:eq(3)").click(function(){
    //恢复默认设置
    $("div").css({width:"", fontSize:"", borderWidth:""});
    });
});

HTML代码:

<input type="button" id="go1" value="Block1动画">
<input type="button" id="go2" value="Block2动画">
<input type="button" id="go3" value="同时动画">
<input type="button" id="go4" value="重置">
<div id="block1">Block1</div>
<div id="block2">Block2</div>

delay(duration,[queueName])

设置一个延时来推迟执行队列中之后的项目。

jQuery代码:

$("button").click(function(){
//$("li").animate(参与过渡的属性,时长,效果,回调函数);
$("li").delay(5000).animate({
"width": "500px"
});
})

HTML代码:

<button>延迟执行</button>
<ul>
<li>1111</li>
</ul>

实例: delay特效

<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Document</title>
<style type="text/css">
*{
margin: 0;
padding: 0;
list-style: none;
}
ul{
width: 500px;
height: 500px;
margin: 0 auto;
border: 1px solid orange;
position: relative;
}
li{
width: 50px;
height: 50px;
background-image: url("img/ym.jpg");
background-repeat: no-repeat;
display: none;
position: absolute;
}
</style>
</head>
<body>
<button>┏ (゜ω゜)=</button>
<ul>
</ul>
<script src="js/jquery-2.2.4.js" type="text/javascript" charset="utf-8"></script>
<script src="js/jquery-ui.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
var str = "";
for(var i=0;i<10;i++){
for(var j=0;j<10;j++){
str += "<li style='left: "+(j*50)+"px;top: "+(i*50)+"px;background-position: "+(j*-50)+"px "+(i*-50)+"px;'></li>";
}
}
$("ul").html(str);



$("button").click(function(){
//$("li").delay(Math.random()*1000).show()
$("li").each(function(){

$(this).delay(Math.random()*1000).fadeIn(100,"easeInOutBounce");
})

})
</script>
</body>
</html>

页面滚动到顶部的效果

$('html, body').animate({scrollTop:0}, 500); //页面滚动

实例: 返回顶部

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
*{
margin: 0;
padding: 0;
}
body{
width: 100%;
height: 5000px;
background-image: linear-gradient(red,blue,orange,green,pink,yellow);
}
a{
width: 50px;
height: 50px;
color: #000;
font-size: 14px;
text-align: center;
line-height: 25px;
border: 1px solid #000;
background-color: #fff;
text-decoration: none;
position: fixed;
right: 50px;
bottom: 50px;
}
</style>
</head>
<body>
<a href="javascript:;">返回<br/>顶部</a>

<script src="js/jquery-2.2.4.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
$("a").click(function(){
//返回顶部特定写法
$("html,body").animate({
"scrollTop": 0
})
});
</script>
</body>
</html>

扫描二维码推送至手机访问。

版权声明:本文由AR编程网发布,如需转载请注明出处。

本文链接:http://www.arlanguage.com/post/4109.html

分享给朋友:

“jQuery 自定义动画——animate” 的相关文章

使用 Nginx 部署 Java web 服务

比较早之前,部署 Java web 服务只是单纯使用 Tomcat 做 Web 服务器,前后端代码融合在一个工程之中。Tomcat 启动后对外提供一个端口接收和相应 http请求。随着 Nginx 得越来越流行,同时加上其优秀的反向代理和负载均衡功能,我们在线上的 Java web 通常会结合二者,...

nginx启动、重启、关闭

一、启动cd usr/local/nginx/sbin./nginx二、重启更改配置重启nginxkill -HUP 主进程号或进程号文件路径或者使用cd /usr/local/nginx/sbin./nginx -s reload 判断配置文件是否正确nginx -t -c /usr/local/...

php高并发的瓶颈到底在哪

php高并发的瓶颈到底在哪?是同步阻塞?还是nginx+fpm不断创建-销毁进程资源过度消耗?高并发到底是什么问题,是语言问题嘛,为什么说php不适合高并发?求大佬指点从2009年后一直用lnmp,从5.2.17一直到现在的PHP7.4,做的项目无数个,大到日IP10W+、PV50W+的平台,小到日...

如何找出爬取网站的来源IP? 爬取网站源代码

1.背景最近网站数据库性能很不稳定,查询性能在某段时间很慢,服务器CPU也很高,平常时间很低,感觉被爬虫恶意搞了,因此我分析了一下最近的nginx访问日志2.方法找出访问量最大20个ip[root@100 nginx]# cat liuhaihua.access.log | awk -F "...

宝塔面板如何关闭https强制跳转http/https共存

宝塔面板如何关闭https强制跳转http/https共存在 宝塔面板 中,如果你需要关闭 HTTPS 强制跳转并实现 HTTP 和 HTTPS 共存,可以通过以下步骤完成配置:一、关闭 HTTPS 强制跳转登录宝塔面板进入宝塔后台,点击左侧菜单中的 网站。找到目标网站在网站列表中找到需要取消 HT...

09《Nginx 入门教程》Nginx 的 Http 模块介绍(下)

本小节,我们将主要介绍 Nginx 中 Http 请求 11 个阶段中的最后几个重要的阶段以及相关的模块,并演示其用法。1. try_files 阶段这个阶段又称为 precontent 阶段,是 content 阶段的前置处理阶段,该阶段主要介入的模块是 ngx_http_try_files_mo...