js实现元素气泡

当表格中文本过多时,我们往往采用省略号的代替,鼠标滑过时用气泡展示全部内容。iview等UI框架自带有改组件,今天我们将用原生js实现改功能。
1、最终效果如下:
在这里插入图片描述
2、HTML

<div class="has-select-dropdown">
	出发地:<input type="text" v-model="searchForm.start" class="city" placeholder="出发地"/>
	目的地:<input type="text" v-model="searchForm.end" class="city" placeholder="目的地"/>
</div>

3、js

// 出发地、目的地、交通工具气泡
		let htmlDom = `<div class="ivu-poptip-content">
			<div class="ivu-poptip-arrow"></div>
			<div class="ivu-poptip-inner">
				<div class="ivu-poptip-body">
					<div class="ivu-poptip-body-content">
						<div class="ivu-poptip-body-content-inner"></div>
					</div>
				</div>
			</div>
		</div>`
		var elem=document.createElement('div');
		elem.setAttribute('class',"ivu-poptip-popper")
		elem.setAttribute('id',"travlCity")
		elem.setAttribute('x-placement',"top")
		elem.style.display = "none";
		elem.innerHTML = htmlDom;
		document.body.appendChild(elem);
		
		var cityDom = document.querySelectorAll(".has-select-dropdown")[0];
		if(cityDom){
			cityDom.addEventListener('mouseover', function (ev) {
				var ev = ev || window.event;
				var target = ev.target || ev.srcElement;
				if(target.placeholder == "出发地" || target.placeholder == "目的地" || target.placeholder == "交通工具"){
					let val = ev.target.value;
					if(val){
						let popTip = document.getElementById('travlCity');
						if(popTip){
							popTip.querySelectorAll('.ivu-poptip-body-content-inner')[0].innerHTML = val;
							popTip.style.display = "block";
							popTip.style.top = ev.target.getBoundingClientRect().top + document.documentElement.scrollTop - 55 + "px";
							popTip.style.left = ev.target.getBoundingClientRect().left + target.offsetWidth/2 - popTip.offsetWidth/2 + "px"
						}
					}
				}
			});
			cityDom.addEventListener('mouseout', function (ev) {
				var ev = ev || window.event;
				var target = ev.target || ev.srcElement;
				if(target.placeholder == "出发地" || target.placeholder == "目的地" || target.placeholder == "交通工具"){
					let popTip = document.getElementById('travlCity');
					if(popTip){
						popTip.style.display = "none";
					}
				}
			});
		}

4、css

</style lang="less">
	body{
		.ivu-poptip-popper {
			min-width: 150px;
			display: block;
			visibility: visible;
			font-size: 12px;
			line-height: 22px;
			position: absolute;
			z-index: 1060;
		}
		.ivu-poptip-popper[x-placement=top] .ivu-poptip-arrow {
			left: 50%;
			margin-left: -7px;
			bottom: 3px;
			border-width: 7px 7px 0;
			border-top-color: hsla(0,0%,85%,.5);
		}
	}
	
<style>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值