CSS3+JavaScript实现炫酷呼吸效果的示例代码


Posted in HTML / CSS onJune 15, 2020

用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>
  <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或者一个具体的值,就会造成下图的效果,文字和圆挤在一块:

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实现炫酷呼吸效果的示例代码的文章就介绍到这了,更多相关CSS3+JavaScript呼吸效果内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章,希望大家以后多多支持三水点靠木!

HTML / CSS 相关文章推荐
css动画效果之animation的常用样式
Mar 09 HTML / CSS
利用css3-animation实现逐帧动画效果
Mar 10 HTML / CSS
详解CSS3弹性伸缩盒
Sep 21 HTML / CSS
canvas基础之图形验证码的示例
Jan 02 HTML / CSS
实例教程 HTML5 Canvas 超炫酷烟花绽放动画实现代码
Nov 05 HTML / CSS
html5使用canvas画一条线
Dec 15 HTML / CSS
使用分层画布来优化HTML5渲染的教程
May 08 HTML / CSS
用HTML5 实现橡皮擦的涂抹效果的教程
May 11 HTML / CSS
用canvas做一个DVD待机动画的实现代码
Apr 12 HTML / CSS
html5的pushstate以及监听浏览器返回事件的实现
Aug 11 HTML / CSS
wordpress添加Html5的表单验证required方法小结
Aug 18 HTML / CSS
如何通过 CSS 写出火焰效果
Mar 24 HTML / CSS
CSS3中引入多种自定义字体font-face
Jun 12 #HTML / CSS
详解CSS3实现响应式手风琴效果
Jun 10 #HTML / CSS
CSS3实现淘宝留白的方法
Jun 05 #HTML / CSS
CSS3实现歌词进度文字颜色填充变化动态效果的思路详解
Jun 02 #HTML / CSS
CSS 3.0 结合video视频实现的创意开幕效果
Jun 01 #HTML / CSS
CSS3自定义滚动条样式 ::webkit-scrollbar的示例代码详解
Jun 01 #HTML / CSS
css3实现背景模糊的三种方式(小结)
May 15 #HTML / CSS
You might like
php5编程中的异常处理详细方法介绍
2008/07/29 PHP
easyui的tabs update正确用法分享
2014/03/21 PHP
使用YUI+Ant 实现JS CSS压缩
2014/09/02 PHP
PHP实现的线索二叉树及二叉树遍历方法详解
2016/04/25 PHP
php进程daemon化的正确实现方法
2018/09/06 PHP
javascript中创建对象的三种常用方法
2010/12/30 Javascript
Prototype源码浅析 String部分(二)
2012/01/16 Javascript
js 使FORM表单的所有元素不可编辑的示例代码
2013/10/17 Javascript
jquery监听div内容的变化具体实现思路
2013/11/04 Javascript
Javascript遍历table中的元素示例代码
2014/07/08 Javascript
JavaScript中实现无缝滚动、分享到侧边栏实例代码
2016/04/06 Javascript
BootStrap中
2016/12/10 Javascript
Angular如何引入第三方库的方法详解
2017/07/13 Javascript
webpack构建react多页面应用详解
2017/09/15 Javascript
解决JQuery全选/反选第二次失效的问题
2017/10/11 jQuery
angularJs 表格添加删除修改查询方法
2018/02/27 Javascript
Vue2.0 实现移动端图片上传功能
2018/05/30 Javascript
vue+Element实现搜索关键字高亮功能
2019/05/28 Javascript
vue elementui tree 任意级别拖拽功能代码
2020/08/31 Javascript
[02:43]DOTA2英雄基础教程 德鲁伊
2014/01/13 DOTA
TensorFlow入门使用 tf.train.Saver()保存模型
2018/04/24 Python
Python使用pydub库对mp3与wav格式进行互转的方法
2019/01/10 Python
解决Python列表字符不区分大小写的问题
2019/12/19 Python
torchxrayvision包安装过程(附pytorch1.6cpu版安装)
2020/08/26 Python
CSS3贝塞尔曲线示例:创建链接悬停动画效果
2020/11/19 HTML / CSS
单身联谊活动方案
2014/01/29 职场文书
大三学生做职业规划:给未来找个方向
2014/02/24 职场文书
销售人员求职的自我评价分享
2014/03/15 职场文书
经济信息系毕业生自荐信
2014/06/02 职场文书
电子商务专业毕业生求职信
2014/06/12 职场文书
公安机关正风肃纪剖析材料
2014/10/10 职场文书
工会2014法制宣传日活动总结
2014/11/01 职场文书
普通员工辞职信范文
2015/05/12 职场文书
2016年企业先进员工事迹材料
2016/02/25 职场文书
2016年暑期社会实践活动总结报告
2016/04/06 职场文书
python在package下继续嵌套一个package
2022/04/14 Python