CSS鼠标动作的五种状态[伪类选择器]

CSS鼠标动作的五种状态[伪类选择器]

注意事项

设置的样式失效的情况 直接跳过就行了 别纠结

如果发现鼠标互动没反应 你要检查一下

css定义超链接是要有先后顺序的。否则,可能会出现某个或某几个样式不起作用的bug。

1.a:link 未访问过的 样式
2.a:visited 访问过的 样式
3.?:hover 鼠标处于鼠标悬停状态的 样式
4.?:foucs 鼠标点击聚焦
5.?:active 当鼠标左键按下时,被激活的 样式

在对一个标签同时定义多个伪类选择器的时候 一定按照上面的顺序

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <style>
        body {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
        }

        * {
            margin: 0px;
            padding: 0px;
        }

        #a_id:link {
            color: chartreuse;
        }

        #a_id:visited {
            color: #ffa23b;
        }
        #a_id:hover{
            color: #fde0ff;
        }
        #a_id:focus{
            color: #8cb2ff;
        }
        #a_id:active{
            color: #ff66d8;
        }

    </style>
</head>

<body>

<a href="#" id="a_id">来鼠标</a>


</body>
</html>

未访问过link

语法 a:link{}

什么都没干 未点击和悬浮时候的 状态

        body,a{
            position:absolute;
            top: 50%;
            left: 50%;
            transform:translate(-50%,-50%);
        }
        *{
            margin: 0px;
            padding: 0px;
        }
        .a_like:link{
            color: salmon;
            /*去掉下划线*/
            text-decoration: none
        }

访问过visited

语法a:visited{ }

visited只适用于带href的a标签。点击后变化 只准写颜色相关的样式 其他的都不能写 如果你想玩花样 那么 你就去使用js click 事件

而且a标签中必须有 href=“” 否则失效

     .a_like:visited{
            color: salmon;
      }

<div style="border: 1px silver solid;width: 100px;height: 100px">
<a href="#" class="a_like">哈哈</a>
</div>

未点击状态

点击后状态

鼠标悬浮hover

语法: a:hover{ } 没有限制

鼠标悬浮时的状态 离开后恢复原来的状态 能加一些动态效果

       .a_like:hover{
            color: #e8dacd;
        }

<div style="border: 1px silver solid;width: 100px;height: 100px">
<a href="#" class="a_like">哈哈</a>
</div>

动态效果

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <style>

        body{ 
            position:absolute;
            top: 50%;
            left: 50%;
            transform:translate(-50%,-50%);
        }
        *{
            margin: 0px;
            padding: 0px;
        }
        #div_id{
            width: 100px;
            height: 100px;
            background-color: darkcyan;
            border: 1px sandybrown solid;
            box-shadow: 1px 1px 5px silver;
            border-radius: 10px;
        }


        #a_id:hover #div_id:nth-of-type(1){
            width: 150px;
            height: 150px;
            background-color: #b85142;
        }
    </style>
</head>

<body>

<a href="#" id="a_id"><div id="div_id"> </div></a>

</body>
</html>

在这里插入图片描述

点击后未释放状态active

当鼠标点击时候状态 释放鼠标后恢复原来的状态 能加一些动态效果

语法 : a:active{ } 没有限制

#a_active:active{
    color: #e820dc;
}

<div style="border: 1px silver solid;width: 100px;height: 100px">
<a href="#" id="a_active">a_active</a>
</div>

在这里插入图片描述

动态效果(附带动画)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <style>
        body{
            position:absolute;
            top: 50%;
            left: 50%;
            transform:translate(-50%,-50%);
        }
        *{
            margin: 0px;
            padding: 0px;
        }
        #div_id{
            width: 100px;
            height: 100px;
            background-color: darkcyan;
            border: 1px sandybrown solid;
            box-shadow: 1px 1px 5px silver;
            border-radius: 10px;
        }

        #a_id:active #div_id:nth-of-type(1){
            background-color: #b85142;
            animation:active 1s ease-in-out 1 forwards;
        }
        @keyframes active {
            0%{

            }
            100%{
                width: 200px;
                height: 200px;
            }
        }
    </style>
</head>

<body>

<a href="#" id="a_id"><div id="div_id"> </div></a>

</body>
</html>

在这里插入图片描述

鼠标聚焦focus

语法 : a:focus{ }

必须使用<a href=“#”> </a> 或者 <input <textarea <select 等等 带聚焦的 才有效

鼠标点击(聚焦)变化状态 当失去焦点(点击其他地方)恢复状态

