>>分享Web前端开发技术,并对孙卫琴的《精通Vue.js:Web前端开发技术详解》提供技术支持 书籍支持  卫琴直播  品书摘要  在线测试  资源下载  联系我们
发表一个新主题 开启一个新投票 回复文章 您是本文章第 27048 个阅读者 刷新本主题
 * 贴子主题:  CSS 提示工具(Tooltip) 回复文章 点赞(0)  收藏  
作者:sunshine    发表时间:2019-08-10 19:45:40     消息  查看  搜索  好友  邮件  复制  引用

  

CSS 提示工具(Tooltip)

     本文我们为大家介绍如何使用 HTML 与 CSS 来创建提示工具。

     提示工具在鼠标移动到指定元素后触发,先看以下四个范例:

.tooltip {

    position: relative;

    display: inline-block;

    border-bottom: 1px dotted #ccc;

    zcursor: help;

    color: #006080;

}

.tooltip .tooltiptext {

    visibility: hidden;

    position: absolute;

    width: 120px;

    background-color: #555;

    color: #fff;

    text-align: center;

    padding: 5px 0;

    border-radius: 6px;

    z-index: 1;

    opacity: 0;

    transition: opacity .6s;

}

.tooltip:hover .tooltiptext {

    visibility: visible;

    opacity: 1;

}

.tooltip .tooltiptext2 {

    visibility: hidden;

    position: absolute;

    width: 120px;

    background-color: #555;

    color: #fff;

    text-align: center;

    padding: 5px 0;

    border-radius: 6px;

    z-index: 1;

}

.tooltip:hover .tooltiptext2 {

    visibility: visible;

}

.tooltip-right {

  top: -5px;

  left: 125%;  

}

.tooltip-right2 {

  top: -5px;

  left: 105%;  

}

        .tooltip-right::after {

    content: "";

    position: absolute;

    top: 50%;

    right: 100%;

    margin-top: -5px;

    border-width: 5px;

    border-style: solid;

    border-color: transparent #555 transparent transparent;

}

        .tooltip-bottom {

  top: 135%;

  left: 50%;  

  margin-left: -60px;

}

        .tooltip-bottom2 {

  top: 125%;

  left: 50%;  

  margin-left: -60px;

}

        .tooltip-bottom::after {

    content: "";

    position: absolute;

    bottom: 100%;

    left: 50%;

    margin-left: -5px;

    border-width: 5px;

    border-style: solid;

    border-color: transparent transparent #555 transparent;

}

        .tooltip-top {

  bottom: 125%;

  left: 50%;  

  margin-left: -60px;

}

.tooltip-top2 {

  bottom: 115%;

  left: 50%;  

  margin-left: -60px;

}

        .tooltip-top::after {

    content: "";

    position: absolute;

    top: 100%;

    left: 50%;

    margin-left: -5px;

    border-width: 5px;

    border-style: solid;

    border-color: #555 transparent transparent transparent;

}

        .tooltip-left {

  top: -5px;

  bottom:auto;

  right: 128%;  

}

        .tooltip-left2 {

  top: -5px;

  bottom:auto;

  right: 105%;  

}

        .tooltip-left::after {

    content: "";

    position: absolute;

    top: 50%;

    left: 100%;

    margin-top: -5px;

    border-width: 5px;

    border-style: solid;

    border-color: transparent transparent transparent #555;

}

        .tooltip .tooltiptext-bottomarrow {

    visibility: hidden;

    width: 120px;

    background-color: #111;

    color: #fff;

    text-align: center;

    border-radius: 6px;

    padding: 5px 0;

    position: absolute;

    z-index: 1;

    bottom: 130%;

    left: 50%;

    margin-left: -60px;

}

.tooltip .tooltiptext-bottomarrow::after {

    content: "";

    position: absolute;

    top: 100%;

    left: 50%;

    margin-left: -5px;

    border-width: 5px;

    border-style: solid;

    border-color: black transparent transparent transparent;

}

.tooltip:hover .tooltiptext-bottomarrow {

    visibility: visible;

}

        .tooltip .tooltiptext-toparrow {

    visibility: hidden;

    width: 120px;

    background-color: #111;

    color: #fff;

    text-align: center;

    border-radius: 6px;

    padding: 5px 0;

    position: absolute;

    z-index: 1;

    top: 150%;

    left: 50%;

    margin-left: -60px;

}

.tooltip .tooltiptext-toparrow::after {

    content: "";

    position: absolute;

    bottom: 100%;

    left: 50%;

    margin-left: -5px;

    border-width: 5px;

    border-style: solid;

    border-color: transparent transparent black transparent;

}

