jQuery效果

本文介绍了jQuery中实现元素隐藏、显示、淡入淡出、滑动及自定义动画的方法,并提供了详细的示例代码。

1.jQuery效果-隐藏和显示

我们可以通过jQuery的hide()和show()方法来隐藏和显示HTML元素
语法如下

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

其中speed参数为可选参数,用其来确定隐藏/显示的速度,可以取以下值’slow’,’fast’,或毫秒
callback也为可选参数,它为操作完成后所执行的回调函数

无参的hide(),show()方法实例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src = 'jquery.js'></script>
    <script>
        $(document).ready(function () {
            $('.hide').click(function () {
                $('p').hide();
            });
            $('.show').click(function () {
                $('p').show();
            });
        });
    </script>
</head>
<body>
    <p>如果点隐藏我就会消失,点显示我就会出来</p>
    <button class="hide" type="button">隐藏</button>
    <button class="show" type="button">显示</button>
</body>
</html>

我们也可以使用toggle()方法来切换hide()和show()方法

toggle实例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src = 'jquery.js'></script>
    <script>
        $(document).ready(function () {
            $('button').click(function () {
                $('p').toggle();
            });
        });
    </script>
</head>
<body>
    <p>切换</p>
    <button type="button">切换</button>
</body>
</html>

2.jQuery效果-淡入淡出

通过jQuery我们可以实现元素的淡入淡出效果
jQuery拥有下面四种fade方法

.fadeIn()
.fadeOut()
.fadeToggle()
.fadeTo()

(1)fadeIn
fadeIn的语法如下

$(selector).fadeIn(speed,callback);

实例如下

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src = 'jquery.js'></script>
    <script>
        $(document).ready(function () {
            $('button').click(function () {
                $('.div1').fadeIn();
                $('.div2').fadeIn('slow');
                $('.div3').fadeIn(3000);
            });
        });
    </script>
</head>


    <button type="button">点击这里使三个矩形淡出</button>
    <br><br>
    <div class="div1" style="width: 80px;height: 80px;display: none;background-color: red;">

    </div><br>
    <div class="div2" style="width: 80px;height: 80px;display: none;background-color: green;">

    </div><br>
    <div class="div3" style="width: 80px;height: 80px;display: none;background-color: blue;">

    </div><br>
</body>
</html>

(2)fadeOut
fadeOut用于淡出可见元素

实例如下

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src = 'jquery.js'></script>
    <script>
        $(document).ready(function () {
            $('button').click(function () {
                $('.div1').fadeOut();
                $('.div2').fadeOut('slow');
                $('.div3').fadeOut(3000);
            });
        });
    </script>
</head>


    <button type="button">点击这里使三个矩形淡出</button>
    <br><br>
    <div class="div1" style="width: 80px;height: 80px;background-color: red;">

    </div><br>
    <div class="div2" style="width: 80px;height: 80px;background-color: green;">

    </div><br>
    <div class="div3" style="width: 80px;height: 80px;background-color: blue;">

    </div><br>
</body>
</html>

(3)fadeToggle
fadeToggle方法可以在fadeIn()与fadeOut()方法之间进行切换

实例如下

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src = 'jquery.js'></script>
    <script>
        $(document).ready(function () {
            $('button').click(function () {
                $('.div1').fadeToggle();
                $('.div2').fadeToggle('slow');
                $('.div3').fadeToggle(3000);
            });
        });
    </script>
</head>


    <button type="button">点击这里使三个矩形淡入淡出</button>
    <br><br>
    <div class="div1" style="width: 80px;height: 80px;background-color: red;">

    </div><br>
    <div class="div2" style="width: 80px;height: 80px;background-color: green;">

    </div><br>
    <div class="div3" style="width: 80px;height: 80px;background-color: blue;">

    </div><br>
</body>
</html>

(4)fadeTo
fadeTo()方法允许渐变为给定的不透明度

实例如下

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src = 'jquery.js'></script>
    <script>
        $(document).ready(function () {
            $('button').click(function () {
                $('.div1').fadeTo('slow',0.15);
                $('.div2').fadeTo('slow',0.4);
                $('.div3').fadeTo('slow',0.7);
            });
        });
    </script>
</head>


    <button type="button">点击这里使三个矩形淡出</button>
    <br><br>
    <div class="div1" style="width: 80px;height: 80px;background-color: red;">

    </div><br>
    <div class="div2" style="width: 80px;height: 80px;background-color: green;">

    </div><br>
    <div class="div3" style="width: 80px;height: 80px;background-color: blue;">

    </div><br>
</body>
</html>

3.jQuery效果-滑动

我们可以在元素上创建滑动效果
jQuery拥有以下滑动方法

.slideDown()
.slideUp()
.slideToggle()

slideToggle实例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src = 'jquery.js'></script>
    <script>
        $(document).ready(function () {
            $('button').click(function () {
                $('.div1').fadeTo('slow',0.15);
                $('.div2').fadeTo('slow',0.4);
                $('.div3').fadeTo('slow',0.7);
            });
        });
    </script>
</head>


<button type="button">点击这里使三个矩形淡出</button>
<br><br>
<div class="div1" style="width: 80px;height: 80px;background-color: red;">

</div><br>
<div class="div2" style="width: 80px;height: 80px;background-color: green;">

</div><br>
<div class="div3" style="width: 80px;height: 80px;background-color: blue;">

</div><br>
</body>
</html>

4.jQuery效果-动画

jQuery的animate()方法用于创建自定义动画
语法

$(selector).animate({params},speed,callback);

.必须的params参数定义形成动画的CSS属性
.可选的speed参数规定效果的时长
.可选的callback参数是动画完成后执行的回调

(1)下面的例子演示animate()方法的简单应用,它把div元素移动到左边,知道left属性等于250像素为止
实例如下

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src = 'jquery.js'></script>
    <script>
        $(document).ready(function () {
            $('button').click(function () {
                $('div').animate({left:'300px'},4000);
            });
        });
    </script>
</head>
<body>

    <button type="button">开始动画</button>
    <br><br>
    <div style="background: #98bf21;height: 100px;width: 100px;position: absolute;">
    </div>
</body>
</html>

(2)生成动画的过程中我们可以使用多个属性
实例如下

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src = 'jquery.js'></script>
    <script>
        $(document).ready(function () {
            $('button').click(function () {
                $('div').animate({
                    heigth:'150px',
                    width:'150px',
                    opacity:'0.5',
                    left:'300px'},4000);
            });
        });
    </script>
</head>
<body>

    <button type="button">开始动画</button>
    <br><br>
    <div style="background: #98bf21;height: 100px;width: 100px;position: absolute;">
    </div>
</body>
</html>

(3)队列功能
jQuery提供针对动画的队列功能
实例如下

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src = 'jquery.js'></script>
    <script>
        $(document).ready(function () {
            $('button').click(function () {
                var div = $('div');
                div.animate({height:'300px',opacity:'0.4'},'slow')
                div.animate({width:'300px',opacity:'0.8'},'slow')
                div.animate({height:'100px',opacity:'0.4'},'slow')
                div.animate({width:'100px',opacity:'0.8'},'slow')
            });
        });
    </script>
</head>
<body>

    <button type="button">开始动画</button>
    <br><br>
    <div style="background: #98bf21;height: 100px;width: 100px;position: absolute;">
    </div>
</body>
</html>
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值