前端之路:一款轻量的tooltip插件tippy.js

安利一款轻量的tooltip插件,使用简单,扩展性好,而且作者一直都有更新维护,用在项目中最好不过了。

官方GitHub : https://github.com/atomiks/tippyjs

CSDN下载插件: http://download.csdn.net/download/qq_25835645/10147435

 

 

简要教程(如下) : 

 

 安装

可以通过npm来安装在Tippy.js插件。

npm install --save tippy.js                 

 使用方法

在页面中引入tippy.js和tippy.css文件。

<linkrel="stylesheet"href="css/tippy.css">

<scriptsrc='path/to/tippy.js'></script>                 

 HTML结构

你需要为使用tooltip的元素设置一个title属性,这个属性中的内容就是tooltip的内容。

<buttonclass="btn tippy" title="I'm a tooltip!">tooltip测试</button>               

 初始化插件

在页面DOM元素加载完毕之后,通过new Tippy()方法来实例化tooltip。

newTippy('.tippy')         

一个完整的使用tippy.js的HTML文档的结构如下:

<!DOCTYPE html>

<html>

  <head>

    <linkrel="stylesheet"href="tippy.css">

  </head>

  <body>

    <buttonid="myId"title="Tooltip text">Button text</button>

    <scriptsrc="tippy.js"></script>

    <script>

    new Tippy('#myId')

    </script>

  </body>

</html>                

 配置参数

你可以在实例化Tippy对象时以对象的方式传入配置参数,例如:

newTippy('.tippy', { position: 'right', animation: 'fade'})                 

也可以在HTML中直接以data-*的方式来使用配置参数:

<buttonclass="btn tippy" title="I'm a tooltip!"

              data-animatefill="false"

              data-animation="scale"

              data-position="bottom">Overridden</button>                

Tippy.js所有可用的配置参数如下:

参数默认值可选值描述
position'top''top' 'bottom' 'left' 'right'指定tooltip出现的位置。
trigger'mouseenter focus''mouseenter' 'focus' 'click' 'manual'指定触发tooltip的事件。
interactivefalsetrue falsetoltip是否可互动。
delay0>=0的整数指定多少毫秒之后才显示tooltip。
animation'shift''shift' 'perspective' 'fade' 'scale' 'none'指定tooltip的动画类型。
arrowfalsetrue false是否在tooltip上显示箭头。
animateFilltruetrue false添加material design风格的动画。如果arrow设置为true,该选项无效。
duration400>=0的整数tooltip的持续动画时间。
htmlfalsefalse或模板的id是否允许在tooltip中显示html模板内容。
theme'dark''dark' 'light'tooltip的主题。
offset0任何数值tooltip的偏移值,单位像素。
hideOnClicktruetrue false指定是否在悬停后单击其元素时隐藏tooltip。

 事件

Tippy.js提供了4个可用的回调函数:

newTippy('.tippy', {

  beforeShown:function() {

    // When the tooltip has been triggered and has started to transition in

  },

  shown:function() {

    // When the tooltip has fully transitioned in and is showing

  },

  beforeHidden:function() {

    // When the tooltip has begun to transition out

  },

  hidden:function() {

    // When the tooltip has fully transitioned out and is hidden

  }

})            

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值