jQuery实现加入购物车飞入动画效果

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了jQuery实现加入购物车飞入动画效果脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

jQuery实现加入购物车飞入动画效果

HTML

首先载入jquery库文件和jquery.fly.min.js插件


<script src="jquery.js"></script>
<script src="jquery.fly.min.js"></script>

接着,将商品信息html结构布置好,本例中,我们用四个商品并排布置,每个商品Box包括有商品图片、价格、名称以及加入购物车按钮等信息。


<div class="Box">
&nbsp;   <img src="images/lg.jpg" width="180" height="180">
    <h4>¥<span>3499.00</span></h4>
    <p>LG 49LF5400-CA 49寸IPS硬屏富贵招财铜钱设计</p>
    <a href="#" class="button orange addcar">加入购物车</a>
</div>
<div class="Box">
    <img src="images/hs.jpg" width="180" height="180">
    <h4>¥<span>3799.00</span></h4>
    <p>Hisense/海信 LED50T1A 海信官方旗舰店</p>
    <a href="#" class="button orange addcar">加入购物车</a>
</div>
<div class="Box">
    <img src="images/cw.jpg" width="180" height="180">
    <h4>¥<span>¥3999.00</span></h4>
    <p>Skyworth/创维 50E8EUS 8核4Kj极清酷开系统智能液晶电视</p>
    <a href="#" class="button orange addcar">加入购物车</a>
</div>
<div class="Box">
    <img src="images/ls.jpg" width="180" height="180">
    <h4>¥<span>6969.00</span></h4>
    <p>乐视TV Letv X60S 4核1080P高清3D安卓智能超级电视</p>
    <a href="#" class="button orange addcar">加入购物车</a>
</div>

然后,我们还需要在页面的右侧加上购物车以及提示信息。


<div class="m-sidebar">
    <div class="cart">
        <i id="end"></i>
        <span>购物车</span>
    </div>
</div>
<div id="msg">已成功加入购物车!</div>

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;text-align:center;line-height: 20px;padding: 200px 0 0 0px;}
.cart span{display:block;width:20px;margin:0 auto;}
.cart i{width:35px;height:35px;display:block; background:url(car.png) no-rePEat;}
#msg{position:fixed; top:300px; right:35px; z-index:10000; width:1px; height:52px; line-height:52px; font-size:20px; text-align:center; color:#fff; background:#360; display:none}

jQuery

我们要实现的效果是,当用户点击“加入购物车”按钮时,当前商品图片会变成一个缩小的球,以按钮为起点,向右侧以抛物线的形式飞出,最后落入页面右侧的购物车里,并提示操作成功。在飞出之前,我们要获取当前商品的图片,然后调用fly插件,之后的抛物线轨迹都是由fly插件完成,我们只需定义起点和终点等参数即可。


<script>
$(function() {
    VAR offset = $("#end").offset();
    $(".addcar").click(function(event){
        var addcar = $(this);
        var img = addcar.parent().find('img').attr('src');
        var flyer = $('<img class="u-flyer" src="'+img+'">');
        flyer.fly({
            start: {
                left: event.pageX,//开始位置(必填)#fly元素会被设置成position: fixed
                top: event.pageY //开始位置(必填)
            },
            end: {
                left: offset.left+10,//结束位置(必填)
                top: offset.top+10,//结束位置(必填)
                width: 0,//结束时
                height: 0 //结束时高度
            },
            onEnd: function(){ //结束回调
                $("#msg").show().aniMATE({width: '250px'},200).fadeOut(1000); //提示信息
                addcar.css("cursor","default").removeClass('orange').unbind('click');
                this.destory(); //移除dom
            }
        });
    });
});
</script>

复制上面的代码,保存并运行即可看到效果,Fly插件的项目官网地址是:https://github.com/amibug/fly,值得一提的是,IE10以下需要添加以下js:

<script src="requestAnimationFrame.js"></script>

以上就是本文的全部内容了,希望大家能够喜欢

脚本宝典总结

以上是脚本宝典为你收集整理的jQuery实现加入购物车飞入动画效果全部内容,希望文章能够帮你解决jQuery实现加入购物车飞入动画效果所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:jQueryjQuery