CSS3+JavaScript實現(xiàn)炫酷呼吸效果的示例代碼
用css3動畫實現(xiàn)的一個簡單炫酷效果,最終的效果圖如下:

頁面結(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; //呼吸的時間為3s
const holdTime = totalTime/5; //保持呼吸的時間為1.5s
console.log(breathTime);
breathAnimation(); //一開始自執(zhí)行breathAnimation函數(shù)
function breathAnimation(){
text.innerHTML = 'Breath In';
container.className = 'container grow'; //給container添加grow類,實現(xiàn)放大效果
setTimeout(function(){
text.innerHTML = 'Hold On';
setTimeout(function(){
text.innerHTML = 'Breath Out';
container.className = 'container shrink';//給container添加shrink類,實現(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稍大,同時z-index要設(shè)為-2,因為還有一層為.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,是因為其中150為半徑,還有40為top-40,這樣就會繞著圓心轉(zhuǎn),且不會換到里面來,注意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或者一個具體的值,就會造成下圖的效果,文字和圓擠在一塊:

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

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

