使用CSS和Vanilla.js實現(xiàn)展示蘋果設(shè)備的交互動畫的方法-創(chuàng)新互聯(lián)

這篇文章給大家分享的是有關(guān)使用CSS和Vanilla.js實現(xiàn)展示蘋果設(shè)備的交互動畫的方法的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考。一起跟隨小編過來看看吧。

創(chuàng)新互聯(lián)公司長期為1000+客戶提供的網(wǎng)站建設(shè)服務(wù),團隊從業(yè)經(jīng)驗10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為臨西企業(yè)提供專業(yè)的成都網(wǎng)站設(shè)計、網(wǎng)站制作,臨西網(wǎng)站改版等技術(shù)服務(wù)。擁有十余年豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。效果預(yù)覽

使用CSS和Vanilla.js實現(xiàn)展示蘋果設(shè)備的交互動畫的方法

源代碼下載

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

代碼解讀

定義 dom,包含 5 個子元素,分別代表 iphone, mini, ipad, macbook, imac 這 5 種設(shè)備:

<div class="container">
    <div class="device iphone"></div>
    <div class="device mini"></div>
    <div class="device ipad"></div>
    <div class="device macbook"></div>
    <div class="device imac"></div>
</div>

居中顯示:

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

設(shè)置容器中子元素的布局方式:

.container {
    position: relative;
    display: flex;
    flex-direction: column;
    align-items: center;
}

設(shè)置設(shè)備的共有屬性,線性漸變圖案將作為屏幕的背景:

.device {
    box-sizing: border-box;
    position: relative;
    display: flex;
    justify-content: center;
    background: linear-gradient(120deg, #ddd 30%, #ccc 30%);
}

.device::before,
.device::after {
    content: '';
    position: absolute;
}

iphone, mini, ipad 的造型相似,都有頂部攝像頭、傳感器開口和底部按鈕,所以這些共有屬性可以一起設(shè)置,用 ::before 偽元素畫出頂部細節(jié),::after 偽元素畫出底部按鈕:

.iphone::before,
.mini::before,
.ipad::before {
    width: 2px;
    height: 2px;
    border-style: solid;
    border-color: #a5adbe;
    border-width: 0 12px 0 2px;
}

.iphone::after,
.mini::after,
.ipad::after {
    width: 8px;
    height: 8px;
    background-color: white;
    border-radius: 50%;
}

接下來逐個畫出設(shè)備。先畫出 iphone 的輪廓:

.iphone {
    width: 59px;
    height: 124px;
    border: #484f5e solid;
    border-width: 18px 4px;
    border-radius: 6px;
}

定位 iphone 的頂部和底部細節(jié):

.iphone::before {
    top: -10px;
}

.iphone::after {
    bottom: -13px;
}

類似地,畫出 mini:

.mini {
    width: 93px;
    height: 138px;
    border: #484f5e solid;
    border-width: 14px 5px;
    border-radius: 10px;
}

.mini::before {
    top: -8px;
}

.mini::after {
    bottom: -11px;
}

再畫出 ipad:

.ipad {
    width: 134px;
    height: 176px;
    border: #484f5e solid;
    border-width: 18px 13px;
    border-radius: 12px;
}

.ipad::before {
    top: -10px;
}

.ipad::after {
    bottom: -13px;
}

接下來畫 macbook,先畫屏幕:

.macbook {
    width: 234px;
    height: 155px;
    border: 8px solid #484f5e;
    border-radius: 7px 7px 0 0;
}

::before偽元素畫出攝像頭:

.macbook::before {
    width: 294px;
    height: 14px;
    background-color: #e8ebf0;
    top: calc(100% + 8px);
    border-radius: 0 0 14px 14px;
}

用 ::after 偽元素畫出主機:

.macbook::after {
    width: 3px;
    height: 3px;
    background-color: #a5adbe;
    top: -6px;
    border-radius: 50%;
}

接下來畫 imac,先畫屏幕,屏幕的左、上、右的黑色邊框沒有用 border 屬性畫,是因為 border 會在端點處遺留一個斜角,所以改用 box-shadow 實現(xiàn):

.imac {
    width: 360px;
    height: 215px;
    border-radius: 10px;
    box-shadow: 
        inset 0 14px #484f5e,
        inset 14px 0 #484f5e,
        inset -14px 0 #484f5e;
    border-bottom: 33px solid #e8ebf1;
    transform: translateY(14px);
}

用 ::before 偽元素畫出梯形的底座:

.imac::before {
    width: 90px;
    height: 0;
    top: calc(100% + 33px);
    border: solid transparent;
    border-bottom-color: #e2e4e8;
    border-width: 0 10px 47px 10px;
}

用 ::after 偽元素畫出頂部的攝像頭和屏幕底部的按鈕,注意按鈕是用 box-shadow 實現(xiàn)的:

.imac::after {
    width: 4px;
    height: 4px;
    background-color: #a5adbe;
    top: 5px;
    border-radius: 50%;
    box-shadow: 0 191px 0 4px #464e5d;
}

至此,設(shè)備全部繪制完成。
刪除除 iphone 之外的其他設(shè)備的 dom 元素,只保留 1 個 dom 元素,后面的動畫效果都在這個 dom 元素上變化:

<div class="container">
        <div class="device iphone"></div>
        <!-- <div class="device mini"></div>
        <div class="device ipad"></div>
        <div class="device macbook"></div>
        <div class="device imac"></div> -->
    </div>

設(shè)置容器尺寸,子元素垂直居中,設(shè)備的高度占容器高度的 75%:

.container {
    width: 360px;
    height: 350px;
    justify-content: center;
}

.device {
    transform: translateY(-25%);
}

在 dom 中增加 2 個按鈕元素,分別用 .left 和 .right 表示:

<div class="container">
    <div class="device iphone"></div>
    <div class="buttons">
        <span class="left"></span>
        <span class="right"></span>
    </div>
</div>

定位按鈕的位置:

.buttons {
    position: absolute;
    width: inherit;
    font-size: 30px;
    height: 2em;
    bottom: 0;
    display: flex;
    justify-content: space-around;
}

.buttons > * {
    position: relative;
    width: 4em;
}

按鈕為向左和向右的箭頭:

.buttons > *::before {
    position: absolute;
}

.buttons .left::before {
    content: '←';
    right: 0;
}

.buttons .right::before {
    content: '→';
}

設(shè)置按鈕樣式為圓形:

.buttons > *t::before {
    position: absolute;
    width: 2em;
    height: 2em;
    background-color: #484f5e;
    color: silver;
    text-align: center;
    line-height: 2em;
    border-radius: 1em;
    cursor: pointer;
}

增加鼠標懸停效果:

.buttons > *::before {
    transition: 0.2s;
}

.buttons .left:hover::before {
    width: 4em;
    content: '?';
}

.buttons .right:hover::before {
    width: 4em;
    content: '?';
}

增加按鈕點擊效果:

.buttons > *:active {
    transform: scale(0.9);
    filter: brightness(0.8);
}

至此,按鈕制作完畢,接下來創(chuàng)建交互腳本。

定義一個獲取元素的函數(shù)$

const $ = (className) => document.getElementsByClassName(className)[0]

定義一個存放設(shè)備名稱的數(shù)組:

let devices = ['iphone', 'mini', 'ipad', 'macbook', 'imac']

定義點擊行為對數(shù)據(jù)的加工方法,當點擊左側(cè)按鈕時,把數(shù)組最左邊的 1 個元素移到最右邊,相反地,當點擊右側(cè)按鈕時,把數(shù)組最右邊的 1 個元素移到最左邊,這樣就可以從 2 個方向循環(huán)遍歷數(shù)組了:

let loop = {
    'left': () => devices.unshift(devices.pop()),
    'right': () => devices.push(devices.shift())
}

定義點擊事件,根據(jù)數(shù)組的變化切換設(shè)備:

Array.from($('buttons').children).forEach(element =>
    element.addEventListener('click', function(e) {
        loop[e.target.className]()
        $('device').className = 'device ' + devices[0]
    })
)

最后,設(shè)置設(shè)備切換的緩動效果:

.device,
.device::before,
.device::after {
    transition: 0.4s cubic-bezier(0.5, 1.7, 0.5, 1.2);
}

感謝各位的閱讀!關(guān)于使用CSS和Vanilla.js實現(xiàn)展示蘋果設(shè)備的交互動畫的方法就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

網(wǎng)頁名稱:使用CSS和Vanilla.js實現(xiàn)展示蘋果設(shè)備的交互動畫的方法-創(chuàng)新互聯(lián)
轉(zhuǎn)載注明:http://bm7419.com/article24/ceceje.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站、網(wǎng)站維護、品牌網(wǎng)站制作、網(wǎng)頁設(shè)計公司、靜態(tài)網(wǎng)站、營銷型網(wǎng)站建設(shè)

廣告

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

小程序開發(fā)