CSS3+JavaScript實(shí)現(xiàn)炫酷呼吸效果的示例代碼
用css3動(dòng)畫(huà)實(shí)現(xiàn)的一個(gè)簡(jiǎn)單炫酷效果,最終的效果圖如下:

頁(yè)面結(jié)構(gòu)(index.html):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>Relax And Breath</h1>
<div class="container">
<div class="circle"></div>
<p id="text"></p>
<div class="pointer-container">
<div class="pointer"></div>
</div>
<div class="gradient-circle"></div>
</div>
<script src="script.js"></script>
</body>
</html>
script.js:
const container = document.querySelector('.container');
const text = document.querySelector('#text');
const totalTime = 7500;
const breathTime = (totalTime/5)*2; //呼吸的時(shí)間為3s
const holdTime = totalTime/5; //保持呼吸的時(shí)間為1.5s
console.log(breathTime);
breathAnimation(); //一開(kāi)始自執(zhí)行breathAnimation函數(shù)
function breathAnimation(){
text.innerHTML = 'Breath In';
container.className = 'container grow'; //給container添加grow類,實(shí)現(xiàn)放大效果
setTimeout(function(){
text.innerHTML = 'Hold On';
setTimeout(function(){
text.innerHTML = 'Breath Out';
container.className = 'container shrink';//給container添加shrink類,實(shí)現(xiàn)縮小效果
},holdTime)
},breathTime)
}
setInterval(breathAnimation,totalTime); //重復(fù)執(zhí)行
樣式(style.css):
*{
margin: 0;
padding: 0;
box-sizing: border-box;
}
body{
background: url('./img/bg.jpg') no-repeat center center /cover;
min-height: 100vh;
font-family: Arial, Helvetica, sans-serif;
color: #fff;
overflow: hidden;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
/*注意設(shè)置margin為auto*/
.container{
position: relative;
width: 300px;
height: 300px;
display: flex;
align-items: center;
justify-content: center;
transform: scale(1);
margin: auto;
}
/*使用圓錐漸變作為背景,寬高比.container稍大,同時(shí)z-index要設(shè)為-2,因?yàn)檫€有一層為.circle,最外層是文字*/
.gradient-circle{
position: absolute;
left: -10px;
top: -10px;
background: conic-gradient(
#55b7a4 0%,
#4ca493 40%,
#fff 40%,
#fff 60%,
#336d62 60%,
#2a5b52 100%
);
width: 320px;
height: 320px;
border-radius: 50%;
z-index: -2;
}
/z-index為-1,為中間黑色的圓/
.circle{
position: absolute;
left: 0;
top: 0;
width: 300px;
height: 300px;
background-color: #010f1c;
border-radius: 50%;
z-index: -1;
}
/*.pointer-container是小球外面的容器,其高設(shè)置為190,是因?yàn)槠渲?50為半徑,還有40為top-40,這樣就會(huì)繞著圓心轉(zhuǎn),且不會(huì)換到里面來(lái),注意transform-origin為中下方*/
.pointer-container{
position: absolute;
width: 20px;
height: 190px;
top: -40px;
left: 140px;
/* background-color: red; */
transform-origin: bottom center;
animation: rotate 7.5s linear forwards infinite;
}
/*小球*/
.pointer{
width: 20px;
height: 20px;
background-color: #fff;
border-radius: 50%;
}
/*設(shè)置小球轉(zhuǎn)圈的效果*/
@keyframes rotate{
from{
transform: rotate(0deg);
}to{
transform: rotate(360deg);
}
}
.container.grow{
animation: grow 3s linear forwards;
}
.container.shrink{
animation: shrink 2s linear forwards;
}
@keyframes grow{
from{
transform: scale(1)
}to{
transform: scale(1.2);
}
}
@keyframes shrink{
from{
transform: scale(1.2)
}to{
transform: scale(1);
}
}
如果.container的margin不設(shè)置為auto或者一個(gè)具體的值,就會(huì)造成下圖的效果,文字和圓擠在一塊:

同時(shí)我把.pointer-container里面的 background-color: red; 添上就會(huì)更加理解為什么要把.pointer-container的高度設(shè)置為190px.另外如果不把transform-origin設(shè)置為bottom center它就會(huì)如圖中標(biāo)注的默認(rèn)點(diǎn)旋轉(zhuǎn),這并不是我們想要的效果.

還有個(gè)細(xì)節(jié)就是.shrink的動(dòng)畫(huà)時(shí)間我設(shè)置成了兩秒,其實(shí)按照js里面的breath out這段時(shí)間應(yīng)該為3s,但是為了從breath out到breath in有個(gè)緩沖的效果,就設(shè)置成了2s,不然breath out到breath in沒(méi)有一個(gè)過(guò)渡,會(huì)顯得突兀不好看.
到此這篇關(guān)于CSS3+JavaScript實(shí)現(xiàn)炫酷呼吸效果的示例代碼的文章就介紹到這了,更多相關(guān)CSS3+JavaScript呼吸效果內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持腳本之家!
相關(guān)文章
使用CSS媒體查詢(Media Queries)和JavaScript判斷瀏覽器設(shè)備類型的方法
有無(wú)數(shù)的理由要求我們?cè)谌魏螘r(shí)候都應(yīng)該知道用戶是使用的什么設(shè)備瀏覽我們的網(wǎng)站——寬屏,普通屏,平板,手機(jī)?知道這些特征,我們web應(yīng)用的CSS和JavaScript才能同步做相應(yīng)2014-04-03使用html5+css3來(lái)實(shí)現(xiàn)slider切換效果告別javascript+css
提到slider,過(guò)去一直都是用css+js來(lái)配合實(shí)現(xiàn)相關(guān)的切換效果。聽(tīng)過(guò)大家一直討論使用html5+css3的實(shí)現(xiàn)方式,自己卻一直沒(méi)有動(dòng)手實(shí)現(xiàn)過(guò)。好吧,這次我有時(shí)間來(lái)玩下css3了,感2013-01-08

