使用CSS content的attr实现鼠标悬浮提示(tooltip)效果

发布时间:2022-04-13 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了使用CSS content的attr实现鼠标悬浮提示(tooltip)效果脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

为什么实现这种效果呢,其实这效果也是根据tITle的提示说明衍生出来的,只是因为原生的比较丑陋,像这种衍生出的插件后很多种,如jquery-ui的tooltip,Bootstrap的tooltips等等,有很多种插件库。

有时候我们不需要那么大的插件库,其实就一两个地方需要做一些提示(tooltip),所以可以使用CSS的content属性与 :before 及 :after 伪元素配合使用来实现插入生成内容。

查看效果如下

htML代码如下

<a class="dui-tips" data-tooltip="我是一个3cbest.COM提示">w3cbest.com</a>

“data-“为自定义属性,如上自定义提示data-tooltip=”我是一个3cbest.com提示”,配合before、after使用content的attr调用自定义提示,content: attr(data-tooltip);

content: attr很好理解,只要会jq的.attr()知道什么意思了,本例的content: attr就是获取data-tooltip里面的值

CSS代码

.dui-tips {
position: relative;
display: inline-block;
cursor: pointer;
}
 
.dui-tips[data-tooltip]:after,
.dui-tips[data-tooltip]:before {
visibility: hidden;
position: absolute;
top: 50%;
left: 100%;
transition: all .3s;
}
 
.dui-tips[data-tooltip]:after {
 
content: attr(data-tooltip);
transform: translate(-5px, -50%);
white-space: PRe;
padding: 5px 10px;
background-color: rgba(0, 0, 0, 0);
color: rgba(255, 255, 255, 0);
}
 
.dui-tips[data-tooltip]:before {
content: '';
height: 0;
width: 0;
transform: translate(-10px, -50%);
border-width: 5px 5px 5px 0;
border-style: solid;
border-color: transparent rgba(0, 0, 0, 0) transparent transparent;
}
.dui-tips:hover:after,.dui-tips:hover:before {
transition: all .3s;
visibility: visible;
 
}
.dui-tips:hover:after {
color: rgba(255, 255, 255, 1);
background-color: rgba(0, 0, 0, 0.8);
transform: translate(5px, -50%);
}
 
.dui-tips:hover:before {
border-color: transparent rgba(0, 0, 0, 0.8) transparent transparent;
transform: translate(0px, -50%);
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本宝典。

脚本宝典总结

以上是脚本宝典为你收集整理的使用CSS content的attr实现鼠标悬浮提示(tooltip)效果全部内容,希望文章能够帮你解决使用CSS content的attr实现鼠标悬浮提示(tooltip)效果所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。