IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> JavaScript知识库 -> 2021-11-09 jQuery常用动画效果 -> 正文阅读

[JavaScript知识库]2021-11-09 jQuery常用动画效果

目录

1.css()方法

2.eq()方法

3.show()方法和hide()方法

4.fadeOut()、fadeIn()和fadeTo()

5.slideUp()、slideDown和slideToggle()

6.text()方法

7.animate()方法

8.queue()方法

附录代码:


1.css()方法

当方法参数为一个参数时获取样式的值,两个参数时候可以设置属性的参数值。 ? ?

? //获取内容div对象
 ? ? ? ?// let $speech = $("div.speech");
 ? ? ? ?// //获取最初文字大小
 ? ? ? ?let defaultSize = $speech.css("font-size");
?
 ? ? ? ?$("#switcher button").click(function() {
 ? ? ? ? ?//parseFloat会将“12px”转换为“12”
 ? ? ? ? ?//$speech.css("font-size") 取得 12px
 ? ? ? ? ?let num = parseFloat($speech.css("font-size"));
 ? ? ? ? ?switch(this.id){
 ? ? ? ? ? ?case "switcher-large":
 ? ? ? ? ? ? ?num *= 1.4;
 ? ? ? ? ? ? ?break;
 ? ? ? ? ? ?case "switcher-small":
 ? ? ? ? ? ? ?num /= 1.4;
 ? ? ? ? ? ? ?break;
 ? ? ? ? ? ?default:
 ? ? ? ? ? ? ?num   = parseFloat(defaultSize);
 ? ? ? ?  }
 ? ? ? ? ?$speech.css("font-size",num+"px");
 ? ? ?  });

2.eq()方法

eq():获取第N个元素


 ? ? ? ?//需求2
 ? ? ? ?//eq方法起始下标从0开始,eq(1)则获取第二段落
 ? ? ? ?$("p").eq(1).hide();    //隐藏段落2
 ? ? ? ?$("a.more").click(function(e) {
 ? ? ? ? ? ?e.preventDefault(); //链接失效
 ? ? ? ? ? ?$("p").eq(1).show();    //段落2显示
 ? ? ? ? ? ?$(this).hide();
 ? ? ?  });

3.show()方法和hide()方法

show()显示隐藏的匹配元素。

hide()隐藏元素

 //需求3,slow 600毫秒,fast 200毫秒,也可自己指定毫秒数,其他字符400毫秒
 ? ? ? ?$("p").eq(1).hide();    //隐藏段落2
 ? ? ? ?$("a.more").click(function(e) {
 ? ? ? ? ?e.preventDefault();   //链接失效
 ? ? ? ? ?$("p").eq(1).show("slow");    //段落2显示
 ? ? ? ? ?$(this).hide();
 ? ? ?  });

4.fadeOut()、fadeIn()和fadeTo()

fadeOut(speed, [callback])淡出,第一个参数为速度,第二个参数为动画完成时要执行的函数

fadeIn(speed, [callback]):淡入

fadeTo(speed, opacity, [callback]):把所有匹配元素的不透明度以渐进方式调整到指定的不透明度,并在动画完成后可选地触发一个回调函数。

这个动画只调整元素的不透明度,也就是说所有匹配的元素的高度和宽度不会发生变化。

 ?//需求4,fadeOut() 淡出
 ? ? ? ?$("p").eq(1).hide();    //隐藏段落2
 ? ? ? ?$("a.more").click(function(e) {
 ? ? ? ? ? ?e.preventDefault(); //链接失效
 ? ? ? ? ? ?$("p").eq(1).fadeIn("slow");    //段落2显示
 ? ? ? ? ? ?$(this).hide();
 ? ? ?  });
