jQuery-02

目录

一、jQuery中的hover事件

二、jQuery中的动画

1、内置动画

1)show()和hide()   |  由左上角缩放出现消失

2)slideDown()和slideUp()  |  上下滑动出现消失

3)fadeIn()/fadeTo()和fadeOut()   |   淡显淡隐

2、自定义动画

3、动画队列现象

三、jQuery中获取标签属性

第一:jQuery.prop()

第二: jQuery.attr()

四、获取标签内容

1、获取非表单元素内容

2、获取表单内容


一、jQuery中的hover事件

:hover 是css中的一个选择器。jQuery中的hover是一个事件。
1.hover事件是jQuery中的一个特殊事件
2.hover事件是完全可以取代鼠标进入事件和鼠标离开事件。

hover事件的用法:

jQuery对象.hover(function(){},function(){})

总结:
 a)第一个回调函数表示鼠标进入事件
 b)第二个回调函数表示鼠标离开事件


例:需求:进入box变成蓝色,离开变成粉色

<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>jQuery中hover事件</title>
    <style>
        div {
            width: 300px;
            height: 300px;
            background-color: #f00;
        }
    </style>
</head>

<body>
    <div class="box"></div>
    <script src="../jquery-3.6.0/jquery-3.6.0.min.js"></script>
    <script>
        // 有hover,能够简洁和方便
        $('div').hover(function() {
            $(this).css('background', 'blue')
        }, function() {
            $(this).css('background', 'pink')
        })
    </script>
</body>

</html>

 

二、jQuery中的动画

1、内置动画

1)show()和hide()   |  由左上角缩放出现消失

show()与hide语法一样

语法 : jQuery对象.show()
作用 : 用来显示元素
用法:

jQuqry.show(speed,easing,fn)

参数含义:
a ) speed表示速度,可以替换为"slow","normal","fast" 或者 设置毫秒数 1000  1250 之类
b ) easing表示切换效果,可以替换为默认"swing","linear"
c ) fn表示回调函数,当动画完成后执行该函数


例:点击按钮显示盒子,点击按钮使盒子消失

<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>jQuery案例</title>
    <style>
        div {
            width: 300px;
            height: 300px;
            display: none;
            background-color: #f00;
        }
    </style>
</head>

<body>
    <button class="show">show按钮</button>
    <button class="hide"> hide 按钮 </button>
    <div class="box1"></div>
    <script src="../jquery-3.6.0/jquery-3.6.0.min.js"></script>
    <script>
        // 代码演示:
        // 点击按钮, 显示div
        $('.show').click(function() {
            $('.box1').show(3000, 'linear', function() {
                console.log('show执行完了')
            })
        });
        // 点击按钮,隐藏div
        $('.hide').click(function() {
            $('.box1').hide(3000, 'linear', function() {
                console.log('hide执行完了')
            })
        });


    </script>
</body>

</html>

 

 


 

2)slideDown()和slideUp()  |  上下滑动出现消失

参数及用法与1)相同

slideDown()表示显示元素
slideUp()表示隐藏元素


例:

<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>jQuery案例</title>
    <style>
        div {
            width: 300px;
            height: 300px;
            display: none;
            background-color: #f00;
        }
    </style>
</head>

<body>
    <button class="slideDown">slideDown按钮</button>
    <button class="slideUp"> slideUp 按钮 </button>
    <div class="box2"></div>
    <script src="../jquery-3.6.0/jquery-3.6.0.min.js"></script>
    <script>
        // 代码演示:
        $('.slideDown').click(function() {
            $('.box2').slideDown(3000, 'linear', function() {
                console.log('slideDown执行完了')
            })
        });
        // hide()
        $('.slideUp').click(function() {
            $('.box2').slideUp(3000, 'linear', function() {
                console.log('slideUp执行完了')
            })
        });
    </script>
</body>

</html>


3)fadeIn()/fadeTo()和fadeOut()   |   淡显淡隐

fadeIn()和fadeOut():

作用:用来显示元素/隐藏元素
用法:
jQuqry对象.fadeIn(speed,easing,fn)
参数含义:
a)speed表示速度,可以替换为"slow","normal","fast" 或者 设置毫秒数 1000  1250 之类
b)easing表示切换效果,可以替换为默认"swing","linear"
c)fn表示回调函数,当动画完成后执行该函数

###特殊:

fadeTo(speed,opacity,easing,fn)

作用:用来显示元素,显示到某一透明度停止显示
用法:
jQuqry对象.fadeTo(speed,opacity,easing,fn)
参数含义:
a)speed表示速度,可以替换为"slow","normal","fast" 或者 设置毫秒数 1000  1250 之类
b)opacity表示透明度(0,1)取值范围
c)easing表示切换效果,可以替换为默认"swing","linear"
d)fn表示回调函数,当动画完成后执行该函数


<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>jQuery案例</title>
    <style>
        div {
            width: 300px;
            height: 300px;
            display: none;
            background-color: #f00;
        }
    </style>
</head>

