前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >CSS实现html指令式Tips文字提示

CSS实现html指令式Tips文字提示

作者头像
Javanx
发布2019-09-04 15:37:50
3.5K0
发布2019-09-04 15:37:50
举报
文章被收录于专栏:web秀web秀

前言

一个小小tips,在网页中能有意想不到的效果,能很好的引导客户,说明功能等等。

CSS实现html指令式Tips文字提示
CSS实现html指令式Tips文字提示

html代码

<div class="container">
    <div class="top">
        <button tooltip="上左">上左</button>
        <button tooltip="上边" placement="top">上边</button>
        <button tooltip="上右" placement="top-right">上右</button>
    </div>
    <div class="left">
        <button tooltip="左上" placement="left-top">左上</button>
        <button tooltip="左边" placement="left" effect="light">左边</button>
        <button tooltip="左右" placement="left-bottom">左下</button>
    </div>
    <div class="right">
        <button tooltip="右上" placement="right-top">右上</button>
        <button tooltip="右边" placement="right" effect="light">右边</button>
        <button tooltip="右下" placement="right-bottom">右下</button>
    </div>
    <div class="bottom">
        <button tooltip="下左" placement="bottom-left">下左</button>
        <button tooltip="下边" placement="bottom" effect="light">下边</button>
        <button tooltip="下右" placement="bottom-right">下右</button>
    </div>
</div>

css代码

由于内容较多,只提供主要部分

气泡样式

[tooltip]::after {
  display: none;
  content: attr(tooltip);
  position: absolute;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  padding: 8px 15px;
  max-width: 200px;
  border-radius: 4px;
  box-shadow: 0 10px 20px -5px rgba(0, 0, 0, 0.4);
  z-index: 100;
}

气泡位置(示例上)

.tooltip-placement-top,
[tooltip]:not([placement])::after,
[tooltip][placement=""]::after,
[tooltip][placement="top"]::after {
    bottom: calc(100% + 10px);
    left: 50%;
    transform: translate(-50%, 0);
}
.tooltip-placement-top-right,
[tooltip][placement="top-right"]::after {
    bottom: calc(100% + 10px);
    left: 100%;
    transform: translate(-100%, 0);
}
.tooltip-placement-top-left,
[tooltip][placement="top-left"]::after {
    bottom: calc(100% + 10px);
    left: 0;
    transform: translate(0, 0);
}

气泡动画(示例上)

[tooltip][placement^="top"]::after,
[tooltip][placement^="top"]::before {
    animation: anime-top 300ms ease-out forwards;
}
@keyframes anime-top {
  from {
      opacity: .5;
      bottom: 150%;
  }
}

三角形样式

[tooltip]::before {
  display: none;
  content: '';
  position: absolute;
  border: 5px solid transparent;
  border-bottom-width: 0;
  z-index: 100;
}

三角形位置(示例上)

.triangle-placement-top,
[tooltip]:not([placement])::before,
[tooltip][placement=""]::before,
[tooltip][placement="top"]::before {
    bottom: calc(100% + 5px);
    left: 50%;
    transform: translate(-50%, 0);
}
.triangle-placement-top-left,
[tooltip][placement="top-left"]::before {
    bottom: calc(100% + 5px);
    left: 10px;
}
.triangle-placement-top-right,
[tooltip][placement="top-right"]::before {
    bottom: calc(100% + 5px);
    right: 10px;
}

总结

css的功能越来越强大,是不是曾经以为这样的效果只能用js实现了,现在用css也可以搞定了,速度上车,试试吧。

演示程序:演示代码 下载源码:下载源码

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018年9月28日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • html代码
  • css代码
    • 气泡样式
      • 气泡位置(示例上)
        • 气泡动画(示例上)
          • 三角形样式
            • 三角形位置(示例上)
            • 总结
            领券
            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档