.tooltip:hover .tooltiptext-toparrow {

    visibility: visible;

}

        .tooltip .tooltiptext-leftarrow {

    visibility: hidden;

    width: 120px;

    background-color: #111;

    color: #fff;

    text-align: center;

    border-radius: 6px;

    padding: 5px 0;

    position: absolute;

    z-index: 1;

    top: -5px;

    left: 110%;

}

.tooltip .tooltiptext-leftarrow::after {

    content: "";

    position: absolute;

    top: 50%;

    right: 100%;

    margin-top: -5px;

    border-width: 5px;

    border-style: solid;

    border-color: transparent black transparent transparent;

}

.tooltip:hover .tooltiptext-leftarrow {

    visibility: visible;

}

.tooltip .tooltiptext-rightarrow {

    visibility: hidden;

    width: 120px;

    background-color: #111;

    color: #fff;

    text-align: center;

    border-radius: 6px;

    padding: 5px 0;

    position: absolute;

    z-index: 1;

    top: -5px;

    right: 110%;

}

.tooltip .tooltiptext-rightarrow::after {

    content: "";

    position: absolute;

    top: 50%;

    left: 100%;

    margin-top: -5px;

    border-width: 5px;

    border-style: solid;

    border-color: transparent transparent transparent black;

}

.tooltip:hover .tooltiptext-rightarrow {

    visibility: visible;

}

        
    头部显示

     提示框文本          
                    右边显示

     提示框文本          
                    底部显示

     提示框文本                  
                    左边显示

     提示框文本          
        

基础提示框(Tooltip)

     提示框在鼠标移动到指定元素上显示:                                                                                                                    

HTML 代码:

< style >  /*  Tooltip 容器  */
.tooltip   {
     position:   relative  ;
     display:   inline-block  ;
     border-bottom:   1 px   dotted   black  ;  /*  悬停元素上显示点线  */
}

/*  Tooltip 文本  */
.tooltip   .tooltiptext   {
     visibility:   hidden  ;
     width:   120 px  ;
     background-color:   black  ;
     color:   #fff  ;
     text-align:   center  ;
     padding:   5 px   0  ;
     border-radius:   6 px  ;

     /*  定位  */
     position:   absolute  ;
     z-index:   1  ;
}

/*  鼠标移动上去后显示提示框  */
.tooltip :hover   .tooltiptext   {
     visibility:   visible  ;
}  </ style >

< div   class = " tooltip " > 鼠标移动到这
   < span   class = " tooltiptext " > 提示文本 </ span >
</ div >

范例解析

     HTML 使用容器元素 (like <div>) 并添加

    "tooltip" 类。在鼠标移动到 <div> 上时显示提示信息。

    提示文本放在内联元素上(如 <span>) 并使用class="tooltiptext"

     CSS tooltip 类使用 position:relative, 提示文本需要设置定位值 position:absolute

     注意: 接下来的范例会显示更多的定位效果。

     tooltiptext 类用于实际的提示文本。模式是隐藏的,在鼠标移动到元素显示 。设置了一些宽度、背景色、字体色等样式。

     CSS3 border-radius 属性用于为提示框添加圆角。

     :hover 选择器用于在鼠标移动到到指定元素 <div> 上时显示的提示。            

定位提示工具

     以下范例中,提示工具显示在指定元素的右侧(left:105%) 。

    注意 top:-5px 同于定位在容器元素的中间。使用数字 5 因为提示文本的顶部和底部的内边距(padding)是 5px。

    如果你修改 padding 的值,top 值也要对应修改,这样才可以确保它是居中对齐的。

    在提示框显示在左边的情况也是这个原理。                    

显示在右侧:

.tooltip   .tooltiptext   {
     top:  - 5 px  ;
     left:   105 %  ;
}

显示在左侧:

.tooltip   .tooltiptext   {
     top:  - 5 px  ;
     right:   105 %  ;
}

                  如果你想要提示工具显示在头部和底部。我们需要使用 margin-left 属性,并设置为 -60px。

     这个数字计算来源是使用宽度的一半来居中对齐,即: width/2 (120/2 = 60)。            

显示在头部:

.tooltip   .tooltiptext   {
     width:   120 px  ;
     bottom:   100 %  ;
     left:   50 %  ;
     margin-left:  - 60 px  ;  /*  使用一半宽度 (120/2 = 60) 来居中提示工具  */
}

显示在底部:

.tooltip   .tooltiptext   {
     width:   120 px  ;
     top:   100 %  ;
     left:   50 %  ;
     margin-left:  - 60 px  ;  /*  使用一半宽度 (120/2 = 60) 来居中提示工具  */
}

添加箭头

     我们可以用CSS 伪元素 ::after 及 content 属性为提示工具创建一个小箭头标志,箭头是由边框组成的,但组合起来后提示工具像个语音信息框。

     以下范例演示了如何为显示在顶部的提示工具添加底部箭头:                    

顶部提示框/底部箭头:

.tooltip   .tooltiptext : :after   {
     content:  " "  ;
     position:   absolute  ;
     top:   100 %  ;  /*  提示工具底部  */
     left:   50 %  ;
     margin-left:  - 5 px  ;
     border-width:   5 px  ;
     border-style:   solid  ;
     border-color:   black   transparent   transparent   transparent  ;
}

范例解析

       在提示工具内定位箭头: top: 100% , 箭头将显示在提示工具的底部。left: 50% 用于居中对齐箭头。

       注意:border-width 属性指定了箭头的大小。如果你修改它,也要修改 margin-left 值。这样箭头在能居中显示。

      border-color 用于将内容转换为箭头。设置顶部边框为黑色,其他是透明的。如果设置了其他的也是黑色则会显示为一个黑色的四边形。

     以下范例演示了如何在提示工具的头部添加箭头,注意设置边框颜色:            

底部提示框/顶部箭头:

.tooltip   .tooltiptext : :after   {
     content:  " "  ;
     position:   absolute  ;
     bottom:   100 %  ;   /*  提示工具头部  */
     left:   50 %  ;
     margin-left:  - 5 px  ;
     border-width:   5 px  ;
     border-style:   solid  ;
     border-color:   transparent   transparent   black   transparent  ;
}

                  以下两个范例是左右两边的箭头范例:                            

右侧提示框/左侧箭头:

.tooltip   .tooltiptext : :after   {
     content:  " "  ;
     position:   absolute  ;
     top:   50 %  ;
     right:   100 %  ;  /*  提示工具左侧  */
     margin-top:  - 5 px  ;
     border-width:   5 px  ;
     border-style:   solid  ;
     border-color:   transparent   black   transparent   transparent  ;
}

左侧提示框/右侧箭头:

.tooltip   .tooltiptext : :after   {
     content:  " "  ;
     position:   absolute  ;
     top:   50 %  ;
     left:   100 %  ;  /*  提示工具右侧  */
     margin-top:  - 5 px  ;
     border-width:   5 px  ;
     border-style:   solid  ;
     border-color:   transparent   transparent   transparent   black  ;
}

淡入效果

     我们可以使用 CSS3 transition 属性及 opacity 属性来实现提示工具的淡入效果:            

左侧提示框/右侧箭头:

.tooltip   .tooltiptext   {
     opacity:   0  ;
     transition:   opacity   1 s  ;
}

.tooltip :hover   .tooltiptext   {
     opacity:   1  ;
}



程序猿的技术大观园:www.javathinker.net



[这个贴子最后由 flybird 在 2020-03-01 10:03:00 重新编辑]
  Java面向对象编程-->内部类
  JavaWeb开发-->面向对象开发方法概述之开发思想(上)
  JSP与Hibernate开发-->接口
  Java网络编程-->面向对象开发方法概述之UML语言(下)
  精通Spring-->面向对象开发方法概述之UML语言(下)
  Vue3开发-->Web运作原理(Ⅲ)
  聊聊Nodejs中的模块化和事件循环
  vue30道面试题
  Vue3.0里为什么要用 Proxy API 替代 defineProperty API ? ...
  vue3-Composition-API的用法
  彻底明白VUE中的done参数和函数作用
  Vue路由传递参数详细说明
  vue+file-saver+jszip实现批量导出图片
  JS对树形数据的遍历和过滤,
  一个AJAX入门范例
  JavaScript的Screen 对象
  CSS 盒子模型
  HTML5 播放Audio(音频)
  HTML5简介
  JavaScript编程规范
  DTD - XML 构建模块
  更多...
 IPIP: 已设置保密
楼主      
1页 0条记录 当前第1
发表一个新主题 开启一个新投票 回复文章


中文版权所有: JavaThinker技术网站 Copyright 2016-2026 沪ICP备16029593号-2
荟萃Java程序员智慧的结晶,分享交流Java前沿技术。  联系我们
如有技术文章涉及侵权,请与本站管理员联系。