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 相关文章推荐
jquery将标签元素的高设为屏幕的百分比
Apr 19 jQuery
jQuery表单验证之密码确认
May 22 jQuery
jQuery回调方法使用示例
Jun 26 jQuery
jQuery实现的手动拖动控制进度条效果示例【测试可用】
Apr 18 jQuery
jQuery+ajax实现动态添加表格tr td功能示例
Apr 23 jQuery
jQuery实现ajax回调函数带入参数的方法示例
Jun 26 jQuery
jQuery中each遍历的三种方法实例分析
Sep 07 jQuery
使用异步controller与jQuery实现卷帘式分页
Jun 18 jQuery
jQuery内容选择器与表单选择器实例分析
Jun 28 jQuery
jQuery中DOM操作原则实例分析
Aug 01 jQuery
JQuery基于FormData异步提交数据文件
Sep 01 jQuery
jQuery treeview树形结构应用
Mar 24 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
深入PHP empty(),isset(),is_null()的实例测试详解
2013/06/06 PHP
一个漂亮的php验证码类(分享)
2013/08/06 PHP
php打印输出棋盘的实现方法
2014/12/23 PHP
Yii中的cookie的发送和读取
2016/07/27 PHP
JavaScript confirm选择判断
2008/10/18 Javascript
JS对URL字符串进行编码/解码分析
2008/10/25 Javascript
javascript mouseover、mouseout停止事件冒泡的解决方案
2009/04/07 Javascript
指定位置如果有图片显示图片,无图片显示广告的JS
2010/06/05 Javascript
jquery.combobox中文api和例子,修复了上面的小bug
2011/03/28 Javascript
用最通俗易懂的代码帮助新手理解javascript闭包 推荐
2012/03/01 Javascript
多种方式实现JS调用后台方法进行数据交互
2013/08/20 Javascript
Extjs 4.x 得到form CheckBox 复选框的值
2014/05/04 Javascript
Javascript节点关系实例分析
2015/05/15 Javascript
jQuery获取页面及个元素高度、宽度的总结——超实用
2015/07/28 Javascript
javascript之Array 数组对象详解
2016/06/07 Javascript
如何处理JSON中的特殊字符
2016/11/30 Javascript
vue-cli启动本地服务局域网不能访问的原因分析
2018/01/22 Javascript
浅谈目前可以使用ES10的5个新特性
2019/06/25 Javascript
jQuery列表动态增加和删除的实现方法
2020/11/05 jQuery
[04:49]期待西雅图之战 2016国际邀请赛中国区预选赛WINGS战队赛后采访
2016/06/29 DOTA
Python实现在Linux系统下更改当前进程运行用户
2015/02/04 Python
Python判断变量是否为Json格式的字符串示例
2017/05/03 Python
基于使用paramiko执行远程linux主机命令(详解)
2017/10/16 Python
python中for循环输出列表索引与对应的值方法
2018/11/07 Python
浅谈numpy生成数组的零值问题
2018/11/12 Python
HTML5 FileReader对象的具体使用方法
2020/05/22 HTML / CSS
娇韵诗Clarins意大利官方网站:法国天然护肤品牌
2020/03/11 全球购物
黄继光的英雄事迹材料
2014/02/13 职场文书
节能减排倡议书
2014/04/15 职场文书
住宿生擅自离校检讨书
2014/09/22 职场文书
校园广播稿精选
2014/10/01 职场文书
2015年项目工作总结
2015/04/29 职场文书
html5中sharedWorker实现多页面通信的示例代码
2021/05/07 Javascript
Python常用配置文件ini、json、yaml读写总结
2021/07/09 Python
 Redis 串行生成顺序编码的方法实现
2022/04/03 Redis
i5-10400f处理相当于i7多少水平
2022/04/19 数码科技