#a_focus:focus{
    color: #e820dc;
}

<div style="border: 1px silver solid;width: 100px;height: 100px">
<a href="#" id="a_focus">a_focus</a>
</div>

在这里插入图片描述

动态效果

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <style>
        body{
            position:absolute;
            top: 50%;
            left: 50%;
            transform:translate(-50%,-50%);
        }
        *{
            margin: 0px;
            padding: 0px;
        }
        #div_id{
            width: 100px;
            height: 100px;
            background-color: darkcyan;
            border: 1px sandybrown solid;
            box-shadow: 1px 1px 5px silver;
            border-radius: 10px;
        }
        
        #a_id:focus #div_id{
            background-color: #b85142;
            animation:active 1s ease-in-out 1 forwards;
        }
        @keyframes active {
            0%{

            }
            100%{
                width: 200px;
                height: 200px;
                transform: rotate(360deg);
            }
        }
    </style>
</head>

<body>

<a href="#" id="a_id"><div id="div_id"> </div></a>



</body>
</html>

在这里插入图片描述

案例实现自定义下内容

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <style>

        body{
            position:absolute;
            top: 50%;
            left: 50%;
            transform:translate(-50%,-50%);
        }
        *{
            margin: 0px;
            padding: 0px;
        }
        .a_num:link{
            text-decoration: none;
        }
        .a_num:hover{
            color: #be423b;
        }
        .a_num:focus+.text_mus{
             display: block;
        }
        .text_mus{
            color: #8c858c;
            font-size: 13px;
            display: none;
        }
    </style>
</head>

<body>

<div style="width: 150px;height: 200px;border: 1px sandybrown solid">
    <a href="#" class="a_num">点击我展开下拉框</a>
   <div class="text_mus">
       天上掉钱下来了!”7月6日晚,在东兴市东盟大道维也纳酒店,有人从15楼撒钱下来,均是面额百元的钞票,这是怎么回事?
   </div>
</div>

</body>
</html>

在这里插入图片描述

其他状态

锚连接:target

锚连接 url后面的锚点,指向 某个元素, 那么该元素就会触发 target

我们可以使用这个属性玩一个选项卡

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <style>
        body{
            position:absolute;
            top: 50%;
            left: 50%;
            transform:translate(-50%,-50%);
        }
        *{
            margin: 0px;
            padding: 0px;
        }
       div{
           width: 300px;
           height: 300px;
           border: 1px solid red;
       }
        a{
            display: block;
            width: 100%;
            background-color: #b2b895;
            color: antiquewhite;
            text-decoration: none;
            text-align: center;
        }
        span{
            display: none;
            background-color: seashell;
        }
        span:target{
            display: block;
        }

    </style>
</head>

<body>


<div>
    <h1>选项卡</h1>
    <a href="#span1">content1</a>
    <span id="span1">span1</span>
    <a href="#span2">content2</a>
    <span id="span2">span2</span>
    <a href="#span3">content3</a>
    <span id="span3">span4</span>

</div>

</body>
</html>

元素可用状态和不可以状态

:enabled 用来指定当元素处于可用状态时的样式(初始状态)

:disabled 用来指定元素处于不可用时的状态

需要配合js使用

       input:disabled{
            border: 2px sienna dashed;
            background-color: red;
        }
        input:enabled{
            border: 2px #a09496 solid;
            background-color: #f5f5ff;
        }



<input id="text" type="text"value="哈哈哈" >
<button  οnclick="but_d()">点击禁用</button>
<button  οnclick="but_h()">恢复可用状态</button>

<script src="jquery-3.4.1.js"></script>
<script>
    function but_d() {
        document.getElementById("text").disabled=true
    }

    function but_h() {
        document.getElementById("text").disabled=false
    }
</script>

可用状态

禁用状态

  1. :read-only 用来获取元素属于只读状态时

  2. :checked 用来获取单选框处于选取状态时的样式

            div{
                width: 100px;
                height: 100px;
                background-color: #f47277;
            }
            input:checked+div{
                background-color: silver;
            }
    
    <input type="radio">
    <div></div>
    

    未选中

    选中后
    在这里插入图片描述

点赞 -收藏-关注-便于以后复习和收到最新内容
有其他问题在评论区讨论-或者私信我-收到会在第一时间回复
如有侵权,请私信联系我
感谢,配合,希望我的努力对你有帮助^_^
  • 7
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

胡安民

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

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

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

打赏作者

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

抵扣说明:

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

余额充值