溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

怎么用HTML5+CSS3動態(tài)畫一個笑臉

發(fā)布時間:2021-08-31 12:49:35 來源:億速云 閱讀:207 作者:chen 欄目:web開發(fā)

本篇內(nèi)容介紹了“怎么用HTML5+CSS3動態(tài)畫一個笑臉”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠?qū)W有所成!

今天本文的主要內(nèi)容是:利用HTML5 svg繪制出一個線條笑臉,然后使用CSS3給它添加動畫效果,讓它可以慢慢被畫出來。光說可能大家還不明白是什么效果,我們直接來看看效果圖:

怎么用HTML5+CSS3動態(tài)畫一個笑臉

下面我們來研究一下是怎么實現(xiàn)這個效果的:

首先設置整個頁面的背景顏色、svg畫布的大小、線條的顏色、

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

svg {
  display: block;
  height: 90vmin;
  width: 90vmin;
}

.stroke {
  stroke-width: 1;
  stroke: #fff;
  fill: none;
}

然后利用svg繪制出一個線條笑臉

  • 定義svg標簽,在當前文檔內(nèi)嵌套一個獨立的svg片段

<svg viewBox="-50 -50 100 100">

</svg>
  • 定義一個path標簽,畫一個圓

<svg viewBox="-50 -50 100 100">
  <path class="stroke" d="M-40 0 a 40 40 0 0 1 80 0 a 40 40 0 0 1 -80 0"></path>
</svg>

怎么用HTML5+CSS3動態(tài)畫一個笑臉

  • 在使用path標簽畫左邊的眼睛

<svg viewBox="-50 -50 100 100">
  <path class="stroke" d="M-40 0 a 40 40 0 0 1 80 0 a 40 40 0 0 1 -80 0"></path>
  <path class="stroke" d="M-20 -20 a 5 5 0 0 1 10 0 a 5 5 0 0 1 -10 0"></path>
</svg>

怎么用HTML5+CSS3動態(tài)畫一個笑臉

  • 將右邊的眼睛也畫出來

<svg viewBox="-50 -50 100 100">
  <path class="stroke" d="M-40 0 a 40 40 0 0 1 80 0 a 40 40 0 0 1 -80 0"></path>
  <path class="stroke" d="M-20 -20 a 5 5 0 0 1 10 0 a 5 5 0 0 1 -10 0"></path>
  <path class="stroke" d="M10 -20 a 5 5 0 0 1 10 0 a 5 5 0 0 1 -10 0"></path>
</svg>

怎么用HTML5+CSS3動態(tài)畫一個笑臉

  • 將嘴巴畫出來

<svg viewBox="-50 -50 100 100">
  <path class="stroke" d="M-40 0 a 40 40 0 0 1 80 0 a 40 40 0 0 1 -80 0"></path>
  <path class="stroke" d="M-20 -20 a 5 5 0 0 1 10 0 a 5 5 0 0 1 -10 0"></path>
  <path class="stroke" d="M10 -20 a 5 5 0 0 1 10 0 a 5 5 0 0 1 -10 0"></path>
  <path class="stroke" d="M30 0 a 30 30 0 1 1 -60 0"></path>
</svg>

怎么用HTML5+CSS3動態(tài)畫一個笑臉

給.stroke元素添加一個stroke-linecaps屬性,將嘴巴路徑兩端的形狀設置為圓弧。

.stroke {
  stroke-linecap: round;
}

怎么用HTML5+CSS3動態(tài)畫一個笑臉

OK,笑臉畫出來了!最后實現(xiàn)動畫效果:

給.stroke元素綁定一個動畫,然后設置stroke-dasharray和stroke-dashoffset屬性,這樣笑臉圖案會被先隱藏起來

.stroke {
  animation: stroke-anim 2s linear forwards;  
  stroke-dasharray: 300;
  stroke-dashoffset: 300;
}

使用@keyframes規(guī)則,給動畫設置動作,將stroke-dashoffsets屬性的值設置為0,這樣笑臉圖案就能慢慢顯示出來

@keyframes stroke-anim {
  to {
	stroke-dashoffset: 0;
  }
}

怎么用HTML5+CSS3動態(tài)畫一個笑臉

