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模拟jq点击事件的实例代码
Jul 06 HTML / CSS
css3中background新增的4个新的相关属性用法介绍
Sep 26 HTML / CSS
CSS的pointer-events属性详细介绍(作用和注意事项)
Apr 23 HTML / CSS
CSS3制作文字半透明倒影效果的两种实现方式
Aug 08 HTML / CSS
canvas粒子动画背景的实现示例
Sep 03 HTML / CSS
HTML5 创建canvas元素示例代码
Jun 04 HTML / CSS
基于HTML5的WebGL经典3D虚拟机房漫游动画
Nov 15 HTML / CSS
HTML5 常见面试题之PC端和移动端区别介绍
Jan 22 HTML / CSS
canvas离屏技术与放大镜实现代码示例
Aug 31 HTML / CSS
html5 canvas手势解锁源码分享
Jan 07 HTML / CSS
Canvas实现放大镜效果完整案例分析(附代码)
Nov 26 HTML / CSS
Html5页面播放M4a音频文件
Mar 30 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
索尼ICF-SW100收音机评测
2021/03/02 无线电
PHP中使用gettext来支持多语言的方法
2011/05/02 PHP
编写Smarty插件在模板中直接加载数据的详细介绍
2013/06/26 PHP
使用PHP接收POST数据,解析json数据
2013/06/28 PHP
php中并发读写文件冲突的解决方案
2013/10/25 PHP
ThinkPHP的URL重写问题
2014/06/22 PHP
解读PHP中的垃圾回收机制
2015/08/10 PHP
php如何修改SESSION的生存存储时间的实例代码
2017/07/05 PHP
PHP简单实现记录网站访问量功能示例
2018/06/06 PHP
让你的PHP,APACHE,NGINX支持大文件上传
2021/03/09 PHP
基于jquery实现图片广告轮换效果代码
2011/07/07 Javascript
Jquery 例外被抛出且未被接住原因介绍
2013/09/04 Javascript
easyui Droppable组件实现放置特效
2015/08/19 Javascript
jquery mobile 移动web(5)
2015/12/20 Javascript
JS Array创建及concat()split()slice()的使用方法
2016/06/03 Javascript
JavaScript实现网页头部进度条刷新
2017/04/16 Javascript
Node.js API详解之 vm模块用法实例分析
2020/05/27 Javascript
基于vue项目设置resolves.alias: '@'路径并适配webstorm
2020/12/02 Vue.js
JavaScript仿京东轮播图效果
2021/02/25 Javascript
[41:05]Serenity vs Pain 2018国际邀请赛小组赛BO2 第二场 8.19
2018/08/21 DOTA
将Python代码打包为jar软件的简单方法
2015/08/04 Python
python清除字符串前后空格函数的方法
2018/10/21 Python
pywinauto自动化操作记事本
2019/08/26 Python
手动安装python3.6的操作过程详解
2020/01/13 Python
python使用Word2Vec进行情感分析解析
2020/07/31 Python
html5教程调用绘图api画简单的圆形代码分享
2013/12/04 HTML / CSS
纽约著名的服装辅料来源:M&J Trimming
2017/07/26 全球购物
英国领先的在线高尔夫设备零售商:Golfgeardirect
2020/12/11 全球购物
通用C#笔试题附答案
2016/11/26 面试题
英语自荐信范文
2013/12/11 职场文书
企业道德讲堂实施方案
2014/03/19 职场文书
美容院合作经营协议书
2014/10/10 职场文书
寻找最美乡村教师观后感
2015/06/18 职场文书
致三级跳运动员加油稿
2015/07/21 职场文书
高二数学教学反思
2016/02/18 职场文书
python中使用redis用法详解
2022/12/24 Redis