
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
ul {
list-style: none;
margin: 0;
padding: 0;
}
li {
display: flex;
align-items: center;
}
li img {
width: 150px;
height: 80px;
}
span {
position: absolute;
width: 150px;
height: 80px;
display: block;
transition: all 2s linear;
}
.cart {
position: fixed;
right: 20px;
bottom: 20px;
line-height: 50px;
color: #fff;
text-align: center;
background-color: aqua;
}
</style>
</head>
<body>
<div id="app">
<ul>
<li v-for="(list,index) in lists" :key="index" ref="listArr">
<img :src="list.cover" alt="">
<button @click="addCart(index)">加入购物车</button>
</li>
</ul>
<transition @enter="enter" @after-enter="after">
<span v-if="isShow"></span>
</transition>
<div class="cart" ref="cart">购物车</div>
</div>
</body>
</html>
<script src="vue.js"></script>
<script>
let vm = new Vue({
el: '#app',
data: {
isShow: false,
currentIndex: -1,
lists: [
{
cover: 'http://www.javascriptpeixun.cn/files/course/2019/08-02/203853de0f7a753037.png',
id: 1
},
{
cover: 'http://www.javascriptpeixun.cn/files/course/2019/08-02/203853de0f7a753037.png',
id: 2
},
{
cover: 'http://www.javascriptpeixun.cn/files/course/2019/08-02/203853de0f7a753037.png',
id: 3
},
]
},
methods: {
enter(el, done) {
let li = this.$refs.listArr[this.currentIndex]
let { x, y } = li.getBoundingClientRect();
el.style.left = x + 'px';
el.style.top = y + 'px';
el.style.background = `url(${this.lists[this.currentIndex].cover}) no-repeat`;
el.style.backgroundSize = "100% 100%";
let { x: a, y: b } = this.$refs.cart.getBoundingClientRect();
el.style.transform = `translate3d(${a - x}px,${b - y}px,0) scale(0)`;
el.addEventListener('transitionend', done, false);
},
after() {
this.isShow = false
},
addCart(index) {
this.isShow = "true";
this.currentIndex = index
}
}
})
</script>