動畫效果雖然出來了,但不是我們想要的。我們需要使用animation-delay定義每一步動作的開始時間,先畫出臉,再畫左眼和右眼,最后畫出嘴巴:

.stroke:nth-child(2) {
  animation-delay: 2s;
}
.stroke:nth-child(3) {
  animation-delay: 3s;
}

.stroke:nth-child(4) {
  animation-delay: 4s;
}

@keyframes stroke-anim {
  to {
	stroke-dashoffset: 0;
  }
}

怎么用HTML5+CSS3動態(tài)畫一個笑臉

ok,完成!下面給出完整代碼:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<style>
			body {
				background: #222;
				display: flex;
				height: 100vh;
				justify-content: center;
				align-items: center;
				margin: 0;
			}

			svg {
				display: block;
				height: 90vmin;
				width: 90vmin;
			}

			.stroke {
				stroke-width: 1;
				stroke: #fff;
				fill: none;
				stroke-linecap: round;
				animation: stroke-anim 2s linear forwards;
				stroke-dasharray: 300;
				stroke-dashoffset: 300;
			}

			.stroke:nth-child(2) {
				animation-delay: 2s;
			}


			.stroke:nth-child(3) {
				animation-delay: 3s;
			}


			.stroke:nth-child(4) {
				animation-delay: 4s;
			}


			@keyframes stroke-anim {
				to {
					stroke-dashoffset: 0;
				}
			}
		</style>
	</head>
	<body>
		<svg viewBox="-50 -50 100 100">
			<path class="stroke" d="M-40 0 a 40 40 0 0 1 80 0 a 40 40 0 0 1 -80 0"></path>
			<path class="stroke" d="M-20 -20 a 5 5 0 0 1 10 0 a 5 5 0 0 1 -10 0"></path>
			<path class="stroke" d="M10 -20 a 5 5 0 0 1 10 0 a 5 5 0 0 1 -10 0"></path>
			<path class="stroke" d="M30 0 a 30 30 0 1 1 -60 0"></path>
		</svg>
	</body>
</html>

大家可以直接復制以上代碼,在本地進行運行演示。

這里給大家介紹幾個關(guān)鍵的標簽和屬性:

  • <svg> 元素

SVG 圖像是使用各種元素創(chuàng)建的,這些元素分別應用于矢量圖像的結(jié)構(gòu)、繪制與布局。如果svg不是根元素,svg 元素可以用于在當前文檔(比如說,一個HTML文檔)內(nèi)嵌套一個獨立的svg片段 。 這個獨立片段擁有獨立的視口和坐標系統(tǒng)。

  • <path> 路徑

path元素是用來定義形狀的通用元素。所有的基本形狀都可以用path元素來創(chuàng)建。SVG <path>元素用于繪制由直線,圓弧,曲線等組合而成的高級形狀,帶或不帶填充。該 <path>元素可能是所有元素中最先進,最通用的SVG形狀。它可能也是最難掌握的元素。

  • animation 屬性和@keyframes 規(guī)則

/* 定義動畫*/
@keyframes 動畫名稱{
    /* 樣式規(guī)則*/
}

/* 將它應用于元素 */
.element {
    animation-name: 動畫名稱(在@keyframes中已經(jīng)聲明好的);

    /* 或使用動畫簡寫屬性*/
    animation: 動畫名稱 1s ...
}

animation 屬性是一個簡寫屬性,可用于設置六個動畫屬性:

animation-name:規(guī)定需要綁定到選擇器的 keyframe 名稱。。   
animation-duration:規(guī)定完成動畫所花費的時間,以秒或毫秒計。   
animation-timing-function:規(guī)定動畫的速度曲線。   
animation-delay:規(guī)定在動畫開始之前的延遲。   
animation-iteration-count:規(guī)定動畫應該播放的次數(shù)。   
animation-direction:規(guī)定是否應該輪流反向播放動畫。
  • animation-delay 屬性定義動畫何時開始。

    該屬性值以秒或毫秒計;允許負值,-2s 使動畫馬上開始,但跳過 2 秒進入動畫。

  • :nth-child()選擇器

    :nth-child(n) 選擇器匹配父元素中的第 n 個子元素,元素類型沒有限制。

    n 可以是一個數(shù)字,一個關(guān)鍵字,或者一個公式。

“怎么用HTML5+CSS3動態(tài)畫一個笑臉”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI