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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值