伪类:after的使用以及结合attr来添加属性的技巧

本案例以实现侧边栏的效果为例来说明

直接上代码看效果:

css

复制代码
<style type="text/css">
        *{margin:0;padding:0;list-style: none;}
        
        ul{position: fixed; right:0;top:50%;  transform: translate(0,-50%); }
        li{width:20px; height:20px; background:#666 url(img/i1.png) no-repeat center center;padding:5px; margin: 10px 0; border-radius:4px 0 0 4px ;cursor: pointer;position: relative;  }
        li:nth-of-type(2){ background-image: url(img/i2.png); }
        li:nth-of-type(3){ background-image: url(img/i3.png); }
        
        li:after{ content:attr(name); width:80px; line-height:30px; background-color:#900; position: absolute;top:0; right:-90px;color:#fff;padding-left: 10px; 
            transition:all .5s; 
            z-index: -1;  }

        li:hover:after{ right:30px; }
        li:hover{ border-radius: 0; background-color: #900; }
    </style>
复制代码

html

利用name像id,class那样是元素都可以添加的特征,然后利用attr在css里来添加属性,再让其在:after里面进行添加内容为文字

这种运用方式很巧妙

<ul>
        <li name="购物车"></li>
        <li name="我的关注"></li>
        <li name="我的脚印"></li>
    </ul>

 

posted @   xuanPhoto  阅读(474)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏
· Manus爆火,是硬核还是营销?
点击右上角即可分享
微信分享提示