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


Posted in jQuery onMay 25, 2017

一、功能描述:

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

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

效果图如下:

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

实现如下:

   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 相关文章推荐
BootStrap中jQuery插件Carousel实现轮播广告效果
Mar 27 jQuery
jQuery实现选中行变色效果(实例讲解)
Jul 06 jQuery
jQuery上传插件webupload使用方法
Aug 01 jQuery
jQuery+vue.js实现的九宫格拼图游戏完整实例【附源码下载】
Sep 12 jQuery
jQuery选择器之属性筛选选择器用法详解
Sep 19 jQuery
jQuery选择器之子元素过滤选择器
Sep 28 jQuery
解决JQuery全选/反选第二次失效的问题
Oct 11 jQuery
jquery一键控制checkbox全选、反选或全不选
Oct 16 jQuery
Vue中正确使用jQuery的方法
Oct 30 jQuery
高效jQuery选择器的5个技巧实例分析
Nov 26 jQuery
jQuery HTML css()方法与css类实例详解
May 20 jQuery
jQuery实现简单轮播图效果
Dec 27 jQuery
jquery.masonry瀑布流效果
May 25 #jQuery
浅谈struts1 &amp; jquery form 文件异步上传
May 25 #jQuery
jquery Form轻松实现文件上传
May 24 #jQuery
解决jQuery ajax动态新增节点无法触发点击事件的问题
May 24 #jQuery
JQuery Ajax 异步操作之动态添加节点功能
May 24 #jQuery
使用jQuery.Pin垂直滚动时固定导航
May 24 #jQuery
jquery append与appendTo方法比较
May 24 #jQuery
You might like
怎么在Windows系统中搭建php环境
2013/08/31 PHP
php解决约瑟夫环示例
2014/04/09 PHP
php实例分享之mysql数据备份
2014/05/19 PHP
php实现只保留mysql中最新1000条记录
2015/06/18 PHP
iOS10推送通知开发教程
2016/09/19 PHP
Yii CFileCache 获取不到值的原因分析
2017/02/08 PHP
ThinkPHP3.2框架使用addAll()批量插入数据的方法
2017/03/16 PHP
jQuery表格插件ParamQuery简单使用方法示例
2013/12/05 Javascript
使用jQuery监听DOM元素大小变化
2016/02/24 Javascript
js模糊查询实例分享
2016/12/26 Javascript
js实现横向拖拽导航条功能
2017/02/17 Javascript
element-ui 设置菜单栏展开的方法
2018/08/22 Javascript
Vue中的情侣属性$dispatch和$broadcast详解
2019/03/07 Javascript
微信公众平台获取access_token的方法步骤
2019/03/29 Javascript
小程序封装路由文件和路由方法(5种全解析)
2019/05/26 Javascript
webpack 最佳配置指北(推荐)
2020/01/07 Javascript
flexible.js实现移动端rem适配方案
2020/04/07 Javascript
[02:44]DOTA2英雄基础教程 克林克兹
2014/01/15 DOTA
Python连接mysql数据库的正确姿势
2016/02/03 Python
浅析Python编写函数装饰器
2016/03/18 Python
Python3多进程 multiprocessing 模块实例详解
2018/06/11 Python
基于pycharm导入模块显示不存在的解决方法
2018/10/13 Python
Pycharm 设置默认头的图文教程
2019/01/17 Python
python异常处理、自定义异常、断言原理与用法分析
2020/03/23 Python
Python RabbitMQ实现简单的进程间通信示例
2020/07/02 Python
我的珠宝盒:Ma boîte à bijoux
2019/08/27 全球购物
经理管理专业自荐信范文
2013/12/31 职场文书
技校个人求职信范文
2014/01/25 职场文书
葬礼司仪主持词
2014/03/31 职场文书
初中作文评语大全
2014/04/23 职场文书
英语教师求职信
2014/06/16 职场文书
学生打架检讨书
2014/10/20 职场文书
研究生就业推荐表导师评语
2014/12/31 职场文书
餐厅服务员管理制度
2015/08/05 职场文书
Vue和Flask通信的实现
2021/05/19 Vue.js
SpringBoot+Redis实现布隆过滤器的示例代码
2022/03/17 Java/Android