在Vue中使用tippy.js,封装为指令调用

文档地址

https://atomiks.github.io/tippyjs/

效果预览

http://demo.webwlx.cn/#/tips

封装为指令

// 封装提示指令
import tippy from 'tippy.js';
// 未导入全部,请根据文档自行导入所需样式文件
import 'tippy.js/dist/tippy.css';
import 'tippy.js/themes/light.css';
import 'tippy.js/themes/light-border.css';
import 'tippy.js/animations/scale.css';
import 'tippy.js/animations/scale-extreme.css';
//  创建tips
function createdTips(el, binding) {
	if (!binding.value.content) {
		// 删除class
		removeTips(el);
		return false;
	}
	// 添加 data-tips class
	el.classList.add('data-tips');
	let defaultConfigProps = {
		animation: 'scale-extreme',
	};
	tippy(el, Object.assign(defaultConfigProps, binding.value));
}
//  销毁
function removeTips(el) {
	el._tippy ? el._tippy.destroy() : '';
	el.classList.remove('data-tips');
}
// 封装指令
const tips = {
	mounted(el, binding) {
		createdTips(el, binding);
	},
	updated(el, binding) {
		createdTips(el, binding);
	},
	// 卸载
	unmounted(el) {
		removeTips(el);
	},
};
export default tips;

挂载指令

单组件引入

<!-- vue3 -->
<script setup>
  import tips from './tips.js';
  const vTips = tips;
</script>
<!-- vue2 -->
<script>
  export default {
    directives:{tips}
  }
</script>

全局引入main.js

// vue3
import tips from './tips.js';
app.directive('tips', tips);
// vue2
import tips from './tips.js';
Vue.directive('tips', tips);

使用指令

<n-button v-tips="{ content: '这是一个默认提示' }">Default</n-button>
<n-button v-tips="{ content: '这是一个默认提示', theme: 'light' }" type="tertiary"> Tertiary </n-button>
<n-button v-tips="{ content: '这是一个默认提示', theme: 'light-border' }" type="primary"> Primary </n-button>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值