先看下效果html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
/* 高亮元素盒子 */
.list-box {
margin-top: 60px;
}
/* 高亮元素樣式 */
.list-box-item {
list-style: none;
background: paleturquoise;
float: left;
margin: 2px;
padding: 5px;
}
/* 遮罩層 */
.mark {
position: fixed;
top: 0;
right: 0;
bottom: 0;
left: 0;
overflow: hidden;
outline: 0;
-webkit-overflow-scrolling: touch;
background-color: rgb(0, 0, 0);
filter: alpha(opacity=60);
background-color: rgba(0, 0, 0, 0.3);
z-index: 521;
}
/* 高亮區域 */
.content {
z-index: 1314;
border: 1px dashed #fff;
position: absolute;
}
/* 有意見,把肚子裏的吐🤮出來 */
.tooltip-box {
width: 150px;
height: 26px;
line-height: 26px;
background: white;
position: relative;
-moz-border-radius: 40px;
-webkit-border-radius: 40px;
border-radius: 40px;
padding: 5px 15px;
font-size: 14px;
}
/*加個小尾巴*/
.tooltip-box:before {
content: "";
position: absolute;
right: 100%;
top: -10px;
left: 20%;
width: 0;
height: 0;
border-left: 10px solid transparent;
border-right: 10px solid transparent;
border-bottom: 13px solid white;
}
</style>
</head>
<body>
<div class="mark">
<div class="content" id='js-mark-content-id'>
</div>
<div class='tooltip-box' id='js-tooltip-box-id'>
我是新來的一個功能!!!!!
</div>
</div>
</div>
<div class="list-box">
<div class="list-box-item">花自飄零水自流</div>
<div class="list-box-item">一種相思</div>
<div class="list-box-item" id='js-show-id'>兩處閒愁</div>
<div class="list-box-item">此情無計可消除</div>
<div class="list-box-item">才下眉頭</div>
<div class="list-box-item">卻上心頭</div>
</div>
</body>
<script>
const show = document.getElementById('js-show-id')
const showLeft = show.offsetLeft
const showTop = show.offsetTop
const showWidth = show.offsetWidth
const showHeight = show.offsetHeight
//設置一個框
const contentMark = document.getElementById('js-mark-content-id')
contentMark.style.left = showLeft - 3 + 'px'
contentMark.style.top = showTop - 3 + 'px'
const showClone = show.cloneNode(true)
contentMark.appendChild(showClone)
//設置一個提示語
const tooltipBox = document.getElementById('js-tooltip-box-id')
tooltipBox.style.left = showLeft + 'px'
tooltipBox.style.top = showTop + showHeight + 15 + 'px'
</script>
</html>
複製代碼