JavaScript實(shí)現(xiàn)頁面高亮操作提示和蒙板
本文實(shí)例為大家分享了JavaScript實(shí)現(xiàn)頁面高亮操作提示和蒙板的具體代碼,供大家參考,具體內(nèi)容如下
在頁面上,有時(shí)候會遇到操作提示,如下圖所示。
可以很直觀的告訴用戶,關(guān)鍵的操作在哪里,有什么做作用。

需要說明的是,被高亮的部分,并不是目標(biāo)的真實(shí)標(biāo)簽,而是用的其他標(biāo)簽?zāi)M的。
真實(shí)的標(biāo)簽被 mask 層蓋住了,在下方呢。
標(biāo)簽高亮的部分和操作提示框,都是用 js 動態(tài)生成的。
這里關(guān)鍵的知識點(diǎn):
1、要用 JS 獲取目標(biāo)標(biāo)簽的位置。
el.getBoundingClientRect() 可以獲得標(biāo)簽距離窗口的位置。
window.pageXOffset
window.pageYOffset
則是獲取頁面左,上邊滾動出去部分的長度。
利用它們相加,可以得到目標(biāo)標(biāo)簽在頁面中的絕對位置。再把【高亮】標(biāo)簽放在相同位置就可以。
2、要能動態(tài)生成提示標(biāo)簽和遮罩層(一般是半透明的黑色)
3、還要用到 CSS 中的定位。
為了更直接的模擬真實(shí)情況,我采用了一些標(biāo)簽來模擬頁面的結(jié)構(gòu)。
HTML 代碼如下:
<!-- 模擬頭部 --> <header class="header"> 模擬頭部 </header> <!-- 模擬頭部 end --> <!-- 模擬導(dǎo)航 --> <nav class="mainNav"> 模擬導(dǎo)航 </nav> <!-- 模擬導(dǎo)航 end --> <!-- 主體部分 --> <main class="pageMain"> <ul class="sidebar"> <li id="step1"><a href="#" >操作第一步</a></li> <li><a href="#" >操作第二步</a></li> <li><a href="#">操作第三步</a></li> </ul> <div class="other"> 模擬其他部分 </div> </main> <!-- 主體部分 end -->
基本樣式如下:
.posa{
position: absolute;
}
.header{
height: 200px;
width: 1200px;
margin-left: auto;
margin-right: auto;
background: #eee;
}
.mainNav{
height: 80px;
width: 1200px;
margin-left: auto;
margin-right: auto;
background: #5f5fd7;
}
.pageMain{
width: 1200px;
margin-left: auto;
margin-right: auto;
background: #eee;
}
.sidebar{
width: 200px;
line-height: 50px;
text-align: center;
background: #fff;
border:1px #666 solid;
border-bottom:none;
}
.sidebar a{
display: block;
border-bottom:1px #666 solid;
color: #333;
}
.other{
height: 700px;
background: #708af5;
font-size: 30px;
color: #fff;
}
.mask{
position: fixed;
top:0;
right:0;
bottom: 0;
left:0;
background: rgba(0, 0, 0, 0.48);
}
.tips{
background: #fff;
position: absolute;
line-height: 50px;
color: #333;
display: block;
text-align: center;
}
.tipsc_content{
margin-left: 200px;
padding-top: 100px;
margin-right: 80px;
}
.tipsc_btn{
padding-top: 30px;
}
.tipsc_btn button{
outline:none;
width: 100px;
height: 40px;
background: #09a366;
color: #fff;
border:none;
cursor: pointer;
}
JavaScript 代碼如下:
// 獲取目標(biāo)標(biāo)簽
let step1 = document.getElementById("step1");
let body = document.getElementsByTagName("body")[0];
let tips = null,
mask = null,
tipsContent= null;
// 創(chuàng)建標(biāo)簽。默認(rèn)生成 mask 標(biāo)簽
let makeElement = function({id="mask",classN="mask",content = ""}={}){
let eId = document.getElementById(id);
if( !eId ){ // 判斷 mask 是否存在
eId = document.createElement("div");
eId.id = id;
eId.className =classN;
eId.innerHTML = content;
body.appendChild( eId );
return eId ;
}else{
return eId; // mask 已經(jīng)存在,不需要再創(chuàng)建。
}
};
// 去掉遮罩層
let removeTag = function(tag){
tag.parentNode.removeChild( tag );
};
// 獲取要提示的內(nèi)容的位置。這里是 li#step1
let getPostion = function(tag){
let x = tag.getBoundingClientRect().x ;
let y = tag.getBoundingClientRect().y ;
return {x:x,y:y};
};
// 設(shè)置tips的位置
let setPosition = function(tips, {x="0",y="0",w="0",h="0"}={}){
tips.style.left = x + "px" ;
tips.style.top = y+ "px" ;
tips.style.width = w+ "px"
tips.style.height = h + "px"
console.info(tagP.x , tagP.y );
};
// 獲取要提示的內(nèi)容的標(biāo)簽位置
let tagP = getPostion(step1);
// 生成 mask
mask = makeElement();
// 生成目標(biāo)標(biāo)簽的高亮框
tips = makeElement({
id:"tips",
classN:"tips",
content :"操作第一步" // 偽裝原標(biāo)簽的內(nèi)容
});
setPosition(tips, {
x:tagP.x + window.pageXOffset,
y:tagP.y + window.pageYOffset,
w:step1.offsetWidth ,
h:step1.offsetHeight
});
// 生成提示內(nèi)容框
tipsContent = makeElement({
id:"tipsContent",
classN:"tipsContent posa",
content :`<div style="width: 490px; height: 300px;background:url('images/op_tips.png') no-repeat;">
<div class="tipsc_content">
根據(jù)項(xiàng)目內(nèi)容調(diào)整樣式
<div class="tipsc_btn">
<button type="button" id="okBtn">確定</button>
</div>
</div>
</div>`
});
setPosition(tipsContent, {
x:tagP.x + window.pageXOffset+200,
y:tagP.y + window.pageYOffset-100,
w:490,
h:300
});
// 點(diǎn)擊“確定”按鈕
let okBtn = document.getElementById("okBtn");
okBtn.addEventListener("click",function(){
removeTag(mask);
removeTag(tips);
removeTag(tipsContent);
});
// 當(dāng)窗口調(diào)整大小時(shí)候,調(diào)整 tips 位置。
window.addEventListener("resize",function(){
tagP = getPostion(step1);
setPosition(tips,tagP);
});
簡單進(jìn)行了下函數(shù)封裝,但是還是覺得代碼寫的不夠完美。比如用JS生成了樣式,其實(shí)可以把一些樣式封裝在CSS 中。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
D3.js實(shí)現(xiàn)拓?fù)鋱D的示例代碼
本篇文章主要介紹了D3.js實(shí)現(xiàn)拓?fù)鋱D的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-06-06
原生javascript+css3編寫的3D魔方動畫旋扭特效
這篇文章主要介紹了原生javascript+css3編寫的3D魔方動畫旋扭特效的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-03-03
概述一個(gè)頁面從輸入U(xiǎn)RL到頁面加載完的過程
本文主要對一個(gè)頁面從輸入 URL 到頁面加載完的過程中都發(fā)生了什么事情進(jìn)行概述,對我們學(xué)習(xí)網(wǎng)絡(luò)語言有一定的幫助,下面就跟小編一起來看下吧2016-12-12
一個(gè)JS小玩意 幾個(gè)屬性相加不能超過一個(gè)特定值.
以前玩游戲,有給玩家角色分配什么魔法點(diǎn)數(shù),體力點(diǎn)數(shù)的功能,而且?guī)讉€(gè)屬性相加不能超過一個(gè)特定值.2009-09-09
javascript實(shí)現(xiàn)下雪效果【實(shí)例代碼】
下面小編就為大家?guī)硪黄猨avascript實(shí)現(xiàn)下雪效果【實(shí)例代碼】。小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考2016-05-05
微信企業(yè)號開發(fā)之微信考勤Cookies的使用
使用微信考勤,每次使用微信企業(yè)號開發(fā):微信用戶信息和web網(wǎng)頁的session的關(guān)系這個(gè)里邊的方法,調(diào)用微信的接口,有點(diǎn)慢,微信官方也推薦使用Cookies,但如何使用Cookies,自己卻一直沒有搞清楚。下面小編幫大家解決難題,需要的朋友可以參考下2015-09-09

