vue加入购物车

此博客展示了如何使用Vue.js和CSS创建一个动态效果,当用户点击商品图片时,图片会无缝滑动并加入到固定在底部右角的购物车中。通过flex布局和CSS动画,实现了流畅的交互体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在这里插入图片描述

<!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) {
                //el 当前操作的动画的元素 
                // done 代表动画完成后执行的函数 
                // 确定点击元素li的位置 
                // span的位置定位成跟li的
                // 购物车的位置
                // 求出span(图片)到购物车的移动距离
                let li = this.$refs.listArr[this.currentIndex] //点击的li
                let { x, y } = li.getBoundingClientRect(); //li距离窗口的位置
                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(); // 重命名为a和b
                //scale  缩放
                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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值