思路:点击当前位置创建小球,获取点击的位置的top left ,下落到指定类名的位置。
js: theBall.js
// import './theBall.css'
function newBall(event) {//当前点击的元素
let div = document.createElement('div');
div.className = 'ballFather';
document.body.appendChild(div);
let sonDiv = document.createElement('div');
sonDiv.className = 'ballSon';
div.appendChild(sonDiv);
let top = event.target.getBoundingClientRect().top;//获得 跳动小球top
let left = event.target.getBoundingClientRect().left;//获得 left
div.style.left = left + 'px';
div.style.top = top + 'px';
let y = (window.innerHeight - top);// 下落的高度
// div.style.display ='block';
div.style.webkitTransform = `translate3d(0,${y}px,0)`;
div.style.transform = `translate3d(0,${y}px,0)`;
sonDiv.style.webkitTransform = `translate3d(-${left - 30}px,0,0)`;
sonDiv.style.transform = `translate3d(-${left - 40}px,0,0)`;
//保证 小球运动结束后,调用购物车的动画
setTimeout(() => {
document.body.removeChild(div);
document.querySelector('.bottom_car_img').classList.add("animation");
setTimeout(() => {
document.querySelector('.bottom_car_img').classList.remove("animation");
}, 400)
}, 620)
}
css:theBall.css
.ballFather {
position: absolute;
width: 0.3rem;
height: 0.3rem;
border-radius: 50%;
transition: all 0.6s cubic-bezier(1, -0.5, 1, 1);
}
.ballSon {
width: 0.3rem;
height: 0.3rem;
background: red;
border-radius: 50%;
transition: all 0.6s linear;
}
@keyframes ani {
0% {
transform: scale(0.5)
}
20% {
transform: scale(1.2)
}
40% {
transform: scale(.8)
}
80% {
transform: scale(1.1)
}
100% {
transform: scale(1)
}
}
.animation {
animation: ani .4s linear;
}
html:调用newBall()
<link rel="stylesheet" href="./js/theball/theBall.css">
<script src="./js/theball/theBall.js"></script>
shangpin(e,index){ //购物车动画效果
console.log(e)
newBall(e)
// setTimeout(function(){
// let div=document.querySelector('.ballFather')
// document.body.removeChild(div)
// },2000)
},