最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

基于JavaScript簡單實現(xiàn)一下新手引導效果

 更新時間:2023年03月07日 10:09:47   作者:頭疼腦脹的代碼搬運工  
這篇文章主要為大家詳細介紹了如何基于JavaScript簡單實現(xiàn)一下新手引導效果,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下

一、實現(xiàn)效果

二、實現(xiàn)

實現(xiàn)其實很簡單,mask蒙版就是平鋪一個整屏的 div,設置背景顏色為透明 transparent ,然后,再設置 outline 為半透明及足夠?qū)捑涂梢粤?,再用同樣的方式?chuàng)建一個 箭頭警告 標簽。

1、用法

let maskIntroduceManage = new MaskIntroduceManage([
    new MaskIntroduceItem('one','人生若只如初見'),
    new MaskIntroduceItem('two','何事秋風悲畫扇'),
    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','何事秋風悲畫扇'),
    new MaskIntroduceItem('five','等閑卻變故人心'),
    new MaskIntroduceItem('six','驪山語罷清宵半'),
    new MaskIntroduceItem('four','卻道故人心易變'),
    new MaskIntroduceItem('finally','謝謝大家支持!')
])
maskIntroduceManage.benginIntroduce()
</script>
</html>

3、JS

// 單元信息model
class MaskIntroduceItem {
    // 需要引導的dom的ID
    id
    // 需要引導的dom功能描述
    warming
    constructor(id,warming){
        this.id = id
        this.warming = warming
    }
}

// 遮罩操作類
class MaskIntroduceManage {
    // 消息展示類集合
    maskIntroduceItems
    // 遮罩層
    el
    // 遮罩層提示框
    warmingEl
    // 指引肩頭
    guidanceEl
    // 展示的第幾個
    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)
        //設置body禁止?jié)L動
        body.style.overflow = 'hidden'
        //保留window事件
        if(window.onclick){
            this.windowEvent = window.onclick
        }
        window.onclick = ()=>{
            this.nextIntroduce()
        }
    }

    // 開始引導
    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'
        //設置對應倒角,但是由于背景顏色是透明的,所以,沒有效果(??????)
        //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
        //最后一個展示完恢復window點擊事件
        if(this.currentShowIndex >= this.maskIntroduceItems.length){
            setTimeout(() => {
                //移除當前遮罩
                this.el.remove()
                //移除當前提示框
                this.warmingEl.remove()
                //移除箭頭
                this.guidanceEl.remove()
                //設置body可以滾動
                document.getElementsByTagName('body')[0].style.overflow = 'auto'
                //恢復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é)與思考

實現(xiàn)原理特別簡單,沒有太多復雜的邏輯在里面,想通過當前“需要介紹”的標簽的 borderRadius 來設置鏤空部分的倒角值,但是背景顏色是透明的,因此設置了,可以生效但也沒有效果。

到此這篇關于基于JavaScript簡單實現(xiàn)一下新手引導效果的文章就介紹到這了,更多相關JavaScript實現(xiàn)新手引導內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • js文件包含的幾種方式介紹

    js文件包含的幾種方式介紹

    這篇文章主要介紹了js文件包含的幾種方式,在某些情況下還是比較實用的,下面以示例代碼的方式來呈現(xiàn)
    2014-09-09
  • 使用js實現(xiàn)動態(tài)背景

    使用js實現(xiàn)動態(tài)背景

    這篇文章主要為大家詳細介紹了使用js實現(xiàn)動態(tài)背景,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • js制作提示框插件

    js制作提示框插件

    這篇文章主要介紹了js制作提示框插件的方法,幫助大家更好的理解和使用js,感興趣的朋友可以了解下
    2020-12-12
  • JS實現(xiàn)的表格行鼠標點擊高亮效果代碼

    JS實現(xiàn)的表格行鼠標點擊高亮效果代碼

    這篇文章主要介紹了JS實現(xiàn)的表格行鼠標點擊高亮效果代碼,涉及JavaScript響應鼠標事件動態(tài)變換頁面元素屬性的相關技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-11-11
  • Javascript中this綁定的3種方法與比較

    Javascript中this綁定的3種方法與比較

    大家都知道JS是一門動態(tài)語言,與傳統(tǒng)的c和c++最大的區(qū)別就是js是在運行時動態(tài)檢測值的類型和變化。this是js中的一個關鍵字,它代表當前作用域的上下文環(huán)境,而且隨著上下文的改變而動態(tài)變化。這篇文章我們將詳細介紹Javascript中綁定this的三種方法與簡單的比較。
    2016-10-10
  • js中json處理總結(jié)之JSON.parse

    js中json處理總結(jié)之JSON.parse

    parse是解析json數(shù)據(jù)多種方法中的其中一種。這篇文章主要介紹了js中json處理總結(jié)之JSON.parse,需要的朋友可以參考下
    2016-10-10
  • jQuery實現(xiàn)div浮動層跟隨頁面滾動效果

    jQuery實現(xiàn)div浮動層跟隨頁面滾動效果

    這篇文章主要介紹了jQuery實現(xiàn)div浮動層跟隨頁面滾動效果,需要的朋友可以參考下
    2014-02-02
  • 6個DIV 135或246間隔一秒輪番顯示效果

    6個DIV 135或246間隔一秒輪番顯示效果

    6個DIV 間隔一秒 輪番進行奇偶顯示. 比如先135 一秒后 246 然后再135 循環(huán)
    2010-07-07
  • java實現(xiàn)單鏈表增刪改查的實例代碼詳解

    java實現(xiàn)單鏈表增刪改查的實例代碼詳解

    在本篇文章里小編給大家整理了關于java實現(xiàn)單鏈表增刪改查的實例內(nèi)容,需要的朋友們可以參考下。
    2019-08-08
  • 簡單理解js的prototype屬性及使用

    簡單理解js的prototype屬性及使用

    這篇文章主要介紹了簡單理解js的prototype屬性及使用 ,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-12-12

最新評論

大新县| 南郑县| 瑞昌市| 蛟河市| 宜宾县| 叙永县| 申扎县| 鄂伦春自治旗| 永顺县| 邵阳县| 宁国市| 济宁市| 河曲县| 博客| 灵丘县| 左权县| 金昌市| 鄂托克前旗| 监利县| 海城市| 林周县| 漳平市| 临洮县| 浑源县| 泌阳县| 泸定县| 桐梓县| 抚顺市| 忻州市| 台南市| 宁城县| 土默特左旗| 望城县| 鹤山市| 绥宁县| 龙山县| 苍山县| 毕节市| 禹城市| 兴义市| 全椒县|