?
 ? //需求18
 ? ? ? ?$("div.label").click(function(e) {
 ? ? ? ? ?//console.log($("div.speech p").outerHeight())
 ? ? ? ? ?var paraWidth = $("div.speech p").outerWidth();
 ? ? ? ? ?var $switcher = $(this).parent();
 ? ? ? ? ?var switcherWidth = $switcher.outerWidth();
 ? ? ? ? ?$switcher.css("position","relative")
 ? ? ? ? ? ? ? ?  .fadeTo("fast",0.5)
 ? ? ? ? ? ? ? ? ?//duration动画的时间,queue与其他动画同时执行
 ? ? ? ? ? ? ? ?  .animate({left:paraWidth-switcherWidth},{duration:2000,queue:false})
 ? ? ? ? ? ? ? ?  .fadeTo("slow",1.0) ?//速度和透明度,第三个参数为执行函数
 ? ? ? ? ? ? ? ?  .slideUp("slow",function(){
 ? ? ? ? ? ? ? ? ? ?$switcher.css("background-color","#f00");
 ? ? ? ? ? ? ? ?  })
 ? ? ? ? ? ? ? ?  .slideDown("slow");
 ? ? ?  });

5.slideUp()、slideDown和slideToggle()

slideUp(speed, [callback]):划上,通过高度变化(向上减小)来动态地隐藏所有匹配的元素,在隐藏完成后可选地触发一个回调函数。

这个动画效果只调整元素的高度,可以使匹配的元素以“滑动”的方式隐藏起来。

slideDown(speed, [callback])划下,通过高度变化(向下增大)来动态地显示所有匹配的元素,在显示完成后可选地触发一个回调函数。

slideToggle(speed, [callback]):通过高度变化来切换所有匹配元素的可见性,并在切换完成后可选地触发一个回调函数。

?//需求5,slideUp()划上效果
 ? ? ? ?$("p").eq(1).hide();    //隐藏段落2
 ? ? ? ?$("a.more").click(function(e) {
 ? ? ? ? ?e.preventDefault();   //链接失效
 ? ? ? ? ?$("p").eq(1).slideDown(2000); //段落2显示
 ? ? ? ? ?$(this).hide();
 ? ? ?  });
 ?//需求7  slideToggle()
 ? ? ? ?var $firstPara = $("p").eq(1);  //得到第二个段落
 ? ? ? ?$firstPara.hide();          //隐藏它
 ? ? ? ?$("a.more").click(function(e) {
 ? ? ? ? ?e.preventDefault();
 ? ? ? ? ?//slideToggle通过高度变化来切换
 ? ? ? ? ?$firstPara.slideToggle("slow");   //显示隐藏自动切换
 ? ? ? ? ?var $like = $(this);
 ? ? ? ? ?if($like.text() == "read more"){
 ? ? ? ? ? ?$(this).text("read less");
 ? ? ? ?  }else{
 ? ? ? ? ? ?$(this).text("read more");
 ? ? ? ?  }
 ? ? ?  });

6.text()方法

取得所有匹配元素的内容。

如果无参数则可以获取div或标签直接的文本内容, ? 如果有参数,则可以设置中间内容进行替换

?//需求6:
 ? ? ? ?//text(),如果无参数则可以获取div或标签直接的文本内容,
 ? ? ? ?// 如果有参数,则可以设置中间内容进行替换
 ? ? ? ?var $firstPara = $("p").eq(1);  //得到第二个段落
 ? ? ? ?$firstPara.hide();          //隐藏它
 ? ? ? ?$("a.more").click(function(e) {
 ? ? ? ? ?e.preventDefault();
 ? ? ? ? ?if($firstPara.is(":hidden")){ //如果段落是隐藏的
 ? ? ? ? ? ?$firstPara.fadeIn("slow");  //显示并更换链接文字
 ? ? ? ? ? ?$(this).text("read less");
 ? ? ? ?  }else{
 ? ? ? ? ? ?$firstPara.fadeOut("slow");
 ? ? ? ? ? ?$(this).text("read more");
 ? ? ? ?  }
 ? ? ?  });

7.animate()方法

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

