当前位置: 首页 > news >正文

css实现鼠标悬停时元素的显示与隐藏

css实现鼠标悬停时元素的显示与隐藏

跟着B站黑马学习小兔鲜项目,有个点记录一下

就是当鼠标悬浮在商品列表上时,列表中的商品会显示出来,离开时,商品隐藏,如下:

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

image-20231113154443947

image-20231113154515365

感觉这个功能经常会遇到,但一直没弄明白具体怎么实现的,现在仔细学习了一下,总算搞清楚了

先上代码

<template><div class="home-category"><ul class="menu"><li v-for="item in categoryStore.categoryList" :key="item.id"><RouterLink to="/">{{ item.name }}</RouterLink><RouterLink to="/" v-for="i in item.children.slice(0, 2)" :key="i.id">{{ i.name }}</RouterLink><div class="layer"><h4>分类推荐<small>根据您的购买或浏览记录推荐</small></h4><ul><li v-for="i in item.goods" :key="i.id"><RouterLink to="/"><img :src="i.picture" alt=""><div class="info"><p class="name ellipsis-2">{{ i.name }}</p><p class="desc ellipsis">{{ i.desc }}</p><p class="price"><i>¥</i>{{ i.price }}</p></div></RouterLink></li></ul></div></li></ul></div>
</template><script setup>
import { useCategoryStore } from '@/stores/category';
// import { onMounted } from 'vue'const categoryStore = useCategoryStore()
// onMounted(() => console.log(categoryStore.categoryList))
</script><style lang="scss" scoped>
.home-category {width: 250px;height: 500px;background: rgba(0, 0, 0, 0.8);position: relative;z-index: 99;.menu {li {padding-left: 40px;height: 55px;line-height: 55px;&:hover {background: $xtxColor;}a {margin-right: 4px;color: #fff;&:first-child {font-size: 16px;}}.layer {width: 990px;height: 500px;background: rgba(255, 255, 255, 0.8);position: absolute;left: 250px;top: 0;display: none;padding: 0 15px;h4 {font-size: 20px;font-weight: normal;line-height: 80px;small {margin-left: 10px;font-size: 16px;color: #666;}}ul {display: flex;flex-wrap: wrap;li {width: 310px;height: 120px;margin-right: 15px;margin-bottom: 15px;border: 1px solid #eee;border-radius: 4px;background: #fff;&:nth-child(3n) {margin-right: 0;}a {display: flex;width: 100%;height: 100%;align-items: center;padding: 10px;&:hover {background: #e3f9f4;}img {width: 95px;height: 95px;}.info {padding-left: 10px;line-height: 24px;overflow: hidden;.name {font-size: 16px;color: #666;}.desc {color: #999;}.price {font-size: 22px;color: $priceColor;i {font-size: 16px;}}}}}}}// 关键样式  hover状态下的layer盒子变成block&:hover {.layer {display: block;}}}}
}
</style>

上面是完整代码,关键在于layer的样式

首先看正常情况下,鼠标未悬浮时layer的样式

.layer {width: 990px;height: 500px;background: rgba(255, 255, 255, 0.8);position: absolute;left: 250px;top: 0;display: none;padding: 0 15px;
}

display:none;实际上就是隐藏元素

再看看悬浮时layer的样式:

            // 关键样式  hover状态下的layer盒子变成block&:hover {.layer {display: block;}}

注意,悬浮是悬浮在layer的父元素也就是menu上,悬浮时,设置display:block;即可展示layer

总结一下:

  • display: none;隐藏元素
  • display:block;显示元素,悬浮时设置
http://www.lryc.cn/news/230413.html

相关文章:

  • 天气越来越寒冷,一定要注意保暖
  • 03 # 类型基础:动态类型与静态类型
  • Python编程——模块、包和__init__.py
  • 220kV110kV10kV变电站初步设计
  • Git企业开发级讲解(一)
  • 【微信支付通知】对resource解密 AEAD_AES_256_GCM算法工具类
  • JVM虚拟机:垃圾回收之三色标记
  • 唯坚持而已
  • 【大语言模型】Docker部署清华大学ChatGLM3教程
  • 详解 KEIL C51 软件的使用·设置工程·编绎与连接程序
  • 小程序实现语音识别功能
  • 判断两层对象中是否有空的value值
  • 【SQLite】环境安装
  • 【QT】飞机大战
  • linux最全基础入门命令(简单明了建议收藏)
  • dgl 的cuda 版本 环境配置(dgl cuda 版本库无法使用问题解决)
  • 回文数和复利的威力(C#)
  • 【Java】面向对象程序设计 课程笔记 Java核心类
  • 16个值得推荐的.NET ORM框架
  • Git 进阶使用
  • 【微软技术栈】C#.NET 泛型数学
  • 【nlp】1.1文本处理的基本方法
  • 流量分析(信息安全铁人三项赛分区赛2-5.18)
  • 云服务器如何选?腾讯云2核2G3M云服务器88元一年!
  • 【Hello Go】初识Go语言
  • 计算机视觉:人脸识别与检测
  • 【NLP】理解 Llama2:KV 缓存、分组查询注意力、旋转嵌入等
  • ctyunos 与 openeuler
  • 跟着GPT学设计模式之工厂模式
  • VScode+python开发,多个解释器切换问题