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

解决CSS中鼠标移入到某个元素其子元素被遮挡的问题

我们在开发中经常遇到一种场景,就是给元素加提示信息,就是鼠标移入到盒子上面时,会出现提示信息这一功能,如果我们给盒子加了hover,当鼠标移入到盒子上时,让他往上移动5px,即transform: translateY(-5px), 同时还让提示信息展示出来,此时受到transform的影响,提示信息会被父盒子遮挡住。代码如下:

<template><div class="container"><divclass="item"v-for="item in 30"@mouseenter="showTooltip"@mouseleave="hideTooltip"><div class="tooltip"></div><span>{{ item }}</span></div></div>
</template><style>
.container {display: flex;flex-wrap: wrap;width: 1145px;margin: 0 auto;padding: 10px;
}
.item {position: relative;display: flex;justify-content: center;align-items: center;width: 120px;height: 120px;background-color: #eceff7;border-radius: 10px;margin: 10px;box-shadow: 0 3px 5px 2px rgba(0, 0, 0, 0.1);cursor: pointer;transition: all 0.5s ease;;
}.item .tooltip {display: none;width: 100px;height: 30px;background-color: #000;border-radius: 6px;position: absolute;bottom: -50px;z-index: 10;
}
.item .tooltip::after {position: absolute;left: 0;top: -25px;width: 0;height: 0;left: 50%;transform: translateX(-50%);border-left: 12px solid transparent;border-right: 12px solid transparent;border-top: 15px solid transparent;border-bottom: 15px solid #000;content: "";z-index: 20px;
}
.item:hover {transform: translateY(-5px);
}
.item:hover .tooltip {display: block;
}
</style>

在这里插入图片描述
我们可以看到,我们给tooltip设置了z-index值为10,但是黑色提示信息依然被挡住了,原因时当hover时,执行了transform, 会改变元素的层级,
此时我们只需要给 .item:hover 加上一句 z-index: 1, 保证hover的时候,层级比item更低,就能解决这个问题。

<template><div class="container"><divclass="item"v-for="item in 30"@mouseenter="showTooltip"@mouseleave="hideTooltip"><div class="tooltip"></div><span>{{ item }}</span></div></div>
</template>
<style>
.container {display: flex;flex-wrap: wrap;width: 1145px;margin: 0 auto;padding: 10px;
}
.item {position: relative;display: flex;justify-content: center;align-items: center;width: 120px;height: 120px;background-color: #eceff7;border-radius: 10px;margin: 10px;box-shadow: 0 3px 5px 2px rgba(0, 0, 0, 0.1);cursor: pointer;transition: all 0.5s ease;;
}.item .tooltip {display: none;width: 100px;height: 30px;background-color: #000;border-radius: 6px;position: absolute;bottom: -50px;z-index: 10;
}
.item .tooltip::after {position: absolute;left: 0;top: -25px;width: 0;height: 0;left: 50%;transform: translateX(-50%);border-left: 12px solid transparent;border-right: 12px solid transparent;border-top: 15px solid transparent;border-bottom: 15px solid #000;content: "";z-index: 20px;
}
.item:hover {z-index: 1; /* 保证hover的时候z-index层级更低 */transform: translateY(-5px);
}
.item:hover .tooltip {display: block;
}
</style>

此时我们再看效果:
在这里插入图片描述

http://www.lryc.cn/news/339635.html

相关文章:

  • 【华为OD机试】虚拟理财游戏【C卷|100分】
  • ssh 使用
  • Springboot+Vue项目-基于Java+MySQL的母婴商城系统(附源码+演示视频+LW)
  • Android多线程:Handler runOnUiThread 异步消息处理机制
  • AndroidStudio 导出aar包,并使用
  • python与设计模式之工厂模式的那些事儿
  • 什么是区块链?
  • 2022年电赛F题23年电赛D题-信号调制度测量装置说明中提到带通采样定律。
  • Rust面试宝典第2题:逆序输出整数
  • Linux笔记之查看docker容器目录映射
  • ​​​​网络编程探索系列之——广播原理剖析
  • jar包解压和重新打包
  • Python基于Django的微博热搜、微博舆论可视化系统
  • Flink SQL:debezium-json 格式的表一定是数据库的 CDC 数据吗?
  • 基于STM32的RFID智能门锁系统
  • 测试用例的编写评审
  • 二叉树的前、中、后序遍历【c++】
  • Hadoop HDFS:海量数据的存储解决方案
  • Leetcode二十三题:合并K个升序链表【22/1000 python】
  • 03-echarts如何画立体柱状图
  • 2024蓝桥A组E题
  • Java单例模式
  • 04—常用方法和正则表达式
  • Python异常处理机制详解及示例
  • 解决:Java后端返回给前端的Date格式数据相差8小时的问题
  • linux安装weblogic
  • Unity WebGL Release-Notes
  • Excel 记录单 快速录入数据
  • go 利用channel实现定时任务
  • JWT介绍