New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[css] 第324天 使用css实现悬浮提示文本 #2008
Labels
css
css
Comments
不知道有没有理解题意 <div class="wrap">
我是一个元素
<div class="tips">
这是悬浮提示文字
</div>
</div>
<style>
.wrap {
position: relative;
display: inline-block;
margin: 4em;
}
.tips {
position: absolute;
top: -2em;
left: 50%;
display: none;
white-space: nowrap;
transform: translate(-50%,0);
}
.wrap:hover .tips {
display: block;
}
</style> |
理解对了!还可以使用伪类,content: attr()获取自定义标签的内容等方法。 |
直接把下方代码粘贴至本页源码中即可预览: <div class="tips-demo" data-tips="提示文本">演示文本</div>
<style>
.tips-demo {
position: fixed;
bottom: 15px;
right: 15px;
}
.tips-demo:after {
content: attr(data-tips);
position: absolute;
top: 0;
left: 0;
right: 0;
margin: 0 auto;
white-space: nowrap;
opacity: 0;
transform: translateY(-150%);
transition: .1s;
}
.tips-demo:hover:after {
opacity: 1;
transform: translateY(-100%);
}
</style> |
.element{
position: relative;
margin-top: 100px;
}
.element:hover::before{
position: absolute;
content: attr(tips);
left:0;
top:-20px;
border: 1px solid#eee;
border-radius: 5px;
} |
html<div class="haveTips" tips='hello world'>Lorem ipsum dolor</div> cssdiv.haveTips {
background-color: #F7F5F1;
color: #BBA985;
font-family: sans-serif;
padding: 1em;
border-radius: 10px;
position: relative;
cursor: pointer;
}
div.haveTips::after {
content: attr(tips);
padding: 5px;
background-color: #B4A078;
box-shadow: 0 0 5px #B4A078;
color: white;
min-width: 5em;
max-width: 100%;
text-align: center;
border-radius: 5px;
position: absolute;
bottom: 100%;
left: 50%;
transform: translate(-50%, -10px);
opacity: 0;
transition: all 0.5s;
}
div.haveTips:hover::after {
opacity: 1;
}
div.haveTips::before {
content: '';
width: 0;
height: 0;
border: 6px solid transparent;
border-top-color: #B4A078;
position: absolute;
top: -10px;
left: calc(50% - 6px);
opacity: 0;
transition: all 0.5s;
}
div.haveTips:hover::before {
opacity: 1;
} 原理和对话气泡类似,提示框也由矩形和小三角组成。
在左右移动时,同时用了 因为它们的百分比计算基数不同:
两个结合起来,可以使提示框与父组件中心对齐。 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
第324天 使用css实现悬浮提示文本
我也要出题
The text was updated successfully, but these errors were encountered: