jQuery模拟实现天猫购物车动画效果实例代码

发布时间 - 2026-01-11 01:16:51    点击率:

一、功能描述:

   1、点击购买按钮,模拟抛物线将物品弹到购物车里;

   2、购物车添加物品后,显示+1动画;

效果图如下:

实现如下:

   1、导入jquery相关的包:

<script type="text/javascript" src="jquery-2.1.1.min.js"></script>
<script src="jquery.fly.min.js"></script> 

  2、html文件内容:

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<title>jQuery实现模拟天猫加入购物车飞入动画效果</title>
<meta name="keywords" content="jquery,购物车" />
<body>
<div id="main">
  <div class="demo">
    <div class="box orange button addcar">
      <div style="padding-top:55px">
      土豪,请猛击我,加入购物车
      </div>
    </div>
  </div>
  </div>
  <div class="m-sidebar">
    <div class="cart">
      <i id="end"></i>
      <span>购物车</span>
    </div>
  </div>
</div>
</body>

3、css样式:

<style type="text/css">
.demo{width:820px; margin:60px auto 10px auto}
.m-sidebar{position: fixed;top: 0;right: 0;background: #000;z-index: 2000;width: 85px;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:10 0 0 0;}
.cart i{width:50px;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}
.box{width:300px; height:150px; border:1px solid #e0e0e0; text-align:center}
.u-flyer{display: block;width: 50px;height: 50px;border-radius: 50px;position: fixed;z-index: 9999;}
.button {
  display: inline-block;
  outline: none;
  cursor: pointer;
  text-align: center;
  text-decoration: none;
  font: 16px/100% 'Microsoft yahei',Arial, Helvetica, sans-serif;
  padding: .5em 2em .55em;
  text-shadow: 0 1px 1px rgba(0,0,0,.3);
  -webkit-border-radius: .5em; 
  -moz-border-radius: .5em;
  border-radius: .5em;
  -webkit-box-shadow: 0 1px 2px rgba(0,0,0,.2);
  -moz-box-shadow: 0 1px 2px rgba(0,0,0,.2);
  box-shadow: 0 1px 2px rgba(0,0,0,.2);
}
.button:hover {
  text-decoration: none;
}
.button:active {
  position: relative;
  top: 1px;
}
/* orange */
.orange {
  color: #fef4e9;
  border: solid 1px #da7c0c;
  background: #f78d1d;
  background: -webkit-gradient(linear, left top, left bottom, from(#faa51a), to(#f47a20));
  background: -moz-linear-gradient(top, #faa51a, #f47a20);
  filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#faa51a', endColorstr='#f47a20');
}
.orange:hover {
  background: #f47c20;
  background: -webkit-gradient(linear, left top, left bottom, from(#f88e11), to(#f06015));
  background: -moz-linear-gradient(top, #f88e11, #f06015);
  filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#f88e11', endColorstr='#f06015');
}
.orange:active {
  color: #fcd3a5;
  background: -webkit-gradient(linear, left top, left bottom, from(#f47a20), to(#faa51a));
  background: -moz-linear-gradient(top, #f47a20, #faa51a);
  filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#f47a20', endColorstr='#faa51a');
}

4、核心的JQuery代码:

<script>
//实现购物车+1动画效果
(function ($) {
  $.extend({
    tipsBox: function (options) {
      options = $.extend({
        obj: null, //jq对象,要在那个html标签上显示
        str: "+1", //字符串,要显示的内容
        startSize: "18px", //动画开始的文字大小
        endSize: "38px",  //动画结束的文字大小
        interval: 600, //动画时间间隔
        color: "red",  //文字颜色
        callback: function () { }  //回调函数
      }, options);
      $("body").append("<span class='num'>" + options.str + "</span>");
      var box = $(".num");
      var left = options.obj.offset().left + options.obj.width() / 2;
      var top = options.obj.offset().top - options.obj.height();
      box.css({
        "position": "absolute",
        "left": left + "px",
        "top": top + "px",
        "z-index": 9999,
        "font-size": options.startSize,
        "line-height": options.endSize,
        "color": options.color
      });
      box.animate({
        "font-size": options.endSize,
        "opacity": "0",
        "top": top - parseInt(options.endSize) + "px"
      }, options.interval, function () {
        box.remove();
        options.callback();
      });
    }
  });
})(jQuery);
function niceIn(prop){
  prop.find('i').addClass('niceIn');
  setTimeout(function(){
    prop.find('i').removeClass('niceIn');
  },1000);
}
//实现抛物线动画功能
$(function() {
  var offset = $("#end").offset();
  $(".addcar").click(function(event){
    var addcar = $(this);
    var img = "images/lg.jpg";
    var flyer = $('<img class="u-flyer" src="'+img+'">');
    flyer.fly({
      start: {
        left: event.pageX,
        top: event.pageY
      },
      end: {
        left: offset.left+10,
        top: offset.top+10,
        width: 0,
        height: 0
      },
      onEnd: function(){
        $.tipsBox({
            obj: $("#end"),
            str: "<b style='font-family:Microsoft YaHei;'>+1</b>",
            callback: function () {
            }
        });
        niceIn($("#end"));
      }
    });
  });
});
</script>

以上所述是小编给大家介绍的jQuery模拟实现天猫购物车动画效果实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!


# jquery购物车动画效果  # jquery实现天猫购物车  # jQuery仿天猫实现超炫的加入购物车  # 购物车  # 小编  # 在此  # 要在  # 给大家  # 所述  # 车里  # 回调  # 给我留言  # 感谢大家  # 疑问请  # 有任何  # margin  # css  # auto  # background  # fixed  # position  # button  # addcar 


相关栏目: 【 网站优化151355 】 【 网络推广146373 】 【 网络技术251813 】 【 AI营销90571


相关推荐: Laravel如何连接多个数据库_Laravel多数据库连接配置与切换教程  VIVO手机上del键无效OnKeyListener不响应的原因及解决方法  如何在IIS7中新建站点?详细步骤解析  Laravel API资源类怎么用_Laravel API Resource数据转换  大型企业网站制作流程,做网站需要注册公司吗?  如何在阿里云服务器自主搭建网站?  iOS正则表达式验证手机号、邮箱、身份证号等  实例解析Array和String方法  Laravel 419 page expired怎么解决_Laravel CSRF令牌过期处理  高端智能建站公司优选:品牌定制与SEO优化一站式服务  Laravel如何构建RESTful API_Laravel标准化API接口开发指南  Android自定义listview布局实现上拉加载下拉刷新功能  如何在IIS中新建站点并解决端口绑定冲突?  制作旅游网站html,怎样注册旅游网站?  高端建站如何打造兼具美学与转化的品牌官网?  Laravel队列由Redis驱动怎么配置_Laravel Redis队列使用教程  Laravel怎么使用Collection集合方法_Laravel数组操作高级函数pluck与map【手册】  网页设计与网站制作内容,怎样注册网站?  Swift开发中switch语句值绑定模式  WEB开发之注册页面验证码倒计时代码的实现  在线制作视频的网站有哪些,电脑如何制作视频短片?  如何有效防御Web建站篡改攻击?  文字头像制作网站推荐软件,醒图能自动配文字吗?  制作公司内部网站有哪些,内网如何建网站?  Laravel如何实现邮件验证激活账户_Laravel内置MustVerifyEmail接口配置【步骤】  深圳防火门网站制作公司,深圳中天明防火门怎么编码?  Laravel如何获取当前用户信息_Laravel Auth门面获取用户ID  Laravel怎么创建控制器Controller_Laravel路由绑定与控制器逻辑编写【指南】  Laravel如何处理异常和错误?(Handler示例)  Laravel如何使用Seeder填充数据_Laravel模型工厂Factory批量生成测试数据【方法】  详解Huffman编码算法之Java实现  LinuxCD持续部署教程_自动发布与回滚机制  如何在阿里云完成域名注册与建站?  html5怎么画眼睛_HT5用Canvas或SVG画眼球瞳孔加JS控制动态【绘制】  如何用5美元大硬盘VPS安全高效搭建个人网站?  悟空浏览器如何设置小说背景色_悟空浏览器背景色设置【方法】  html文件怎么打开证书错误_https协议的html打开提示不安全【指南】  详解Android图表 MPAndroidChart折线图  WordPress 子目录安装中正确处理脚本路径的完整指南  广州网站制作公司哪家好一点,广州欧莱雅百库网络科技有限公司官网?  Laravel路由怎么定义_Laravel核心路由系统完全入门指南  Android Socket接口实现即时通讯实例代码  百度浏览器ai对话怎么关 百度浏览器ai聊天窗口隐藏  iOS UIView常见属性方法小结  专业企业网站设计制作公司,如何理解商贸企业的统一配送和分销网络建设?  Laravel Seeder怎么填充数据_Laravel数据库填充器的使用方法与技巧  零服务器AI建站解决方案:快速部署与云端平台低成本实践  详解一款开源免费的.NET文档操作组件DocX(.NET组件介绍之一)  哪家制作企业网站好,开办像阿里巴巴那样的网络公司和网站要怎么做?  网易LOFTER官网链接 老福特网页版登录地址