利用JavaScript事件冒泡制作特效

冒泡

更多原生js小项目,参见本人GitHub—Aguang5241

利用js冒泡特性:

  • 实现图像经鼠标点击后外围传播的动画效果
  • 注.IE浏览器会由外向内进行传播

效果展示

  • 未点击
    在这里插入图片描述
  • 点击外围
    在这里插入图片描述
  • 点击中心
    在这里插入图片描述

源代码

  • html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <link rel="stylesheet" href="css/bubble.css">
  <script src="js/bubble.js"></script>
  <title>Document</title>
</head>
<body>
  <div class="box outer">
    <div class="box">
      <div class="box">
        <div class="box">
          <div class="box">
            <div class="box">
              <div class="box">
                <div class="box">
                  <div class="box">
                    <div class="box">
                      <div class="box">
                        <div class="box">
                          <div class="box">
                            <div class="box">
                              <div class="box">
                              </div>
                            </div>
                          </div>
                        </div>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</body>
</html>
  • css
* {
  margin: 0;
  padding: 0;
}
body {
  text-align: center;
}
.box {
  position: relative;
  margin: 0 auto;
  border: 1px solid #333;
  background-color: #399c30;
  box-shadow: 1px 1px 10px #333;
}
.outer {
  border: 1px solid #fff;
  background-color: #eee;
}
  • javascript
window.onload = function() {
  var boxes = this.document.querySelectorAll('.box');
  var windowH = this.document.documentElement.clientHeight - 10;

  boxes[0].style.width = windowH + 'px';
  boxes[0].style.height = windowH + 'px';

  for (var i = 1; i < boxes.length; i++) {
    var w = windowH - i * 44;
    var h = windowH - i * 44;
    boxes[i].style.width = h + 'px';
    boxes[i].style.height = h + 'px';
    boxes[i].style.top = '22px';
    boxes[i].style.borderRadius = '10000px';
    boxes[boxes.length - i].style.transition = i/2 + 's';
    change(boxes[i]);
  };
  
  function change(obj) {
    obj.onclick = function() {
      obj.style.backgroundColor = '#007abc';
      obj.style.boxShadow = '10px 10px 10px #333';
    }
  }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值