当前位置: 首页 > Web前端 > HTML5

CSS技巧:hover+active

时间:2023-04-05 22:50:01 HTML5

.flex__itemwidth15%height15%box-shadow004pxrgba(0,0,0,.5),inset2px01pxrgba(0,0,0,.2)displayflexalign-itemscenterjustify-contentcentercolor#142334//钢蓝色&:nth-child(1)width20%height20%&:nth-child(2)width16%height18%&:nth-child(3)width14%height28%&::beforepositionabsolutecontent'一个人gettheway'padding10px6pxborder-radius6pxcolor#e0c8d1//lightpurplebackground-color#1661ab//indigo&::afterpositionabsolutecontent'laststand'padding10px6pxborder-radius6pxcolor#e0c8d1//lightcyanbackground-color#1661ab//indigo简介在JavaScript中,我们可以使用变量和DOM来保存状态,在CSS中,我们也可以使用伪类选择器来保存状态,这是CSS的核心。.核心概念:保存状态。在之前的CSS技巧:checkbox+label+selector中,通过checkbox+label+selector加深了对Flex的弹性容器属性的理解。本节是通过:hover+:active来了解Flex的弹性item属性。你熟悉这两个属性吗?其实我们经常在a标签中使用它们LVHAorder::link--:visited--:hover--:active陷入自闭症,缺乏创作激情,所以跳过这个链接...技能说明当hover被触发时,隐藏显示子元素;当active被触发时,子元素根据需求变化。代码解读1.基本布局因为显示需要垂直居中显示,所以我使用Vueprops固化垂直居中样式:helloflexitem为了演示更简单,请三位身高矮胖瘦不一致的师兄:大哥

二哥
三妹
为其添加样式,添加伪元素:.flex__itemwidth15%height15%box-shadow004pxrgba(0,0,0,.5),inset2px01pxrgba(0,0,0,.2)displayflexalign-itemscenterjustify-contentcentercolor#142334//钢蓝色&:nth-child(1)width20%height20%&:nth-child(2)width16%height18%&:nth-child(3)width14%height28%&::beforepositionabsolutecontent'一个人gettheway'padding10px6pxborder-radius6pxcolor#e0c8d1//lightpurplebackground-color#1661ab//indigo&::afterpositionabsolutecontent'laststand'padding10px6pxborder-radius6pxcolor#e0c8d1//lightcyanbackground-color#1661ab//indigo查看效果:2.:hover基本布局完成,然后添加:hover效果。鼠标悬停时会显示两个伪元素,一上一下:.flex__item&::beforeopacity0&::afteropacity0.flex__item:hover::beforetransformtranslateY(-80px)opacity1。flex__item:hover::aftertransformtranslateY(80px)opacity1查看效果:3.:active在我的记忆中,:active对任何元素都是有效的,但是在伪元素上设置失败了,然后去MDN:也许伪元素和元素本身算作一个?或者你想选择父元素(提示::focus-within)?留到以后再说,FLag+1。还有一种方法可以选择(伪装),祭祀大哥:.flex__item&:nth-child(1)width20%height20%&::afterpositionabsolutecontent'LastStand'padding10px6pxborder-radius6pxcolor#e0c8d1//lightcyanbackground-color#1661ab//indigotransitionall0.5s线性不透明度0&:nth-child(2)width16%height18%&::beforepositionabsolutecontent'onepersongetstheway'padding10px6pxborder-radius6pxcolor#e0c8d1//Lightpurplebackground-color#1661ab//Indigotransitionall0.5slinearopacity0&:nth-child(3)width14%height28%&::beforeposition绝对内容'一人得道'padding10px6pxborder-radius6pxcolor#e0c8d1//浅紫色background-color#1661ab//Indigotransitionall0.5slinearopacity0查看效果:在伪类中添加:activeEffect:。flex__item:active::before,.flex__item:active::aftercolor#f1908c//榴莲红背景颜色#fffbox-shadow02px4pxrgba(0,0,0,.5),2px04pxrgba(0,0,0,.6)查看效果:4.align-self给子元素添加align-self不同的值:.flex__item&:nth-child(1)&:activealign-selfflex-end&:nth-child(2)&:activealign-selfflex-start&:nth-child(3)&:activealign-selfflex-startFinal结果如介绍所示,到最后我们可能很难理解CSS的很多属性的作用。我个人认为用CSS来解释CSS是一个很好的方式。参考资料MDN中国色彩