|
導(dǎo)讀網(wǎng)頁(yè)的本質(zhì)就是超級(jí)文本標(biāo)記語(yǔ)言,通過(guò)結(jié)合使用其他的Web技術(shù)(如:腳本語(yǔ)言、公共網(wǎng)關(guān)接口、組件等),可以創(chuàng)造出功能強(qiáng)大的網(wǎng)頁(yè)。因而,超級(jí)文本標(biāo)記語(yǔ)言是萬(wàn)維網(wǎng)(Web)編程的基礎(chǔ),也就是說(shuō)萬(wàn)維網(wǎng)是建立... 網(wǎng)頁(yè)的本質(zhì)就是超級(jí)文本標(biāo)記語(yǔ)言,通過(guò)結(jié)合使用其他的Web技術(shù)(如:腳本語(yǔ)言、公共網(wǎng)關(guān)接口、組件等),可以創(chuàng)造出功能強(qiáng)大的網(wǎng)頁(yè)。因而,超級(jí)文本標(biāo)記語(yǔ)言是萬(wàn)維網(wǎng)(Web)編程的基礎(chǔ),也就是說(shuō)萬(wàn)維網(wǎng)是建立在超文本基礎(chǔ)之上的。超級(jí)文本標(biāo)記語(yǔ)言之所以稱為超文本標(biāo)記語(yǔ)言,是因?yàn)槲谋局邪怂^“超級(jí)鏈接”點(diǎn)。 本篇文章給大家?guī)?lái)的內(nèi)容是關(guān)于如何使用CSS和D3實(shí)現(xiàn)一個(gè)舞動(dòng)的畫面(附源碼),有一定的參考價(jià)值,有需要的朋友可以參考一下,希望對(duì)你有所幫助。效果預(yù)覽
源代碼下載https://github.com/comehope/front-end-daily-challenges 代碼解讀定義 dom,容器中包含 1 個(gè) <figure class="container">
<div class="square">
<span></span>
<span></span>
<span></span>
<span></span>
</div>
</figure>居中顯示: body {
margin: 0;
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
background-color: #222;
}設(shè)置容器的尺寸單位, .container {
font-size: 8px;
}子容器中的 4 個(gè) .square span {
display: block;
border: 2.5em solid transparent;
color: #ddd;
}
.square span:nth-child(1),
.square span:nth-child(4) {
border-left-color: currentColor;
border-right-color: currentColor;
}
.square span:nth-child(2),
.square span:nth-child(3) {
border-top-color: currentColor;
border-bottom-color: currentColor;
}把邊框改為圓弧: .square span {
border-radius: 50%;
}在子容器中用 grid 布局把 4 個(gè) .square {
display: grid;
grid-template-columns: repeat(2, 1fr);
grid-gap: 0.2em;
padding: 0.1em;
}旋轉(zhuǎn) 4 個(gè) .square span {
transform: rotate(calc(45deg + 90deg * 0));
}增加讓 .square span {
animation: rotation 2s ease-in-out infinite;
}
@keyframes rotation {
0% { transform: rotate(calc(45deg + 90deg * 0)); }
25% { transform: rotate(calc(45deg + 90deg * 1)); }
50% { transform: rotate(calc(45deg + 90deg * 2)); }
75% { transform: rotate(calc(45deg + 90deg * 3)); }
100% { transform: rotate(calc(45deg + 90deg * 4)); }
}使其中 2 個(gè) .square span:nth-child(2),
.square span:nth-child(3) {
animation-direction: reverse;
}至此,一個(gè) <figure class="container">
<div class="square">
<span></span>
<span></span>
<span></span>
<span></span>
</div>
<div class="square">
<span></span>
<span></span>
<span></span>
<span></span>
</div>
<div class="square">
<span></span>
<span></span>
<span></span>
<span></span>
</div>
<div class="square">
<span></span>
<span></span>
<span></span>
<span></span>
</div>
</figure>用 grid 布局把 4 個(gè) .container {
display: grid;
--columns: 2;
grid-template-columns: repeat(var(--columns), 1fr);
}現(xiàn)在看起來(lái)好像是有幾個(gè)黑色的小方塊在不停地移動(dòng),當(dāng) dom 元素越多時(shí),動(dòng)畫效果看起來(lái)就越壯觀,就像集體舞一樣,人越多越有氣勢(shì)。接下來(lái)用 d3 批量增加 dom 的元素。 <script src="https://d3js.org/d3.v5.min.js"></script> 聲明一個(gè) const COLUMNS = 2; 刪除掉 html 文件中的 d3.select('.container')
.selectAll('p')
.data(d3.range(COLUMNS * COLUMNS))
.enter()
.append('p')
.attr('class', 'square');繼續(xù)用連綴語(yǔ)法增加 d3.select('.container')
.selectAll('p')
.data(d3.range(COLUMNS * COLUMNS))
.enter()
.append('p')
.attr('class', 'square')
.selectAll('span')
.data(d3.range(4))
.enter()
.append('span');刪除掉 css 文件中的 d3.select('.container')
.style('--columns', COLUMNS)
/*略*/最后,把邊長(zhǎng)改為 4,即讓 16 個(gè) const COLUMNS = 4; 大功告成! 以上就是如何使用CSS和D3實(shí)現(xiàn)一個(gè)舞動(dòng)的畫面(附源碼)的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注php中文網(wǎng)其它相關(guān)文章! 網(wǎng)站建設(shè)是一個(gè)廣義的術(shù)語(yǔ),涵蓋了許多不同的技能和學(xué)科中所使用的生產(chǎn)和維護(hù)的網(wǎng)站。 |
溫馨提示:喜歡本站的話,請(qǐng)收藏一下本站!