欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

使用CSS和Vanilla.js怎么實(shí)現(xiàn)tooltip提示框-創(chuàng)新互聯(lián)

小編給大家分享一下使用CSS和Vanilla.js怎么實(shí)現(xiàn)tooltip提示框,希望大家閱讀完這篇文章后大所收獲,下面讓我們一起去探討吧!

讓客戶(hù)滿(mǎn)意是我們工作的目標(biāo),不斷超越客戶(hù)的期望值來(lái)自于我們對(duì)這個(gè)行業(yè)的熱愛(ài)。我們立志把好的技術(shù)通過(guò)有效、簡(jiǎn)單的方式提供給客戶(hù),將通過(guò)不懈努力成為客戶(hù)在信息化領(lǐng)域值得信任、有價(jià)值的長(zhǎng)期合作伙伴,公司提供的服務(wù)項(xiàng)目有:域名申請(qǐng)、網(wǎng)頁(yè)空間、營(yíng)銷(xiāo)軟件、網(wǎng)站建設(shè)、洮南網(wǎng)站維護(hù)、網(wǎng)站推廣。

效果預(yù)覽

使用CSS和Vanilla.js怎么實(shí)現(xiàn)tooltip提示框

源代碼下載

https://github.com/comehope/front-end-daily-challenges

代碼解讀

定義 dom,容器中包含一個(gè)名為 .emoji 的子容器,代表一個(gè)頭像,它的子元素 eye left、eye right、mouth 分別代表左眼、右眼和嘴巴:

<section class="container">
    <div class="emoji">
        <span class="eye left"></span>
        <span class="eye right"></span>
        <span class="mouth"></span>
    </div>
</section>

居中顯示:

body {
    margin: 0;
    height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: lightyellow;
}

定義容器尺寸和子元素對(duì)齊方式:

.container {
    position: relative;
   width: 20em;
    height: 20em;
    font-size: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
}

定義頭像的輪廓:

.emoji {
    position: relative;
    box-sizing: border-box;
   width: 10em;
    height: 10em;
    background-color: pink;
    border-radius: 50% 50% 75% 50%;
}

定義頭像眼睛的輪廓:

.emoji .eye {
    position: absolute;
    box-sizing: border-box;
   width: 3em;
    height: 3em;
    border: 0.1em solid gray;
    border-radius: 50%;
    top: 3em;
}

.emoji .eye.left {
    left: 1em;
}

.emoji .eye.right {
    right: 1em;
}

畫(huà)出眼珠:

.emoji .eye.left::before,
.emoji .eye.right::before {
    content: '';
    position: absolute;
   width: 1em;
    height: 1em;
    background-color: #222;
    border-radius: 50%;
    top: 1em;
    left: calc((100% - 1em) / 2);
}

畫(huà)出微笑的嘴:

.emoji .mouth {
    position: absolute;
   width: 2em;
    height: 2em;
    border: 0.1em solid;
    bottom: 1em;
    left: 40%;
    border-radius: 50%;
    border-color: transparent gray gray transparent;
    transform: rotate(20deg);
}

接下來(lái)制作眼珠轉(zhuǎn)向 4 個(gè)方向的效果。
用 2 個(gè)變量分別表示眼珠的定位位置:

.emoji .eye {
    --top: 1em;
    --left: calc((100% - 1em) / 2);
}

.emoji .eye.left::before,
.emoji .eye.right::before {
    top: var(--top);
    left: var(--left);
}

設(shè)置眼珠在 4 個(gè)方向的定位位置:

.emoji.top .eye {
    --top: 0;
}

.emoji.bottom .eye {
    --top: 1.8em;
}

.emoji.left .eye {
    --left: 0;
}

.emoji.right .eye {
    --left: 1.8em;
}

此時(shí),如果為 dom 元素 .emoji 增加 top、bottom、left、right 4 個(gè)樣式中的任何一個(gè)樣式,眼珠就會(huì)轉(zhuǎn)向特定的方向。

在 dom 中增加 4 個(gè)元素,每個(gè)元素的內(nèi)容是一個(gè) @ 字符:

<section class="container">
    <div class="emoji">
        <!-- 略 -->
    </div>
    
    <span class="tip top">@</span>
    <span class="tip left">@</span>
    <span class="tip right">@</span>
    <span class="tip bottom">@</span>
</section>

把 4 個(gè)元素布局在頭像周?chē)?/p>

.tip {
    position: absolute;
    cursor: pointer;
    font-size: 4.5em;
    color: silver;
    font-family: sans-serif;
    font-weight: 100;
}

.tip.top {
    top: -15%;
}

.tip.bottom {
    bottom: -15%;
}

.tip.left {
    left: -15%;
}

.tip.right {
    right: -15%;
}

