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 相关文章推荐
利用CSS3的flexbox实现水平垂直居中与三列等高布局
Sep 12 HTML / CSS
50个强大璀璨的CSS3/JS技术运用实例
Feb 27 HTML / CSS
css3实现文字扫光渐变动画效果的示例
Nov 07 HTML / CSS
CSS3 边框效果
Nov 04 HTML / CSS
突破canvas语法限制 让他支持链式语法
Dec 24 HTML / CSS
使用HTML5里的classList操作CSS类
Jun 28 HTML / CSS
canvas绘制视频封面的方法
Feb 05 HTML / CSS
canvas实现扭蛋机动画效果的示例代码
Oct 17 HTML / CSS
uniapp+Html5端实现PC端适配
Jul 15 HTML / CSS
html5移动端禁止长按图片保存的实现
Apr 20 HTML / CSS
面试必问:圣杯布局和双飞翼布局的区别
May 13 HTML / CSS
CSS Transition通过改变Height实现展开收起元素
Aug 07 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
基于PHP遍历数组的方法汇总分析
2013/06/08 PHP
php结合正则批量抓取网页中邮箱地址
2015/05/19 PHP
Yii2框架中一些折磨人的坑
2019/12/15 PHP
PHP 图片处理
2020/09/16 PHP
静态图片的十一种滤镜效果--不支持Ie7及非IE浏览器。
2007/03/06 Javascript
JavaScript获取GridView选择的行内容
2009/04/14 Javascript
javascript 函数调用规则
2009/08/26 Javascript
JavaScript访问样式表代码
2010/10/15 Javascript
Extjs gridpanel 出现横向滚动条问题的解决方法
2011/07/04 Javascript
js操作iframe的一些方法介绍
2013/06/25 Javascript
JS实现随机化快速排序的实例代码
2013/08/01 Javascript
浅谈DOM的操作以及性能优化问题-重绘重排
2017/01/08 Javascript
JavaScript简介_动力节点Java学院整理
2017/06/26 Javascript
vue loadmore 组件滑动加载更多源码解析
2017/07/19 Javascript
JavaScript定义及输出螺旋矩阵的方法详解
2017/12/01 Javascript
vue better-scroll插件使用详解
2018/01/25 Javascript
vue引入js数字小键盘的实现代码
2018/05/14 Javascript
使用Python保存网页上的图片或者保存页面为截图
2016/03/05 Python
Python实现二叉树结构与进行二叉树遍历的方法详解
2016/05/24 Python
python GUI实例学习
2017/11/21 Python
Python实现矩阵相乘的三种方法小结
2018/07/26 Python
Python求两点之间的直线距离(2种实现方法)
2019/07/07 Python
PyCharm2020最新激活码+激活码补丁(亲测最新版PyCharm2020.2激活成功)
2020/11/25 Python
基于Python的图像阈值化分割(迭代法)
2020/11/20 Python
python 发送邮件的示例代码(Python2/3都可以直接使用)
2020/12/03 Python
HTML5实现预览本地图片
2016/02/17 HTML / CSS
非常详细的C#面试题集
2016/07/13 面试题
XMLHttpRequest对象在IE和Firefox中创建方式有没有不同
2016/03/23 面试题
高中校园广播稿
2014/01/11 职场文书
心理学专业大学生职业生涯规划范文
2014/02/19 职场文书
优秀广告词大全
2014/03/19 职场文书
黄埔军校观后感
2015/06/10 职场文书
基于Redis位图实现用户签到功能
2021/05/08 Redis
js Proxy的原理详解
2021/05/25 Javascript
Python Pandas读取Excel日期数据的异常处理方法
2022/02/28 Python
Flutter Navigator 实现路由传递参数
2022/04/22 Java/Android