加入购物车动画效果

css代码块

.footer{
    bottom: 0;
    position: fixed;
}

ul li{
    list-style: none;
    display: flex;
}
img{
    width: 100px;
}
ul li button{
    height: 30px;
    line-height: 30px;
    margin-right: 10px;
}
ul li span{
    margin-right: 10px;
}
#ball{
    width: 15px;
    height: 15px;
    background: aqua;
    border-radius: 50%;
    position: fixed;
    transition: left 1s linear , top 1s ease-in;
    z-index: 1;
}

js代码块

export default function App7() {
    const [list, setList] = useState(lis)
    // 点击按钮 抛掷小球
    const addNum = (e, id) => {
        let idx = list.findIndex(item => item.id === id)
        list[idx].num += 1
        setList([...list])
        var ball = document.getElementById('ball');  //获取小球的dom节点
        // 将小球进行归位,移到点击的位置,
        ball.style.top = e.pageY + 'px';
        ball.style.left = e.pageX + 'px';
        ball.style.transition = 'left 0s, top 0s';  //动画
        // 小球向下进行抛掷, 设置定时器可以重复点击
        setTimeout(() => {
            ball.style.top = window.innerHeight + 'px';
            ball.style.bottom = "20px"
            ball.style.left = '10px';
            //css动画   加上cubic-bezier(.35,.8,1,1) 立方贝塞尔曲线  底部有官网链接
            ball.style.transition = 'left 1s cubic-bezier(.35,.8,1,1), top 1s ease-in';
        }, 20)
    }

    const descNum = (e,id) => {
        let idx = list.findIndex(item => item.id === id)
        if (list[idx].num < 1) {
            return false
        } else {
            list[idx].num -= 1
        }
        setList([...list])
        var ball = document.getElementById('ball');  //获取小球的dom节点
        // 将小球进行归位,移到点击的位置,
        ball.style.top = e.pageY + 'px';
        ball.style.left = e.pageX + 'px';
        ball.style.transition = 'left 0s, top 0s';  //动画
        // 小球向下进行抛掷, 设置定时器可以重复点击
        setTimeout(() => {
            ball.style.top = window.innerHeight + 'px';
            ball.style.bottom = "20px"
            ball.style.left = '10px';
            //css动画   加上cubic-bezier(.35,.8,1,1) 立方贝塞尔曲线  底部有官网链接
            ball.style.transition = 'left 1s cubic-bezier(.35,.8,1,1), top 1s ease-in';
        }, 20)
    }
    let nums = 0
    let zj = () => {
        lis.forEach(item => {
            nums += item.num
        })
        return nums
    }
    return (
        <div>
            <ul>
                {
                    list.map(item => {
                        return (
                            <li key={item.id}>
                                <img src={item.img} alt="" />
                                <p>{item.name}</p>
                                <button onClick={(e) => { descNum(e,item.id) }}>-</button>
                                <span>{item.num}</span>
                                <button onClick={(e) => { addNum(e, item.id) }}>+</button>
                            </li>
                        )
                    })
                }
            </ul>
            <div id="ball"></div>
            <div className="footer">
                <ActionBar>
                    <ActionBar.Icon icon={<CartO />} badge={{ content: zj() }} text='购物车' />
                </ActionBar>
            </div>
        </div>
    )
}

 实现数量加减购物车也会发生相对应的变化

   let nums = 0

    let zj = () => {

        lis.forEach(item => {

            nums += item.num

        })

        return nums

    }

 <div className="footer">

                <ActionBar>

                    <ActionBar.Icon icon={<CartO />} badge={{ content: zj() }} text='购物车' />

                </ActionBar>

            </div>

 