寫(xiě)一段腳本,增加一點(diǎn)交互效果。當(dāng)鼠標(biāo)懸停在 4 個(gè)方向的 @ 上時(shí),使眼珠朝相應(yīng)的方向轉(zhuǎn)去。這里的 DIRECTION 常量存儲(chǔ)了 4 個(gè)方向,EVENTS 常量存儲(chǔ)了 2 個(gè)鼠標(biāo)事件,$ 常量包裝了根據(jù)類(lèi)名獲取 dom 元素的操作:

const DIRECTIONS = ['top', 'bottom', 'left', 'right']
const EVENTS = ['mouseover', 'mouseout']
const $ = (className) => document.getElementsByClassName(className)[0]

DIRECTIONS.forEach(direction => 
    EVENTS.forEach((e) => 
        $(`tip ${direction}`).addEventListener(e, () =>
            $('emoji').classList.toggle(direction)
        )
    )
)

為眼珠設(shè)置緩動(dòng)時(shí)間,使動(dòng)畫(huà)平滑:

.emoji .eye.left::before,
.emoji .eye.right::before {
    transition: 0.3s;
}

接下來(lái)制作 tooltip 提示框。
為 4 個(gè) @ 符號(hào)的 dom 增加 data-tip 屬性,其內(nèi)容就是 tooltip 信息:

<section class="container">
    <div class="emoji">
        <!-- 略 -->
    </div>
    
    <span class="tip top" data-tip="look up">@</span>
    <span class="tip bottom" data-tip="look down">@</span>
    <span class="tip left" data-tip="look to the left">@</span>
    <span class="tip right" data-tip="look to the right">@</span>
</section>

::before 偽元素展示提示信息,樣式為黑底白字:

.tip::before {
    content: attr(data-tip);
    position: absolute;
    font-size: 0.3em;
    font-family: sans-serif;
   width: 10em;
    text-align: center;
    background-color: #222;
    color: white;
    padding: 0.5em;
    border-radius: 0.2em;
    box-shadow: 0 0.1em 0.3em rgba(0, 0, 0, 0.3);
}

把頂部的提示框定位到頂部 @ 符號(hào)的上方正中:

.tip.top::before {
    top: 0;
    left: 50%;
    transform: translate(-50%, calc(-100% - 0.6em));
}

類(lèi)似地,把其他 3 個(gè)提示框也定位到 @ 符號(hào)的旁邊:

.tip.bottom::before {
    bottom: 0;
    left: 50%;
    transform: translate(-50%, calc(100% + 0.6em));
}

.tip.left::before {
    left: 0;
    top: 50%;
    transform: translate(calc(-100% - 0.6em), -50%);
}

.tip.right::before {
    right: 0;
    top: 50%;
    transform: translate(calc(100% + 0.6em), -50%);
}

::after 偽元素在頂部提示框下面畫(huà)出一個(gè)倒三角形:

.tip::after {
    content: '';
    position: absolute;
    font-size: 0.3em;
   width: 0;
    height: 0;
    color: #222;
    border: 0.6em solid transparent;
}

.tip.top::after {
    border-bottom-width: 0;
    border-top-color: currentColor;
    top: -0.6em;
    left: 50%;
    transform: translate(-50%, 0);
}

類(lèi)似地,在其他 3 個(gè)提示框旁邊畫(huà)出三角形:

.tip.bottom::after {
    border-top-width: 0;
    border-bottom-color: currentColor;
    bottom: -0.6em;
    left: 50%;
    transform: translate(-50%, 0);
}

.tip.left::after {
    border-right-width: 0;
    border-left-color: currentColor;
    left: -0.6em;
    top: 50%;
    transform: translate(0, -50%);
}

.tip.right::after {
    border-left-width: 0;
    border-right-color: currentColor;
    right: -0.6em;
    top: 50%;
    transform: translate(0, -50%);
}

最后,隱藏提示框,使提示框只在鼠標(biāo)懸停時(shí)出現(xiàn):

.tip::before,
.tip::after {
    visibility: hidden;
    filter: opacity(0);
    transition: 0.3s;
}

.tip:hover::before,
.tip:hover::after {
    visibility: visible;
    filter: opacity(1);
}

看完了這篇文章,相信你對(duì)使用CSS和Vanilla.js怎么實(shí)現(xiàn)tooltip提示框有了一定的了解,想了解更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線(xiàn),公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性?xún)r(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專(zhuān)為企業(yè)上云打造定制,能夠滿(mǎn)足用戶(hù)豐富、多元化的應(yīng)用場(chǎng)景需求。

分享文章:使用CSS和Vanilla.js怎么實(shí)現(xiàn)tooltip提示框-創(chuàng)新互聯(lián)
文章源于:http://chinadenli.net/article28/ddijjp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營(yíng)銷(xiāo)型網(wǎng)站建設(shè)、Google、網(wǎng)站收錄、網(wǎng)站改版、云服務(wù)器、網(wǎng)站設(shè)計(jì)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

綿陽(yáng)服務(wù)器托管