溫馨提示×

溫馨提示×

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

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

CSS3+JavaScript如何實現炫酷呼吸效果

發布時間:2021-03-19 09:23:58 來源:億速云 閱讀:570 作者:小新 欄目:web開發

這篇文章給大家分享的是有關CSS3+JavaScript如何實現炫酷呼吸效果的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

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

CSS3+JavaScript如何實現炫酷呼吸效果

頁面結構(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>
  <h2>Relax And Breath</h2>
  <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();    //一開始自執行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):

*{
  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+JavaScript如何實現炫酷呼吸效果

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

CSS3+JavaScript如何實現炫酷呼吸效果

還有個細節就是.shrink的動畫時間我設置成了兩秒,其實按照js里面的breath out這段時間應該為3s,但是為了從breath out到breath in有個緩沖的效果,就設置成了2s,不然breath out到breath in沒有一個過渡,會顯得突兀不好看.

感謝各位的閱讀!關于“CSS3+JavaScript如何實現炫酷呼吸效果”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

向AI問一下細節

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

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女