js加入购物车抛物线动画购物车效果特效,亲测可用, 当您在电商购物网站浏览中意的商品时,您可以点击页面中的“加入购物车”按钮即可将商品加入购物车中。本文介绍借助一款基于jQuery的动画插件,点击加入购物车按钮时,实现商品将飞入到右侧的购物车中的效果。 HTML 首先载入jQuery库文件和jquery.fly.min.js插件。 复制代码 代码如下: 接着,将商品信息html结构布置好,本例中,我们用四个商品并排布置,每个商品box中包括有商品图片、价格、名称以及加入购物车按钮等信息。 复制代码 代码如下: ¥3499.00 LG 49LF5400-CA 49寸IPS硬屏富贵招财铜钱设计 加入购物车 ¥3799.00 Hisense/海信 LED50T1A 海信电视官方旗舰店 加入购物车 ¥¥3999.00 Skyworth/创维 50E8EUS 8核4Kj极清酷开系统智能液晶电视 加入购物车 ¥6969.00 乐视TV Letv X60S 4核1080P高清3D安卓智能超级电视 加入购物车 然后,我们还需要在页面的右侧加上购物车以及提示信息。 复制代码 代码如下: 购物车 已成功加入购物车CSS 我们使用CSS先将商品排列美化,然后设置右侧购物车样式,具体请看代码: 复制代码 代码如下: .box{float:left; width:198px; height:320px; margin-left:5px; border:1px solid #e0e0e0; text-align:center} .box p{line-height:20px; padding:4px 4px 10px 4px; text-align:left} .box:hover{border:1px solid #f90} .box h4{line-height:32px; font-size:14px; color:#f30;font-weight:500} .box h4 span{font-size:20px} .u-flyer{display: block;width: 50px;height: 50px;border-radius: 50px;position: fixed;z-index: 9999;} .m-sidebar{position: fixed;top: 0;right: 0;background: #000;z-index: 2000;width: 35px;height: 100%;font-size: 12px;color: #fff;} .cart{color: #fff;t
可以使用jQuery的动画函数来实现加入购物车动画效果,具体实现步骤如下: 1. 给“加入购物车”按钮添加点击事件,通过jQuery选择器获取到该按钮元素,并为其添加click事件处理函数。 ```javascript $('#add-to-cart-btn').click(function() { // 动画效果代码 }); ``` 2. 获取到被添加到购物车的商品图片元素,通过jQuery选择器获取到该元素,并获取到该元素的位置信息。 ```javascript var productImg = $('#product-img'); var imgPosition = productImg.offset(); ``` 3. 在页面上添加一个新的图片元素,该元素的src属性值为被添加到购物车的商品图片的src属性值。 ```javascript var cartImg = $('<img src="' + productImg.attr('src') + '">'); ``` 4. 设置新的图片元素的样式,使其定位到被添加到购物车的商品图片的位置,并设置其宽度和高度。 ```javascript cartImg.css({ 'position': 'absolute', 'top': imgPosition.top, 'left': imgPosition.left, 'width': productImg.width(), 'height': productImg.height() }); ``` 5. 将新的图片元素添加到页面上,并使用jQuery的animate函数使其移动到购物车图标的位置,并在动画结束后将其从页面中移除。 ```javascript cartImg.appendTo('body').animate({ 'top': cartIconPosition.top, 'left': cartIconPosition.left, 'width': 0, 'height': 0 }, 1000, function() { $(this).remove(); }); ``` 完整代码如下: ```javascript $('#add-to-cart-btn').click(function() { var productImg = $('#product-img'); var imgPosition = productImg.offset(); var cartIcon = $('#cart-icon'); var cartIconPosition = cartIcon.offset(); var cartImg = $('<img src="' + productImg.attr('src') + '">'); cartImg.css({ 'position': 'absolute', 'top': imgPosition.top, 'left': imgPosition.left, 'width': productImg.width(), 'height': productImg.height() }); cartImg.appendTo('body').animate({ 'top': cartIconPosition.top, 'left': cartIconPosition.left, 'width': 0, 'height': 0 }, 1000, function() { $(this).remove(); }); }); ``` 上述代码实现了一个简单的加入购物车动画效果,可以根据需求进行修改和完善。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值