CSS3+JavaScript實現炫酷呼吸效果

用css3動畫實現的一個簡單炫酷效果,最終的效果圖以下:javascript

頁面結構(index.html):css

<!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:html

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();    //一開始自執行breathAnimation函數
function breathAnimation(){
  text.innerHTML = 'Breath In';
  container.className = 'container grow';    //給container添加grow類,實現放大效果

  setTimeout(function(){
    text.innerHTML = 'Hold On';
    setTimeout(function(){
      text.innerHTML = 'Breath Out';
      container.className = 'container shrink';//給container添加shrink類,實現縮小效果
    },holdTime)
  },breathTime)
}

setInterval(breathAnimation,totalTime);    //重複執行

樣式(style.css):java

*{
  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;
}
/*注意設置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要設爲-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是小球外面的容器,其高設置爲190,是由於其中150爲半徑,還有40爲top-40,這樣就會繞着圓心轉,且不會換到裏面來,注意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%;
}

/*設置小球轉圈的效果*/
@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不設置爲auto或者一個具體的值,就會形成下圖的效果,文字和圓擠在一塊:css3

同時我把.pointer-container裏面的 background-color: red; 添上就會更加理解爲何要把.pointer-container的高度設置爲190px.另外若是不把transform-origin設置爲bottom center它就會如圖中標註的默認點旋轉,這並非咱們想要的效果.css3動畫

還有個細節就是.shrink的動畫時間我設置成了兩秒,其實按照js裏面的breath out這段時間應該爲3s,可是爲了從breath out到breath in有個緩衝的效果,就設置成了2s,否則breath out到breath in沒有一個過渡,會顯得突兀很差看.函數

好啦,結束咯.下次再更新.記錄學習成長過程,fighting!學習

相關文章
相關標籤/搜索