Home  >  Article  >  Web Front-end  >  What special effects can jQuery achieve? Use of jQuery special effects (code example)

What special effects can jQuery achieve? Use of jQuery special effects (code example)

青灯夜游
青灯夜游forward
2018-11-13 10:26:012371browse

The content of this article is to introduce what kind of special effects jQuery can achieve? Use of jQuery special effects (code example). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you. [Recommended related video tutorials: jQuery Tutorial]

1. jQuery Effect--Hide and Show

Use the hide() and show() methods to hide and show HTML elements.

Example:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>My Test JQuery</title>
    <script type="text/javascript" src="./js/jquery-1.10.2.min.js"></script>
    <script type="text/javascript" >    
        $(function(){
            $("#btnHide").click(function(){
                $("#myDiv1").hide();
                //语法格式:$(selector).hide(speed,callback);
                //可选参数1:speed 参数规定隐藏/显示的速度,可以取值:"slow"、"fast" 或毫秒。
                //可选参数2:callback 参数是隐藏或显示完成后所执行的函数名称。

                // callback的使用如下,即第2个参数是一个函数,jQuery效果执行完会执行该函数。
                // $("#myDiv1").hide(3000,function(){
                //      alert("myDiv1隐藏了");
                // });
            });
            
            $("#btnShow").click(function(){
                $("#myDiv1").show();
                //语法格式:$(selector).show(speed,callback); 
                //可选参数1:speed 参数规定隐藏/显示的速度,可以取值:"slow"、"fast" 或毫秒。
                //可选参数2:callback 参数是隐藏或显示完成后所执行的函数名称。
            });
        });
    </script>
</head>
<body>
    <button type="button" id="btnHide">隐藏</button>
    <button type="button" id="btnShow">显示</button>
    <div id="myDiv1" style="width:100px;height:50px;background-color:green"></div>
</body>
</html>

  

2. jQuery effect - fade in and fade out

 (1) The fadeIn() method is used to fade in hidden elements.

 (2) The fadeOut() method is used to fade out visible elements.

 (3) The fadeToggle() method can switch between the fadeIn() and fadeOut() methods. If the element is already faded out, add a fade in effect. If the element is already faded in, add a fade out effect.

 (4) The fadeTo() method allows a gradient to a given opacity (value between 0 and 1).

Example:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>My Test JQuery</title>
    <script type="text/javascript" src="./js/jquery-1.10.2.min.js"></script>
    <script type="text/javascript" >    
        $(function(){
            $("#btnfadeIn").click(function(){
                $("#myDiv1").fadeIn();
                //语法格式:$(selector).fadeIn(speed,callback);
                //可选参数1:speed 参数规定效果的时长。它可以取值:"slow"、"fast" 或毫秒。
                //可选参数2:callback 参数是该效果完成后所执行的函数名称。
            });
            
            $("#btnfadeOut").click(function(){
                $("#myDiv1").fadeOut();
                //语法格式:$(selector).fadeOut(speed,callback);
                //可选参数1:speed 参数规定效果的时长。它可以取值:"slow"、"fast" 或毫秒。
                //可选参数2:callback 参数是该效果完成后所执行的函数名称。
            });

            $("#btnfadeToggle").click(function(){
                $("#myDiv1").fadeToggle();
                //语法格式:$(selector).fadeToggle(speed,callback);
                //可选参数1:speed 参数规定效果的时长。它可以取值:"slow"、"fast" 或毫秒。
                //可选参数2:callback 参数是该效果完成后所执行的函数名称。
            });

            $("#btnfadeTo").click(function(){
                $("#myDiv1").fadeTo("slow",0.35); 
                //语法格式:$(selector).fadeTo(speed,opacity,callback);
                //第一个参数是必须的:传入的值可以是"slow"、"fast" 、毫秒;
                //第二个参数是必须的:传入值是透明度,取值在0-1之间
                //第三个参数是可选的:callback 参数是该效果完成后所执行的函数名称。
            });
        });
    </script>
</head>
<body>
    <button type="button" id="btnfadeIn">fadeIn</button>
    <button type="button" id="btnfadeOut">fadeOut</button>
    <button type="button" id="btnfadeToggle">fadeToggle</button>
    <button type="button" id="btnfadeTo">fadeTo</button>
    <div id="myDiv1" style="width:200px;height:100px;background-color:green"></div>
</body>
</html>

3. jQuery effect-sliding

 (1) slideDown() method is used for downward Sliding element.

 (2) The slideUp() method is used to slide the element up.

 (3) The slideToggle() method can switch between the slideDown() and slideUp() methods. If the elements are already slid down, slide them up. If the elements are already slid up, slide them down.

