【前端开发】常见自定义指令tips
Vue.directive('tip', {
bind(el, binding) {
el.style.position = 'relative'
// icon插入
const a = `<i type="primary" class="el-icon-question question-style-table"></i>`
const objE = document.createElement('div')
objE.innerHTML = a
const iconElementArr: any = objE.childNodes
const iconElement = iconElementArr[0]
iconElement.style.color = '#c0c4cc'
iconElement.style.position = 'absolute'
iconElement.style.right = '-16px'
iconElement.style.zIndex = 999999999
if (binding.value.tipsType === 'table') {
iconElement.style.top = '2px'
} else {
iconElement.style.top = '8px'
}
el.appendChild(iconElement)
// hover提示插入
const tip = document.createElement('span')
tip.className = 'tooltip'
// 提示框样式
const css: any = {
display: 'none',
position: 'fixed',
width: '300px',
padding: '5px 10px',
borderRadius: '4px',
fontSize: '12px',
color: '#333333',
backgroundColor: 'white',
lineHeight: 1.4,
textAlign: 'left',
zIndex: 9999999999,
border: '1px solid #999'
}
const pos: any = setPosition(tip, binding.value.position, binding.value.bgColor)
for (const item in pos) {
css[item] = pos[item]
}
tip.innerHTML = binding.value.content
setCss(tip, css)
iconElement.addEventListener('mouseover', function(e: any) {
console.log(e)
if (binding.value.position === 'left') {
tip.style.left = `${e.clientX + 16}px`
tip.style.top = `${e.clientY - 6}px`
} else {
tip.style.left = `${e.clientX + 6}px`
tip.style.top = `${e.clientY - 10}px`
}
tip.style.display = 'block'
})
iconElement.addEventListener('mouseout', function() {
tip.style.display = 'none'
})
document.body.appendChild(tip)
}
})
本文来自博客园,作者:JeckHui;
个人主页:前端实用站点推荐; 热榜资讯早读:热榜资讯-导航;
转载请注明原文链接:https://www.cnblogs.com/xiaohuizhang/p/16868550.html
分类:
vue开发
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)