CSS100Day(12)-hover出Tooltip技巧

682 字
3 分钟
CSS100Day(12)-hover出Tooltip技巧

设计简介#

本次练习做个文字的hover显示Tooltip,解决老大难问题鼠标在移入缝隙时候消失动画触发:

  1. 关键点1:为了有过渡使用visibility和opacity属性来进行设置隐藏

  2. 关键点2:在tooltip元素中使用::before伪元素,设置宽度100%,然后bottom为负数刚好将缝隙覆盖,但不显示,这样就不会出现鼠标移入缝隙消失问题

效果展示#

I know quite certainly that I myself have no special talent; curiosity, obsession [əbˈseʃ(ə)n] Obsession, a persistent disturbing preoccupation with an often unreasonable idea or feeling. and dogged endurance, combined with self-criticism have brought me to my ideas.

Albert Einstein

实现代码和步骤#

HTML部分#

HTML部分如下

<div class="frame">
<div class="quote">
<p>
I know quite certainly that I myself have no special talent; curiosity,
<span onclick="void(0);" class="tooltip"
>obsession
<span class="info">
<span class="pronounce">[əbˈseʃ(ə)n]</span>
<span class="text"
><b>Obsession</b>, a persistent disturbing preoccupation with an often unreasonable idea or
feeling.
</span>
</span>
</span>
and dogged endurance, combined with self-criticism have brought me to my ideas.
</p>
<p class="author">Albert Einstein</p>
</div>
</div>

CSS部分#

注意2个关键点即可

.frame {
position: absolute;
width: 400px;
height: 400px;
border-radius: 2px;
box-shadow: 1px 2px 10px 0px rgba(0, 0, 0, 0.3);
background: #4cb6de;
color: #fff;
}
.quote {
position: relative;
margin-top: 90px;
padding: 0 30px;
&::before {
display: block;
content: "„";
position: absolute;
top: 0;
left: 0;
font-size: 250px;
color: #6ac2e3;
font-family: Arial;
line-height: 35px;
top: -100px;
left: 7px;
z-index: 0;
}
p {
position: relative;
font-size: 24px;
margin: 20px 0;
z-index: 1;
}
.author {
font-weight: 300;
font-style: italic;
line-height: 28px;
font-size: 20px;
}
}
.tooltip {
position: relative;
display: inline-block;
background: #286f8a;
padding: 0px 8px 5px 8px;
cursor: pointer;
margin: -10px 0;
.info {
visibility: hidden;
position: absolute;
background: #286f8a;
box-sizing: border-box;
bottom: 53px;
left: -85px;
font-size: medium;
cursor: auto;
width: 300px;
opacity: 0;
line-height: 24px;
transform: translateY(-20px);
transition: all 0.5s ease-out;
&::before {
position: absolute;
content: "";
width: 100%;
height: 15px;
bottom: -15px;
}
&::after {
position: absolute;
display: block;
content: "";
width: 10px;
height: 10px;
background: #286f8a;
bottom: -5px;
rotate: 45deg;
left: 50%;
margin-left: -5px;
}
.pronounce {
display: block;
background-color: #fff;
color: #286f8a;
padding: 8px 15px;
}
.text {
display: block;
padding: 15px 15px;
}
}
&:hover .info,
&:focus .info {
display: block;
visibility: visible;
opacity: 1;
transform: translateY(0);
}
}
分享给其他人
如果该文章对您有帮助,欢迎分享给其他人阅读!
复制链接

评论区

图片
犟哟
Hello, I'm Juyial.
公告
欢迎来到我的博客,在这里可以一起进步,一起学习!2026年起航!
分类
站点统计
文章
19
分类
1
标签
3
总字数
35,134
运行时长
0
最后活动
0 天前

目录