Example:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>My Test JQuery</title>
    <script type="text/javascript" src="./js/jquery-1.10.2.min.js"></script>
    <script type="text/javascript" >    
        $(function(){
            $("#btnslideDown").click(function(){
                $("#myDiv1").slideDown();
                //语法格式:$(selector).slideDown(speed,callback);
                //可选参数1:speed 参数规定效果的时长。它可以取值:"slow"、"fast" 或毫秒。
                //可选参数2:callback 参数是滑动完成后所执行的函数名称。
            });
            
            $("#btnslideUp").click(function(){
                $("#myDiv1").slideUp();
                //语法格式:$(selector).slideUp(speed,callback);;
                //可选参数1:speed 参数规定效果的时长。它可以取值:"slow"、"fast" 或毫秒。
                //可选参数2:callback 参数是滑动完成后所执行的函数名称。

            });

            $("#btnslideToggle").click(function(){
                $("#myDiv1").slideToggle();
                //语法格式:$(selector).slideToggle(speed,callback);;
                //可选参数1:speed 参数规定效果的时长。它可以取值:"slow"、"fast" 或毫秒。
                //可选参数2:callback 参数是滑动完成后所执行的函数名称。
            });

        });
    </script>
</head>
<body>
    <button type="button" id="btnslideDown">slideDown</button>
    <button type="button" id="btnslideUp">slideUp</button>
    <button type="button" id="btnslideToggle">slideToggle</button>
    <div id="myDiv1" style="width:200px;height:100px;background-color:green"></div>
</body>
</html>

4. jQuery Effect - Animation

The animate() method is used to create custom animations.

Example:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>My Test JQuery</title>
    <script type="text/javascript" src="./js/jquery-1.10.2.min.js"></script>
    <script type="text/javascript" >    
        $(function(){
            $("#btn_animate").click(function(){
                $("#myDiv1").animate({left:'250px'});
                //语法格式:$(selector).animate({params},speed,callback);
                //第一个参数是必须的:值是定义形成动画的 CSS 属性
                //第二个参数是可选的:传入的值是"slow"、"fast" 、毫秒
                //第三个参数是可选的:callback 参数是该效果完成后所执行的函数名称
                 
                //第一个参数的css属性也可以使用相对值,像下面这样
                //$("#myDiv1").animate({left:'250px',height:'+=10px',width:'+=10px'});
            });
            
        });
    </script>
</head>
<body>
    <button type="button" id="btn_animate">animate</button>
    <div id="myDiv1" style="top:50px;width:100px;height:50px;background-color:green;position:absolute;"></div>
</body>
</html>

5. jQuery -- Stop animation

The stop() method is used to stop animations or effects before they are completed.

The stop() method applies to all jQuery effect functions, including sliding, fading, and custom animations.

Example:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>My Test JQuery</title>
    <script type="text/javascript" src="./js/jquery-1.10.2.min.js"></script>
    <script type="text/javascript" >    
        $(function(){
            $("#btn_animate").click(function(){
                $("#myDiv1").animate({left:'250px'},5000);
            });

            $("#btn_stop").click(function(){
                $("#myDiv1").stop();
                //语法格式:$(selector).stop(stopAll,goToEnd);
                //可选参数1:规定是否应该清除动画队列。默认是 false,即仅停止活动的动画,允许任何排入队列的动画向后执行。
                //可选参数2:规定是否立即完成当前动画。默认是 false。
            });
            
        });
    </script>
</head>
<body>
    <button type="button" id="btn_animate">animate</button>
    <button type="button" id="btn_stop">Stop</button>
    <div id="myDiv1" style="top:50px;width:100px;height:50px;background-color:green;position:absolute;"></div>
</body>
</html>

6, jQuery -- Chain Programming

Chain Programming :Run multiple jQuery commands on the same element, one right after the other. This way, the browser doesn't have to look for the same element multiple times. To link an action, you simply append the action to the previous action.

Example:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>My Test JQuery</title>
    <script type="text/javascript" src="./js/jquery-1.10.2.min.js"></script>
    <script type="text/javascript" >    
        $(function(){  
            $("#btnLink").click(function(){
                $("#myDiv1").css("background-color","yellow").slideUp(2000).slideDown(2000);
            });
        });
    </script>
</head>
<body>
    <button type="button" id="btnLink">链式编程</button>
    <div id="myDiv1" style="width:100px;height:50px;background-color:green"></div>
</body>
</html>

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's learning. For more jQuery special effects, it is recommended to visit: js special effects collection!

The above is the detailed content of What special effects can jQuery achieve? Use of jQuery special effects (code example). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete