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教程(2):网页边框半径和网页圆角
Apr 02 HTML / CSS
CSS3支持IE6, 7, and 8的边框border属性
Dec 28 HTML / CSS
实列教程 一款基于jquery和css3的响应式二级导航菜单
Nov 13 HTML / CSS
CSS3+js实现简单的时钟特效
Mar 18 HTML / CSS
CSS3中的@keyframes关键帧动画的选择器绑定
Jun 13 HTML / CSS
CSS3实现图片抽屉式效果的示例代码
Nov 06 HTML / CSS
HTML5添加鼠标悬浮音响效果不使用FLASH
Apr 23 HTML / CSS
10个最常见的HTML5面试题 附答案
Jun 06 HTML / CSS
html5使用canvas实现弹幕功能示例
Sep 11 HTML / CSS
在canvas上实现元素图片镜像翻转动画效果的方法
Mar 20 HTML / CSS
AmazeUI 手机版页面的顶部导航条Header与侧边导航栏offCanvas的示例代码
Aug 19 HTML / CSS
详解CSS故障艺术
May 25 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 前一天或后一天的日期
2008/06/28 PHP
Array of country list in PHP with Zend Framework
2011/10/17 PHP
php数组生成html下拉列表的方法
2015/07/20 PHP
PHP页面转UTF-8中文编码乱码的解决办法
2015/10/20 PHP
分享php代码将360浏览器导出的favdb的sqlite数据库文件转换为html
2015/12/09 PHP
详解WordPress中的头像缓存和代理中的缓存更新方法
2016/03/01 PHP
PHP缩略图生成和图片水印制作
2017/01/07 PHP
利用phpexcel对数据库数据的导入excel(excel筛选)、导出excel
2017/04/27 PHP
PHP4和PHP5版本下解析XML文档的操作方法实例分析
2017/05/20 PHP
JavaScript方法和技巧大全
2006/12/27 Javascript
javascript indexOf函数使用说明
2008/07/03 Javascript
jQuery选择没有colspan属性的td的代码
2010/07/06 Javascript
原生JavaScript实现连连看游戏(附源码)
2013/11/05 Javascript
jquery基础教程之deferred对象使用方法
2014/01/22 Javascript
jquery中对于批量deferred的处理方法
2014/01/22 Javascript
JS运动相关知识点小结(附弹性运动示例)
2016/01/08 Javascript
BootStrap daterangepicker 双日历控件
2017/06/02 Javascript
详解iframe跨域的几种常用方法(小结)
2019/04/29 Javascript
JavaScript遍历数组和对象的元素简单操作示例
2019/07/09 Javascript
js页面加载后执行的几种方式小结
2020/01/30 Javascript
Python 编码Basic Auth使用方法简单实例
2017/05/25 Python
Python基于生成器迭代实现的八皇后问题示例
2018/05/23 Python
python 按不同维度求和,最值,均值的实例
2018/06/28 Python
opencv python 傅里叶变换的使用
2018/07/21 Python
django 自定义filter 判断if var in list的例子
2019/08/20 Python
Python 字符串处理特殊空格\xc2\xa0\t\n Non-breaking space
2020/02/23 Python
Python+OpenCV图像处理——图像二值化的实现
2020/10/24 Python
美国瑜伽品牌:Gaiam
2017/10/31 全球购物
旅游管理实习自我鉴定
2013/09/29 职场文书
党员志愿者活动总结
2014/06/26 职场文书
义务教育学校标准化建设汇报材料
2014/08/16 职场文书
简历中自我评价范文
2015/03/11 职场文书
新年寄语2016
2015/08/17 职场文书
Go语言空白表示符_的实例用法
2021/07/04 Golang
MYSQL 运算符总结
2021/11/11 MySQL
Python加密与解密模块hashlib与hmac
2022/06/05 Python