用css3动漫完成的一个简易酷炫实际效果,最后的实际效果图以下:
网页页面构造(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(); //一刚开始自实行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或是一个实际的值,便会导致下面的图的实际效果,文本和圆挤在一块:
同时我将.pointer-container里边的 background-color: red; 添上便会更为了解为何要把.pointer-container的高宽比设定为190px.此外假如不把transform-origin设定为bottom center它便会如图所示招标注的默认设置点转动,这其实不就是我们要想的实际效果.
也有个关键点便是.shrink的动漫時间我设定变成两秒,实际上依照js里边的breath out这一段時间应当为3s,可是以便从breath out到breath in有一个缓存的实际效果,就设定变成2s,要不然breath out到breath in沒有一个衔接,会看起来生硬不太好看.
到此这篇有关CSS3+JavaScript完成酷炫吸气实际效果的实例编码的文章内容就详细介绍到这了,大量有关CSS3+JavaScript吸气实际效果內容请检索脚本制作之家之前的文章内容或再次访问下边的有关文章内容,期待大伙儿之后多多的适用脚本制作之家!