360开机小助手js循环效果
这里我想用这个案例探讨一下onclick和addEventListener嵌套问题,下面是案例的整体代码
<style>
.box {
width: 322px;
position: fixed;
bottom: 0;
right: 0;
overflow: hidden;
}
span {
position: absolute;
top: 0;
right: 0;
width: 30px;
height: 20px;
cursor: pointer;
}
.bd {
height: 105px;
transition: all 2s;
}
.box {
transition: all 0.6s;
}
</style>
<body>
<div class="box" id="box">
<span id="closeButton"></span>
<div class="hd" id="headPart"><img src="images/t.jpg" alt=""></div>
<div class="bd" id="bottomPart"><img src="images/b.jpg" alt=""></div>
</div>
</body>
<script>
window.onload = function () {
setInterval(function () {
var btn = document.querySelector('#closeButton')
var bottomPart = document.querySelector('#bottomPart')
var box = document.querySelector('#box')
btn.onclick = function () {
bottomPart.style.height = 0
// bottomPart.addEventListener('transitionend', function () {
// box.style.width = 0;
// })
console.log(1);
}
bottomPart.addEventListener('transitionend', function () {
box.style.width = 0;
console.log(2);
})
console.log(3);
bottomPart.style.height = 105+'px'
box.style.width = 322+'px';
}, 10000)
}
</script>
我想实现点击按钮,360下移再往右边移出,然后再恢复原来的样子,再点击按钮反复循环,如果addEventListener嵌套在onclick时间里面是否会重复addEventListener注册的行为,具体原理是什么,有没有大神能解析一下我这个代码,图片我放到下面
图片:
一共两张图片