Home >Web Front-end >JS Tutorial >jQuery effect implementation

jQuery effect implementation

一个新手
一个新手Original
2017-10-10 10:13:551397browse


Hide/Show

The hide() and show() functions in jQuery are used to control the hiding and display of elements, calling format As follows:

$(selector).hide(speed,callback);
$(selector).show(speed,callback);

Combining the above two methods, it is the two parameters of the toggle function
. The speed parameter controls the speed at which the element is hidden/displayed (the value can be slow, fast, or The time you define, the unit is milliseconds Please note that slow and fast must be enclosed in quotation marks ), callback can be passed in to the function, which is triggered after the hide or show action is completed, for example:

<!DOCTYPE html><html><head>
  <title>jQuery-效果</title>
  <script type="text/javascript"  src="jquery-3.2.1.js"></script>
  <style type="text/css">
    *{      
    margin: 0px;      
    padding: 0px;      
    font-size: 20px;      
    font-weight: bold;    
    }
  </style></head><body>
  <p id="p1">Hello jQuery!</p>
  <button id="btn_hide">HIDE</button>
  <button id="btn_show">SHOW</button>
  <button id="btn_toggle">TOGGLE</button><!-- 使用jQuery进行元素的隐藏与显示的基本格式是: 
  $(selector).event(speed,callback) -->

  <script type="text/javascript">
    $(document).ready(function(){
      $("#btn_hide").click(function(){
        $("#p1").hide(2000,function(){
          alert("HIDE!");         //动作完成之后弹出消息框
        });
      });
      $("#btn_show").click(function(){
        $("#p1").show();
      });
      $("#btn_toggle").click(function(){
        $("#p1").toggle();
      });
    });  </script></body></html>

Fade in and out effect

The fade in and fade out effect is basically the same as the hide and show functions, but in addition to this there is another function, fadeTo(), this function is used to change elements Transparent, the usage format is as follows:

$(selector).fadeTo(speed,opacity,callback);

The first two parameters must be passed in, and opacity is a decimal between 0 and 1. The example is as follows:

<!DOCTYPE html><html><head>
  <title>jQuery-效果</title>
  <script type="text/javascript"  src="jquery-3.2.1.js"></script>
  <style type="text/css">
    *{      
    margin: 5px;     
    padding: 0px;      
    font-size: 20px;      
    font-weight: bold;    
    }
  </style></head><body>
  <p id="p2">Hello jQuery!</p>
  <button id="btn_fadein">FADEIN</button>
  <button id="btn_fadeout">FADEOUT</button>
  <button id="btn_fadetoggle">FADETOGGLE</button>
  <button id="btn_fadeto">FADETO</button><!-- 使用jQuery进行元素的隐藏与显示的基本格式是: $(selector).event(speed,callback) --><!-- 其实jQuery的很多效果都是这样的用法,比如淡入淡出的效果也是这样的参数 -->

  <script type="text/javascript">
    $(document).ready(function(){
      $("#btn_fadein").click(function(){         //淡入淡出的效果展示
        $("#p2").fadeIn(1000);
      });
      $("#btn_fadeout").click(function(){
        $("#p2").fadeOut(1000);
      });
      $("#btn_fadetoggle").click(function(){
        $("#p2").fadeToggle(1000);
      });
      $("#btn_fadeto").click(function(){
        $("#p2").fadeTo("slow",0.5);          //注意slow要加上引号!!,前两个参数是必须要传入的
      });
    });  </script></body></html>

The above is the detailed content of jQuery effect implementation. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn