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 相关文章推荐
使用HTML5和CSS3表单验证功能
May 05 HTML / CSS
CSS3教程(9):设置RGB颜色
Apr 02 HTML / CSS
深入浅析css3 border-image边框图像详解
Nov 24 HTML / CSS
利用CSS3实现进度条的两种姿势详解
Mar 21 HTML / CSS
深入研究HTML5实现图片压缩上传功能
Mar 25 HTML / CSS
Android本地应用打开方法——通过html5写连接
Mar 11 HTML / CSS
HTML5教程之html 5 本地数据库(Web Sql Database)
Apr 03 HTML / CSS
HTML5使用drawImage()方法绘制图像
Jun 23 HTML / CSS
HTML5+CSS3实现拖放(Drag and Drop)示例
Jul 07 HTML / CSS
详解HTML5中的元素与元素
Aug 17 HTML / CSS
纯CSS3实现div按照顺序出入效果
Jul 15 HTML / CSS
纯CSS如何禁止用户复制网页的内容
Nov 01 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无敌近乎加密方式!
2010/07/17 PHP
PHP有序表查找之插值查找算法示例
2018/02/10 PHP
限制文本字节数js代码
2007/03/06 Javascript
JQuery与Ajax常用代码实现对比
2009/10/03 Javascript
推荐 21 款优秀的高性能 Node.js 开发框架
2014/08/18 Javascript
jQuery中document与window以及load与ready 区别详解
2014/12/29 Javascript
JS实现一个按钮的方法
2015/02/05 Javascript
详解JavaScript中的自定义事件编写
2016/05/10 Javascript
基于JavaScript实现点击页面任何位置返回
2016/08/31 Javascript
微信小程序 数组(增,删,改,查)等操作实例详解
2017/01/05 Javascript
React根据宽度自适应高度的示例代码
2017/10/11 Javascript
MUI 实现侧滑菜单及其主体部分上下滑动的方法
2018/01/25 Javascript
NodeJS使用Range请求实现下载功能的方法示例
2018/10/12 NodeJs
layui 实现加载动画以及非真实加载进度的方法
2019/09/23 Javascript
[00:35]DOTA2上海特级锦标赛 EG战队宣传片
2016/03/04 DOTA
详解Python中break语句的用法
2015/05/14 Python
python线程池threadpool实现篇
2018/04/27 Python
Python实现正弦信号的时域波形和频谱图示例【基于matplotlib】
2018/05/04 Python
flask入门之表单的实现
2018/07/18 Python
python中pip的安装与使用教程
2018/08/10 Python
对python使用telnet实现弱密码登录的方法详解
2019/01/26 Python
python 调用钉钉机器人的方法
2019/02/20 Python
Python实现的插入排序,冒泡排序,快速排序,选择排序算法示例
2019/05/04 Python
python数据归一化及三种方法详解
2019/08/06 Python
python lambda函数及三个常用的高阶函数
2020/02/05 Python
python实现将range()函数生成的数字存储在一个列表中
2020/04/02 Python
利物浦足球俱乐部官方网上商店:Liverpool FC Official Store
2018/01/13 全球购物
Mansur Gavriel官网:纽约市的一个设计品牌
2019/05/02 全球购物
2014年督导工作总结
2014/11/19 职场文书
财产保全担保书
2015/01/20 职场文书
企业财务经理岗位职责
2015/04/08 职场文书
工作调动申请报告
2015/05/18 职场文书
React Hook用法示例详解(6个常见hook)
2021/04/28 Javascript
js实现自动锁屏功能
2021/06/02 Javascript
Mysql中存储引擎的区别及比较
2021/06/04 MySQL
一次SQL如何查重及去重的实战记录
2022/03/13 MySQL