<body>
    <button class="fadeIn">fadeIn 按钮</button>
    <button class="fadeOut"> fadeOut 按钮 </button>
    <div class="box3"></div>
    <hr>
    <button class="fadeTo">fadeTo 按钮</button>
    <!-- <button class="fadeOut"> fadeOut 按钮 </button> -->
    <div class="box4"></div>
    <script src="../jquery-3.6.0/jquery-3.6.0.min.js"></script>
    <script>

        // 淡显淡没        // 淡显淡没        // 淡显淡没        // 淡显淡没
        // 淡显淡没        // 淡显淡没        // 淡显淡没        // 淡显淡没

        $('.fadeIn').click(function() {
            $('.box3').fadeIn(3000, 'linear', function() {
                console.log('fadeIn 执行完了')
            })
        });
        // hide()
        $('.fadeOut').click(function() {
            $('.box3').fadeOut(3000, 'linear', function() {
                console.log('fadeOut 执行完了')
            })
        });


        //可以设置元素半透明的动画效果 //可以设置元素半透明的动画效果
        //可以设置元素半透明的动画效果 //可以设置元素半透明的动画效果

        $('.fadeTo').click(function() {
            $('.box4').fadeTo(3000, 0.2, 'linear', function() {
                console.log('fadeTo 执行完了')
            })
        });
    </script>
</body>

</html>

2、自定义动画

jQuery中自定义动画:
1.内置动画:局限性,都是与元素隐藏和显示相关的
2.网页中的动画(位置移动的动画,颜色改变的动画),内置动画无法满足

语法:

jQuery对象.animate({},speed,linear,fn) 

 后边的三个参数可以不写空着

a)第一个参数就是对象(键值对)
b)后面三个参数用法与show()内置动画一样

自定义动画的特点:
1.jQuery中自定义动画不支持与颜色相关的属性(背景颜色,文字颜色,边框颜色)
2.Query中自定义动画不支持transform相关的属性(位移,旋转,缩放)


<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>jQuery自定义动画</title>
    <style>
        div {
            width: 300px;
            height: 300px;
            background-color: #f00;
        }
    </style>
</head>

<body>
    <div></div>
    <script src="../jquery-3.6.0/jquery-3.6.0.min.js"></script>
    <script src="./jQuery.color.js"></script>
    <script>
        //代码演示自定义动画
        $('div').hover(function() {
            $(this).stop().animate({
                'width': '500px'
            }); //后边的三个参数可以不写空着 
        }, function() {
            $(this).stop().animate({
                'width': '300px'
            }); //后边的三个参数可以不写空着 
        })
    </script>
</body>

</html>

3、动画队列现象

动画队列:当元素执行多个动画的时候,动画会排队等待执行,动画队列是jQuery中动画的一个特点
记住:以后在使用jQuery动画的时候都要停止动画队列

操作方法:
停止动画就是再动画的前边加.stop()      // $('div').stop().show()
利用了排他思想:先停止其他所有动画,再单独加个动画

<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>jQuery案例</title>
    <style>
        div {
            width: 300px;
            height: 300px;
            background-color: #f00;
        }
    </style>
</head>

<body>
    <div></div>
    <script src="../jquery-3.6.0/jquery-3.6.0.min.js"></script>
    <script>
        //代码演示
        //当鼠标进入元素的时候,让元素隐藏,鼠标离开的时候让元素显示
        $('div').hover(function() {
            $(this).stop().hide(3000, 'linear', function() {
                console.log('hide执行完了')
            })
        }, function() {
            $(this).stop().show(3000, 'linear', function() {
                console.log('show执行完了')
            })
        });
    </script>
</body>

</html>

三、jQuery中获取标签属性

获取标签身上的属性 
标准属性:标签本身自带的属性
自定义属性:程序员自己给标签定义的属性

jQuery中获取标签的属性就俩固定方法
推荐: 标准用prop()   自定义用attr()
第一: jQuery对象.prop()   用来获取或者设置标签身上标准属性
第二: jQuery对象.attr()   用来获取或者设置自定义属性 既可以操作自定义属性又能操作标准属性

第一:jQuery.prop()

用来获取或者设置标签身上标准属性,并且,只能获取标准属性

 获取:

        console.log($('div').prop('class'))

修改:

        $('div').prop('class', 'newbox')

第二: jQuery.attr()

用来获取或者设置自定义属性,既可以操作自定义属性又能操作标准属性

 获取:

        console.log($('div').attr('class'))

修改:

        $('div').attr('data-mag', 'hehe')

四、获取标签内容

1、获取非表单元素内容

语法 : jQuery对象.text()    或者   jQuery对象.html()
和innerText()和innerHtml()的区别一样

获取:

        console.log($('.box1').text());

        console.log($('.box2').html());

设置:

        $('.box1').text('<p>这是box1中新加的一句话</p>')

        $('.box2').html('<p>这是box2新加的一句话</p>')

2、获取表单内容

语法: jQuery对象.val()

获取:

        console.log($('input').val());

更改:

        $('input').val('新写的内容')

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

李建雨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值