animate(params, options):

参数列表:

params

一组包含作为动画属性和终值的样式属性和及其值的集合

duration *(可选)*

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

easing *(可选)*

要使用的擦除效果的名称(需要插件支持).默认jQuery提供"linear" 和 "swing".

callback *(可选)*

在动画完成时执行的函数

options

一组包含动画选项的值的集合。

 //需求8:
 ? ? ? ?var $firstPara = $("p").eq(1);  //得到第二个段落
 ? ? ? ?$firstPara.hide();          //隐藏它
 ? ? ? ?$("a.more").click(function(e) {
 ? ? ? ? ?e.preventDefault();
 ? ? ? ? ?//animate第一个参数可以传入一个对象来切换属性值,第二个参数可以设置切换的速度
 ? ? ? ? ? ?//显示隐藏自动切换,height:toggle值表  示高度从0变到最大值      
 ? ? ? ? ?$firstPara.animate({height:"toggle"},"slow");      ?                                   /* $firstPara.animate({height:"toggle",opacity:"toggle"},3000);*/  
 ? ? ? ? ?var $like = $(this);
 ? ? ? ? ?if($like.text() == "read more"){
 ? ? ? ? ? ?$(this).text("read less");
 ? ? ? ?  }else{
 ? ? ? ? ? ?$(this).text("read more");
 ? ? ? ?  }
 ? ? ?  });
?
 //需求12
?
 ? ? ? ?$("div.label").click(function(e) {//同步
 ? ? ? ? ?var paraWidth = $("div.speech p").outerWidth();
 ? ? ? ? ?var $switcher = $(this).parent();
 ? ? ? ? ?var switcherWidth = $switcher.outerWidth();
 ? ? ? ? ?$switcher.css("position","relative")
 ? ? ? ? ? ? ? ?  .animate({left:paraWidth-switcherWidth},"slow")
 ? ? ? ? ? ? ? ?  .animate({height:"+=20px"},"slow")
 ? ? ? ? ? ? ? ?  .animate({borderWidth:"5px"},"slow");
 ? ? ?  });
?
 ?//需求14
 ? ? ? ?$("div.label").click(function(e) {
 ? ? ? ? ?var paraWidth = $("div.speech p").outerWidth();
 ? ? ? ? ?var $switcher = $(this).parent();
 ? ? ? ? ?var switcherWidth = $switcher.outerWidth();
 ? ? ? ? ?$switcher.css("position","relative")
 ? ? ? ? ? ? ? ?  .fadeTo("fast",0.5)
 ? ? ? ? ? ? ? ? ?//duration:动画的时间,queue与其他动画同时执行
 ? ? ? ? ? ? ? ?  .animate({left:paraWidth-switcherWidth},{duration:2000,queue:false})
 ? ? ? ? ? ? ? ?  .fadeTo("slow",1.0)
 ? ? ? ? ? ? ? ?  .slideUp("slow")
 ? ? ? ? ? ? ? ?  .slideDown("slow");
 ? ? ?  });

8.queue()方法

在匹配的元素的队列最后添加一个函数,使队列函数中的动画和其他动画同时执行

queue(name, callback):

 //需求15
 ? ? ? ?$("div.label").click(function(e) {
 ? ? ? ? ?var paraWidth = $("div.speech p").outerWidth();
 ? ? ? ? ?var $switcher = $(this).parent();
 ? ? ? ? ?var switcherWidth = $switcher.outerWidth();
 ? ? ? ? ?$switcher.css("position","relative")
 ? ? ? ? ? ? ? ?  .fadeTo("fast",0.5)
 ? ? ? ? ? ? ? ? ?//duration动画的时间,queue与其他动画同时执行
 ? ? ? ? ? ? ? ?  .animate({left:paraWidth-switcherWidth},{duration:2000,queue:false})
 ? ? ? ? ? ? ? ?  .fadeTo("slow",1.0)
 ? ? ? ? ? ? ? ?  .slideUp("slow")
 ? ? ? ? ? ? ? ?  .queue(function(next){
 ? ? ? ? ? ? ? ? ? ?//$switcher.css({"background-color":"#F00"});
 ? ? ? ? ? ? ? ? ? ?$('p').eq(1).slideUp("fast").slideDown("fast")
 ? ? ? ? ? ? ? ? ? ?next();     //与后续链接在一起
 ? ? ? ? ? ? ? ?  })
 ? ? ? ? ? ? ? ?  .slideDown("slow");
 ? ? ?  })

