jQuery中的动画与特效
1.显示和隐藏hide()和show()
对于动画来说,显示和隐藏是最基本的效果之一,简单介绍jQuery的显示和隐藏。
<script type="text/javascript">
$(function() {
$("input:first").click(function() {
$("p").hide(); //隐藏
});
$("input:last").click(function() {
$("p").show(); //显示
});
});
</script>
<input type="button" value="Hide">
<input type="button" value="Show">
<p>点击按钮,看看效果</p>
<div><em>本节主要降级和学习jQuery的自动显隐,渐入渐出、飞入飞出。自定义动画等。 .显示和隐藏hide()和show() 对于动画来说,显示和隐藏是最基本的效果之一,本节简单介绍jQuery的显示和隐藏。</em>
</div>
以上是对hide()和show()函数的测试。
2.使用show()、hide()和toggle()方法
上个例子对show()和hide()方法做了简单介绍,其实这两个方法可以接受参数控制显隐藏过程。
语法如下
show(duration,[callback]);
hide(duration,[callback]);
其中,duration表示动画执行时间的长短,可以表示速度的字符串,包括slow(0.6秒),normal(0.4秒),fast(0.2秒).也可以是表示时间的整数(毫秒)。callback是可选的回调函数。在动画完成之后执行
<script type="text/javascript">
$(function() {
$("input:first").click(function() {
$("p").hide(); //隐藏
});
$("input:last").click(function() {
$("p").show(); //显示
});
});
</script>
例子和第一个例子相同,只是对hide()和show()增加了时间参数。其实toogle()也可以加入事件参数。
如果直接调用toogle() 也可以实现显示和隐藏
$(function () {
$("#mybtn").toggle(function () {
$(".hidden").toggle();//获取到隐藏的将其显示
$(this).attr("src", "image/up.jpg");
}, function () {
$(".hidden").toggle();//获取到显示的将其隐藏
$(this).attr("src", "image/down.jpg");
});
});
3.使用fadeIn()和fadeOut()方式
对于动画效果显隐,jQuery还提供了fadeIn()个fadeOut()这两个实用的方法,他们的动画效果类似褪色,语法与show()和hide()完全相同。
fadeIn(duration, [callback]);
fadeOut(duration,
[callback]);
例子
<script type="text/javascript">
$(function() {
$("input:eq(0)").click(function() {
$("img").fadeOut(); //逐渐fadeOut
});
$("input:eq(1)").click(function() {
$("img").fadeIn(); //逐渐fadeIn
});
});
</script>
<img src="http://study.ahthw.com/gtimg/book/2014/11/201411120129479688.jpg">
<input type="button" value="Hide">
<input type="button" value="Show">
fadeTo()方法的使用。
fadeTo() 方法将被选元素的不透明度逐渐地改变为指定的值。
例子:
<script type="text/javascript">
$(function() {
$("input:eq(0)").click(function() {
$("img").fadeOut();
});
$("input:eq(1)").click(function() {
$("img").fadeIn();
});
$("input:eq(2)").click(function() {
$("img").fadeTo(, 0.5);
});
$("input:eq(3)").click(function() {
$("img").fadeTo(, );
});
});
</script>
<p><img src="03.jpg"></p>
<input type="button" value="FadeOut">
<input type="button" value="FadeIn">
<input type="button" value="FadeTo 0.5">
<input type="button" value="FadeTo 0">
fadeOut相关参数
speed
可选。规定元素从当前透明度到指定透明度的速度。
可能的值:
毫秒 (比如 1500)
"slow"
"normal"
"fast"
opacity 必需。规定要淡入或淡出的透明度。必须是介于
0.00 与 1.00 之间的数字。
callback
可选。fadeTo 函数执行完之后,要执行的函数。
如需学习更多有关 callback 的内容,请访问我们的 jQuery Callback 这一章。
除非设置了 speed 参数,否则不能设置该参数。
4.幻灯片slideUp和slideDown效果
<script type="text/javascript">
$(function() {
$("input:eq(0)").click(function() {
$("div").add("img").slideUp();
});
$("input:eq(1)").click(function() {
$("div").add("img").slideDown();
});
$("input:eq(2)").click(function() {
$("div").add("img").hide();
});
$("input:eq(3)").click(function() {
$("div").add("img").show();
});
});
</script>
<input type="button" value="SlideUp">
<input type="button" value="SlideDown">
<input type="button" value="Hide">
<input type="button" value="Show"><br>
<div></div><img src="04.jpg">
5.自定义动画
考虑到框架的通用性及代码文件的大小,jQuery不能涵盖所有的动画效果,但它提供了animate()方法,能够使开发者自定义动画。本节主要通过介绍animate()方法的两种形式及应用。
animate()方法给开发者很大的空间。它一共有两种形式。第一种形式比较常用。用法如下
animate(params,[duration],[easing],[callback])
其中params为希望进行变幻的css属性列表,以及希望变化到的最终值,duration为可选项,与show()/hide()的参数含义完全相同。easing为可选参数,通常供动画插件使用。
用来控制节奏的变化过程。jQuery中只提供了linear和swing两个值.callback为可选的回调函数。在动画完成后触发。
需要注意。params中的变量遵循camel命名方式。例如paddingLeft不能写成padding-left.另外,params只能是css中用数值表示的属性。例如width.top.opacity等
像backgroundColor这样的属性不被animate支持。
<style>
div {
background-color: #FFFF00;
height: 40px;
width: 80px;
border: 1px solid #;
margin-top: 5px;
padding: 5px;
text-align: center;
}
</style>
<script type="text/javascript">
$(function() {
$("button").click(function() {
$("#block").animate({
opacity: "0.5",
width: "80%",
height: "100px",
borderWidth: "5px",
fontSize: "30px",
marginTop: "40px",
marginLeft: "20px"
}, );
});
});
</script>
<button id="go">Go>></button>
<div id="block">动画!</div>
在params中,jQuery还可以用“+=”或者"-="来表示相对变化。如
<style>
div {
background-color: #FFFF00;
height: 40px;
width: 80px;
border: 1px solid #;
margin-top: 5px;
padding: 5px;
text-align: center;
position: absolute;
}
</style>
<script type="text/javascript">
$(function() {
$("button:first").click(function() {
$("#block").animate({
left: "-=80px" //相对左移
}, );
});
$("button:last").click(function() {
$("#block").animate({
left: "+=80px" //相对右移
}, );
});
});
</script>
<button >Go>></button>
<button >Go>></button>
<div id="block">动画!</div>
先将div进行绝对定位,然后使用animate()中的-=和+=分别实现相对左移和相对右移。
animate()方法还有另外一种形式,如下所示:
animate(params,options)
其中,params与第一种形式完全相同,options为动画可选参数列表,主要包括duration,esaing,callback,queue等,其中duration.easing.callback与第一种形式完全一样,queue为布尔值,表示当有多个
animate()组成jQuery时,当前animate()紧接这下一个animate(),是按顺序执行(true)还是同时触发false
如下例子,展示了animate()第二种用法。
<style>
div {
background-color: #FFFF22;
width: 100px;
text-align: center;
border: 2px solid #;
margin: 3px;
font-size: 13px;
font-family: Arial, Helvetica, sans-serif;
}
input {
border: 1px solid #;
}
</style>
<script type="text/javascript">
$(function() {
$("input:eq(0)").click(function() {
//第一个animate与第二个animate同时执行,然后再执行第三个
$("#block1").animate({
width: "90%"
}, {
queue: false,
duration:
})
.animate({
fontSize: "24px"
}, )
.animate({
borderRightWidth: "20px"
}, );
});
$("input:eq(1)").click(function() {
//依次执行三个animate
$("#block2").animate({
width: "90%"
}, )
.animate({
fontSize: "24px"
}, )
.animate({
borderRightWidth: "20px"
}, );
});
$("input:eq(2)").click(function() {
$("input:eq(0)").click();
$("input:eq(1)").click();
});
$("input:eq(3)").click(function() {
//恢复默认设置
$("div").css({
width: "",
fontSize: "",
borderWidth: ""
});
});
});
</script>
<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>
以上两个div块同时运用了三个动画效果,其中第一个div快的第一个动画添加了queue:false参数,使得前两项两个动画同时执行。
jQuery中的动画与特效的更多相关文章
- jQuery学习笔记(四)jQuery中的动画
目录 show()方法和hide()方法 fideIn()方法和fadeOut()方法 slideUp方法和slideDown()方法 自定义动画方法animate toogle(),slideTog ...
- 第五章 jQuery中的动画
通过jQuery中的动画方法,能轻松地为网页添加精彩的视觉效果,给用户一种全新体验. 1.show()方法和hide()方法 该方法的功能与css()方法设置display属性效果相同. 给show( ...
- jQuery中animate动画第二次点击事件没反应
jQuery中animate动画第二次点击事件没反应 用animate做点击翻页动画时发现第二次点击事件动画没反应,而第一次点击有动画效果,代码如下: 复制代码 代码如下: $(".page ...
- jQuery中的动画理论干货
[jQuery中的动画] 通过jQuery动画能够轻松地为页面添加精彩的视觉效果 [show()方法和hide()方法]1.show()方法和hide()方法是jQUERY中最基本的动画方法,相当于在 ...
- 史上最全的CSS hack方式一览 jQuery 图片轮播的代码分离 JQuery中的动画 C#中Trim()、TrimStart()、TrimEnd()的用法 marquee 标签的使用详情 js鼠标事件 js添加遮罩层 页面上通过地址栏传值时出现乱码的两种解决方法 ref和out的区别在c#中 总结
史上最全的CSS hack方式一览 2013年09月28日 15:57:08 阅读数:175473 做前端多年,虽然不是经常需要hack,但是我们经常会遇到各浏览器表现不一致的情况.基于此,某些情况我 ...
- jQuery笔记(四)jQuery中的动画
jQuery最吸引人的地方莫过于能做出绚丽的动画了,也是能极大提高用户体验的地方,这次我们就来一探jQuery中的动画! 一. show()方法和hide()方法 show()方法与hide()方法是 ...
- JQuery中的动画
一.show()方法和hide()方法 这两种方法是jQuery动画的最基本方法.当为元素调用show方法时相当于将该元素的display样式改为block或者inline,同理,如果当元素调用hid ...
- 【学习笔记】jQuery中的动画与效果
1.基本效果 匹配元素从左上角开始变浓变大或缩小到左上角变淡变小 ①隐藏元素 除了可以设置匹配元素的display:none外,可以用以下函数 hide(speed,[callback]) 返回值: ...
- jQuery中的动画与效果
1.基本效果 匹配元素从左上角开始变浓变大或缩小到左上角变淡变小 ①隐藏元素 除了可以设置匹配元素的display:none外,可以用以下函数 hide(speed,[callback]) 返回值: ...
随机推荐
- 【转】Objective-C Class Dump
(转自:http://blog.sina.com.cn/s/blog_4431c7610100x15t.html) class dump是一个命令行工具,用来监测存储在Mach-O二进制文件理的Obj ...
- atom 安装插件出现 EIO 错误
今天给 atom 安装一个插件autocomplete-python的时候出现错误 npm ERR! Windows_NT 6.1.7600 npm ERR! argv "C:\\Progr ...
- 关于创建可执行的jar文件(assembly)
java利用maven生成一个jar包,如何自动生成清单属性文件(MANIFEST.MF),如何解决jar依赖问题? 办法很简单: 只需在pom.xml文件中配置如下plugin即可: <plu ...
- [原创]Andorid DexClassLoader的创建过程解析(基于5.0)
做Android插件框架时,经常会用到dex的动态加载,就要直接或间接的使用DexClassLoader,在new DexClassLoader的时候Android系统做了很多工作,下面我们详细分析一 ...
- [PaPaPa][需求说明书][V2.0]
前 言 大家好,我是“今晚打老虎”. 什么? 你问我为什么这次亮字号了? 还不是因为哥太出名了,即使我不亮你们也知道是我写的了. 自从发布了V1.0版本之后.群里又进来好多人.30K大大分发的任务 ...
- IPv4分析
IPv4的头部格式: 1. Version 版本号,默认是4. 2. IHL(Internet Header Length) 就是IPv4头部长度.这个长度的单位是32bit,一般是5,那么头部的长度 ...
- 如何重置CentOS 7的Root密码?设置CentOS 7的Root密码的方法与步骤
- Git使用总结
一.Git的特性 Speed 速度(git是用c语言写的.一般都是提交到本地) Simple design Strong support for non-linear development (tho ...
- Safari下默认10位数字为电话号码,点击拨号
<meta content="telephone=no" name="format-detection"/>
- WPF中多个RadioButton绑定到一个属性
如图样: 在View中: <RadioButton IsChecked="{Binding Option, Converter={cvt:EnumToBooleanConverter} ...