tooltip(支持input输入框)

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<link rel="stylesheet" type="text/css" href="tooltip.css" />
		<style type="text/css">
			* {
				box-sizing: border-box;
			}
		</style>
	</head>
	<body style="padding:1990px 20px 1990px 20px ">
		
		<div tooltip tooltipWidth='auto' inTop>2wqw</div><br>
		<div ><input type="text" tooltip tooltipWidth='auto'></div><br>
		<div style="padding-left: 333px;"><input type="text" tooltip tooltipWidth='300px' inTop></div><br>
		<div><input type="text" tooltip inTop></div><br>
		<!-- 
		tooltipWidth:代表提示框宽度,不设置的话跟表单宽度一样宽
		tooltipWidth='auto' 时文字自动撑开
		inTop: 代表提示框放在表单上面
		-->
		
		
		<!-- <div class='tooltip-box inTop' style='max-width: 155px;top:50px;left:20px'>ewrewrweewrewrweewrewrweewrewrweewrewrweewrewrweewrewrwe<div class="popper__arrow"></span></div> -->

		<script src="jquery-1.11.3.min.js" type="text/javascript" charset="utf-8"></script>
		<script src="tooltip.js" type="text/javascript" charset="utf-8"></script>
		
	</body>
</html>

tooltip.js 

(function($) {
	const num = 10
	$('[tooltip]').mouseover(function() {
		const tagNameIsINPUT = $(this)[0].tagName === 'INPUT'
		const top = $(this).offset().top
		const left = $(this).offset().left
		const val = tagNameIsINPUT ? $(this).val() : $(this).html()
		const h = $(this).outerHeight()
		const w = $(this).outerWidth()
		const tooltipWidth = $(this).attr('tooltipWidth')
		const inTop = $(this).attr('inTop')
		const wh = window.innerHeight

		if ((tagNameIsINPUT && val) || !tagNameIsINPUT) {
			$('body').append(
				`<div class='tooltip-box ${inTop!==undefined?'inTop':''}'  style='top:${top+h+num}px;left:${left}px;width:${tooltipWidth?tooltipWidth:w+'px'};'><span class='val-span'>${val}</span><span class="popper__arrow"></span></div>`
			)
			const tooltipBoxHeight = $('.tooltip-box').outerHeight()
			if (inTop !== undefined) {
				$(".tooltip-box").css("top", `${top-tooltipBoxHeight-num}px`)
			}
			if (parseFloat(tooltipWidth) > w) {
				$(".tooltip-box .popper__arrow").addClass("arrowInLeft")
			}
		}
	}).mouseout(function() { //鼠标指针从 a标签 上离开时 发生mouseout 事件
		$(".tooltip-box").remove(); //移除对象
	})

	$('input[tooltip]').bind('input propertychange', function() {
		const val = $(this).val()
		$(".tooltip-box .val-span").html($(this).val())
		if (!val) {
			$(".tooltip-box").remove(); //移除对象
		}
	});
})(jQuery)

tooltip.css 

.tooltip-parent {
	position: relative;
}

input[tooltip] {
	white-space: nowrap;
	text-overflow: ellipsis;
	overflow: hidden;
}

.tooltip-box {
	/* animation: fadeInDown 0.2s ease 0s 1 both; */
	position: absolute;
	background: #fff;
	/* min-width: 150px; */
	border-radius: 4px;
	border: 1px solid #ebeef5;
	z-index: 2000;
	color: #606266;
	line-height: 1.4;
	text-align: justify;
	font-size: 14px;
	box-shadow: 0 2px 12px 0 rgb(0 0 0 / 10%);
	word-break: break-all;
	padding: 8px 10px;
	top: 0;
	left: 0;
	transform-origin: center top;

}

@keyframes fadeInDown {
	0% {
		opacity: 0;
		-webkit-transform: translate3d(0, -50%, 0);
		-ms-transform: translate3d(0, -50%, 0);
		transform: translate3d(0, -50%, 0)
	}

	100% {
		opacity: 1;
		-webkit-transform: none;
		-ms-transform: none;
		transform: none
	}
}

.popper__arrow {
	position: absolute;
	display: block;
	width: 0;
	height: 0;
	border-color: transparent;
	border-style: solid;
	border-width: 6px;
	filter: drop-shadow(0 2px 12px rgba(0, 0, 0, .03));
	top: -6px;
	left: 50%;
	/* margin-right: 3px; */
	border-top-width: 0;
	border-bottom-color: #ebeef5;
	transform: translateX(-50%);
}
.popper__arrow.arrowInLeft{
	transform: translateX(0);
	left: 20px;
}

.popper__arrow::before {
	top: -5px;
	margin-left: -6px;
	border-top-width: 0;
	content: " ";
	border-width: 6px;
	position: absolute;
	display: block;
	width: 0;
	height: 0;
	border-color: transparent;
	border-bottom-color: #fff;
	border-style: solid;
}

.inTop .popper__arrow {
	top: unset;
	bottom: -6px;
	transform: rotate(180deg) translateX(50%);
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值