基于JavaScript簡(jiǎn)單實(shí)現(xiàn)一下新手引導(dǎo)效果
一、實(shí)現(xiàn)效果

二、實(shí)現(xiàn)
實(shí)現(xiàn)其實(shí)很簡(jiǎn)單,mask蒙版就是平鋪一個(gè)整屏的 div,設(shè)置背景顏色為透明 transparent ,然后,再設(shè)置 outline 為半透明及足夠?qū)捑涂梢粤?,再用同樣的方式?chuàng)建一個(gè) 箭頭 跟 警告 標(biāo)簽。
1、用法
let maskIntroduceManage = new MaskIntroduceManage([
new MaskIntroduceItem('one','人生若只如初見'),
new MaskIntroduceItem('two','何事秋風(fēng)悲畫扇'),
new MaskIntroduceItem('five','等閑卻變故人心'),
new MaskIntroduceItem('six','驪山語罷清宵半'),
new MaskIntroduceItem('four','卻道故人心易變'),
new MaskIntroduceItem('finally','謝謝大家支持!')
])
maskIntroduceManage.benginIntroduce()
2、HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<style type="text/css">
*{
padding: 0;
margin: 0;
}
.content {
padding: 0;
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
width: 100%;
}
span {
width: 60px;
height: 60px;
line-height: 60px;
margin-left: 40px;
margin-top: 140px;
margin-bottom: 0px;
text-align: center;
display: block;
background-color: antiquewhite;
}
.finally {
width: 100px;
height: 100px;
background-color: cornsilk;
border-radius: 50%;
line-height: 100px;
text-align: center;
margin-top: 30px;
margin-left: auto;
margin-right: auto;
}
span:nth-of-type(1){
margin-top: 30px;
}
span:nth-of-type(2){
margin-top: 70px;
}
span:nth-of-type(3){
margin-top: 160px;
}
span:nth-of-type(4){
margin-top: 160px;
}
span:nth-of-type(5){
margin-top: 70px;
}
span:nth-of-type(6){
margin-top: 30px;
}
</style>
<body>
<div class="content">
<span id="one">納</span>
<span id="two">蘭</span>
<span id="three">容</span>
<span id="four">若</span>
<span id="five">作</span>
<span id="six">詞</span>
</div>
<div class="finally" id="finally">
謝謝
</div>
</body>
<script src="./maskIntroduce.js"></script>
<script>
let maskIntroduceManage = new MaskIntroduceManage([
new MaskIntroduceItem('one','人生若只如初見'),
new MaskIntroduceItem('two','何事秋風(fēng)悲畫扇'),
new MaskIntroduceItem('five','等閑卻變故人心'),
new MaskIntroduceItem('six','驪山語罷清宵半'),
new MaskIntroduceItem('four','卻道故人心易變'),
new MaskIntroduceItem('finally','謝謝大家支持!')
])
maskIntroduceManage.benginIntroduce()
</script>
</html>3、JS
// 單元信息model
class MaskIntroduceItem {
// 需要引導(dǎo)的dom的ID
id
// 需要引導(dǎo)的dom功能描述
warming
constructor(id,warming){
this.id = id
this.warming = warming
}
}
// 遮罩操作類
class MaskIntroduceManage {
// 消息展示類集合
maskIntroduceItems
// 遮罩層
el
// 遮罩層提示框
warmingEl
// 指引肩頭
guidanceEl
// 展示的第幾個(gè)
currentShowIndex = 0
// 記錄window事件
windowEvent = null
constructor(maskIntroduceItems){
this.maskIntroduceItems = maskIntroduceItems
}
// 添加消息展示類
addIntroduceItem(introduceItem){
this.maskIntroduceItems.push(introduceItem)
}
// body增加遮罩
addMaskToBody(){
//添加遮罩框
this.el = document.createElement('div')
this.el.style.cssText = 'position: fixed;background: transparent;outline:rgba(0, 0, 0, 0.5) 3500px solid;'
let body = document.getElementsByTagName('body')[0]
body.appendChild(this.el)
//添加提示框
this.warmingEl = document.createElement('div')
this.warmingEl.style.cssText = 'position:fixed;width:100px;background:white;border-radius: 10px;padding: 30px;font-size: 14px;'
body.appendChild(this.warmingEl)
//添加指引箭頭
this.guidanceEl = document.createElement('div')
this.guidanceEl.style.cssText = 'position:fixed;width: 14px; height: 13px; background-color: white;clip-path: polygon(50% 0,100% 100%,0 100%);'
body.appendChild(this.guidanceEl)
//設(shè)置body禁止?jié)L動(dòng)
body.style.overflow = 'hidden'
//保留window事件
if(window.onclick){
this.windowEvent = window.onclick
}
window.onclick = ()=>{
this.nextIntroduce()
}
}
// 開始引導(dǎo)
benginIntroduce(){
this.addMaskToBody()
this.nextIntroduce()
}
// 下一步
nextIntroduce(){
let maskIntroduceItem = this.maskIntroduceItems.length > 0 ? this.maskIntroduceItems[this.currentShowIndex] : null
if(!maskIntroduceItem){
return
}
let needIntroduceEl = document.getElementById(maskIntroduceItem.id)
//遮罩層的鏤空位置
this.el.style.width = needIntroduceEl.offsetWidth + 'px'
this.el.style.height = needIntroduceEl.offsetHeight + 'px'
this.el.style.top = this.getElementPosition(needIntroduceEl).top + 'px'
this.el.style.left = this.getElementPosition(needIntroduceEl).left + 'px'
//設(shè)置對(duì)應(yīng)倒角,但是由于背景顏色是透明的,所以,沒有效果(??????)
//this.el.style.borderRadius = window.getComputedStyle(needIntroduceEl,null)['border-radius']
this.currentShowIndex ++
//指引箭頭位置
let guidanceElLeft = this.getElementPosition(needIntroduceEl).left + needIntroduceEl.offsetWidth / 2.0
this.guidanceEl.style.top = this.getElementPosition(needIntroduceEl).top + needIntroduceEl.offsetHeight + 20 + 'px'
this.guidanceEl.style.left = guidanceElLeft + 'px'
//提示框的位置
this.warmingEl.style.top = this.getElementPosition(this.guidanceEl).top + this.guidanceEl.offsetHeight - 4 + 'px'
let warmingElLeft = this.getElementPosition(needIntroduceEl).left - ((this.warmingEl.offsetWidth - needIntroduceEl.offsetWidth) / 2.0)
if(warmingElLeft < 0){
warmingElLeft = this.getElementPosition(needIntroduceEl).left + 10
}
if(warmingElLeft + this.warmingEl.offsetWidth > document.getElementsByTagName('body')[0].offsetWidth){
warmingElLeft = warmingElLeft - 10 - (this.warmingEl.offsetWidth - needIntroduceEl.offsetWidth) / 2.0
}
this.warmingEl.style.left = warmingElLeft + 'px'
this.warmingEl.innerHTML = maskIntroduceItem.warming
//最后一個(gè)展示完恢復(fù)window點(diǎn)擊事件
if(this.currentShowIndex >= this.maskIntroduceItems.length){
setTimeout(() => {
//移除當(dāng)前遮罩
this.el.remove()
//移除當(dāng)前提示框
this.warmingEl.remove()
//移除箭頭
this.guidanceEl.remove()
//設(shè)置body可以滾動(dòng)
document.getElementsByTagName('body')[0].style.overflow = 'auto'
//恢復(fù)window事件
if(this.windowEvent){
window.onclick = this.windowEvent
}
}, 2000);
}
}
// 獲取元素在屏幕的位置
getElementPosition(element){
var top = element.offsetTop
var left = element.offsetLeft
var currentParent = element.offsetParent;
while (currentParent !== null) {
top += currentParent.offsetTop
left += currentParent.offsetLeft
currentParent = currentParent.offsetParent
}
return {top,left}
}
}三、總結(jié)與思考
實(shí)現(xiàn)原理特別簡(jiǎn)單,沒有太多復(fù)雜的邏輯在里面,想通過當(dāng)前“需要介紹”的標(biāo)簽的 borderRadius 來設(shè)置鏤空部分的倒角值,但是背景顏色是透明的,因此設(shè)置了,可以生效但也沒有效果。
到此這篇關(guān)于基于JavaScript簡(jiǎn)單實(shí)現(xiàn)一下新手引導(dǎo)效果的文章就介紹到這了,更多相關(guān)JavaScript實(shí)現(xiàn)新手引導(dǎo)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JavaScript如何獲取到導(dǎo)航條中HTTP信息
這篇文章主要為大家詳細(xì)介紹了JavaScript如何獲取到導(dǎo)航條中HTTP信息,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10
微信小程序?qū)崿F(xiàn)兩邊小中間大的輪播效果的示例代碼
這篇文章主要介紹了微信小程序?qū)崿F(xiàn)兩邊小中間大的輪播效果的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-12-12
javascript顯式類型轉(zhuǎn)換實(shí)例分析
這篇文章主要介紹了javascript顯式類型轉(zhuǎn)換,實(shí)例分析了javascript實(shí)現(xiàn)類型轉(zhuǎn)換的常用技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04
原生JS實(shí)現(xiàn)圖片網(wǎng)格式漸顯、漸隱效果
這篇文章主要介紹了原生JS實(shí)現(xiàn)圖片網(wǎng)格式漸顯、漸隱效果,需要的朋友可以參考下2017-06-06