附录代码:

<!DOCTYPE html>

<html lang="en">
  <head>
    <meta charset="utf-8">
	  <title>Abraham Lincoln's Gettysburg Address</title>

    <link rel="stylesheet" href="style/04.css" type="text/css" />
    <script src="js/jquery.js"></script>
  </head>
  <body>
    <div id="container">
      <h2>Abraham Lincoln's Gettysburg Address</h2>
      <div id="switcher">
        <div class="label">Text Size</div>
        <button id="switcher-default">Default</button>
        <button id="switcher-large">Bigger</button>
        <button id="switcher-small">Smaller</button>
      </div>
      <div class="speech">
        <p>Fourscore and seven years ago our fathers brought forth on this continent a new nation, conceived in liberty, and dedicated to the proposition that all men are created equal.</p>
        <p>Now we are engaged in a great civil war, testing whether that nation, or any nation so conceived and so dedicated, can long endure. We are met on a great battlefield of that war. We have come to dedicate a portion of that field as a final resting-place for those who here gave their lives that the nation might live. It is altogether fitting and proper that we should do this. But, in a larger sense, we cannot dedicate, we cannot consecrate, we cannot hallow, this ground.</p>
        <a href="#" class="more">read more</a>
        <p>The brave men, living and dead, who struggled here have consecrated it, far above our poor power to add or detract. The world will little note, nor long remember, what we say here, but it can never forget what they did here. It is for us the living, rather, to be dedicated here to the unfinished work which they who fought here have thus far so nobly advanced.</p>
        <p>It is rather for us to be here dedicated to the great task remaining before us&mdash;that from these honored dead we take increased devotion to that cause for which they gave the last full measure of devotion&mdash;that we here highly resolve that these dead shall not have died in vain&mdash;that this nation, under God, shall have a new birth of freedom and that government of the people, by the people, for the people, shall not perish from the earth.</p>
      </div>
    </div>
  </body>
</html>
/***************************************
   Default Styles
************************************** */

html, body {
  margin: 0;
  padding: 0;
}

body {
  font: 62.5% Verdana, Helvetica, Arial, sans-serif;
  color: #000;
  background: #fff;
}
#container {
  font-size: 1.2em;
  margin: 10px 2em;
}

h1 {
  font-size: 2.5em;
  margin-bottom: 0;
}

h2 {
  font-size: 1.3em;
  margin-bottom: .5em;
}
h3 {
  font-size: 1.1em;
  margin-bottom: 0;
}

code {
  font-size: 1.2em;
}

a {
  color: #06581f;
}


/***************************************
   Chapter Styles
************************************** */

#switcher {
  position: relative;
  width: 300px;
  padding: .5em;
  border: 1px solid #777;
}
.label {
  width: 130px;
  margin: .5em 0;
  cursor: pointer;
}

  JavaScript知识库 最新文章
ES6的相关知识点
react 函数式组件 & react其他一些总结
Vue基础超详细
前端JS也可以连点成线(Vue中运用 AntVG6)
Vue事件处理的基本使用
Vue后台项目的记录 (一)
前后端分离vue跨域,devServer配置proxy代理
TypeScript
初识vuex
vue项目安装包指令收集
上一篇文章      下一篇文章      查看所有文章
加:2021-11-10 12:19:03  更:2021-11-10 12:20:10 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/10 19:35:20-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码