jquery插件--增减数字
来源:互联网 发布:java future 线程池 编辑:程序博客网 时间:2024/05/01 21:39
转载地址:http://www.ituring.com.cn/minibook/29471
http://jsfiddle.net/gs_jquery/fb4jczwd/
base.html
<!doctype html><html><head><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"><link rel="stylesheet" href="css.css"><script type="text/javascript" src="jquery-2.1.0.js"></script><script type="text/javascript" src="jquery.quantitySelect.js"></script></head><body><div class="num-box fl"><a href="javascript:;" class="num-add fr">+</a><a href="javascript:;" class="num-cut fl num-lose">-</a><span class="num-number">1</span></div><div class="det-msg"></div><script>$(function(){var quantity=$('.num-box').quantity({cutBtn:'.num-cut',addBtn:'.num-add',numBox:'.num-number',msgBox:'.det-msg'})})</script></body></html>css.css
a { color: #333; text-decoration: none;}.fl { float: left;}.fr { float: right;}.num-box, .det-msg { height: 28px; line-height: 28px; margin: 0 10px 0 0;}.num-box { border: 1px solid #b3b3b3; width: 80px; text-align: center;}.num-box a { display: block; width: 24px; height: 28px; cursor: pointer; background: #efefef;}.num-cut { border-right: 1px solid #b3b3b3;}.num-add { border-left: 1px solid #b3b3b3;}.num-number { font-size: 16px;}a.num-lose { color: #aaa; background: #fefefe; cursor: not-allowed;}
jquery.quantitySelect.js
;(function ($) { function Quantity(opt) { this.addBtn = null; //加按钮 this.cutBtn = null; //减按钮 this.numBox = null; //数量div this.msgBox = null; //消息div this.minbuy = 1; //最小购买量 this.maxbuy = 99; //最大购买量 this.stock = 99; //存量 this.tuple = 1; //倍数购买 this.no = 1; //当前选中的数量 this.tupleMaxbuy = 99; //倍数最大购买量 this.tupleMinbuy = 1; //倍数最小购买量 this.init(opt); } //初始化 Quantity.prototype.init = function (opt) { opt = opt || {}; this.addBtn = $(opt.addBtn); this.cutBtn = $(opt.cutBtn); this.numBox = $(opt.numBox); this.msgBox = $(opt.msgBox); this.evt(); }; //负责绑定事件 Quantity.prototype.evt = function () { var _this = this, timer, timerout; _this.addBtn.on('click', function () { _this.add(); }) .on('mousedown', function () { timerout = setTimeout(function () { timer = setInterval(function () { _this.add(); }, 200); }, 500); }) .on('mouseup', function () { clearInterval(timer); clearTimeout(timerout); }); _this.cutBtn.on('click', function () { _this.cut(); }) .on('mousedown', function () { timerout = setTimeout(function () { timer = setInterval(function () { _this.cut(); }, 200); }, 500); }) .on('mouseup', function () { clearInterval(timer); clearTimeout(timerout); }); }; //加 Quantity.prototype.add = function (n) { var no = this.no + this.tuple; if (no <= this.tupleMaxbuy) { this.no = no; this.numBox.text(this.no); } this.btnStyle(); this.msgCtrl(); }; //减 Quantity.prototype.cut = function (n) { var no = this.no - this.tuple; if (no >= this.tupleMinbuy) { this.no = no; this.numBox.text(this.no); } this.btnStyle(); this.msgCtrl(); }; //设置最小购买量 Quantity.prototype.setMinbuy = function (n) { this.minbuy = Number(n); this.reinit(); }; //设置最大购买量 Quantity.prototype.setMaxbuy = function (n) { this.maxbuy = Number(n); this.reinit(); }; //设置库存 Quantity.prototype.setStock = function (n) { this.stock = Number(n); this.reinit(); }; //设置倍数购买 Quantity.prototype.setTuple = function (n) { this.tuple = n; this.reinit(); }; //设置值后,刷新一些状态 Quantity.prototype.reinit = function () { //1.真实的最大购买量和真实的最小购买量计算 this.tupleMaxbuy = Math.floor(Math.min(this.maxbuy, this.stock) / this.tuple) * this.tuple; this.tupleMinbuy = Math.max(Math.ceil(this.minbuy / this.tuple) * this.tuple, this.tuple); //2.将当前的选中数变成真实的最小购买量 this.no = this.tupleMinbuy; //3.界面上显示真实的最小购买量 this.numBox.text(this.tupleMinbuy); //4.控制样式 this.btnStyle(); }; //控制按钮样式 Quantity.prototype.btnStyle = function () { if (this.no <= this.tupleMinbuy) { this.cutBtn.addClass('num-lose'); } else { this.cutBtn.removeClass('num-lose'); } if (this.no >= this.tupleMaxbuy) { this.addBtn.addClass('num-lose'); } else { this.addBtn.removeClass('num-lose'); } }; //消息控制 Quantity.prototype.msgCtrl = function () { this.msgBox.empty(); if (this.no >= this.tupleMaxbuy) { this.msgBox.text('本商品最多可购买:' + this.tupleMaxbuy + '件'); } if (this.tuple > 1 && this.no <= this.tupleMinbuy) { this.msgBox.text('本商品最少买数为:' + this.tupleMinbuy + '件'); } }; $.fn.quantity = function (opt) { return new Quantity(opt); };}(jQuery));$(function () { var quantity = $('.num-box').quantity({ cutBtn: '.num-cut', addBtn: '.num-add', numBox: '.num-number', msgBox: '.det-msg' }); quantity.setTuple(1); quantity.setMinbuy(1); quantity.setMaxbuy(99); quantity.setStock(99);});
0 0
- jquery插件--增减数字
- 14、jQuery插件之jquery.spinner数字智能增减插件
- jQuery数字加减插件
- jQuery购物篮增减
- jquery动态增减行
- [jQuery][插件][tgAnimationLabel]数字滚动
- Jquery-animateNumber数字动画插件
- jquery-animateNumber数字动画插件
- jquery动态增减选项卡
- 【jQuery插件】jquery.onlypressnum.js 文本框只能输入数字插件
- 输入框中数字的增减实现
- Jquery实现数字微调 FineTuning 小插件
- autoNumeric网页数字格式化Jquery插件
- jquery.spinner数字智能加减插件
- 动态数字时钟jquery拓展小插件
- jquery 输入框动态增减的代码
- 共享一个小技巧。文本框增减数字的方法。
- 限制输入框只能输入数字(JQuery插件)
- 欢迎使用CSDN-markdown编辑器
- Java简单笔试题(二)
- 基于文本服务框架的拼音输入法研究与实现
- 20170307-leetcode-263-Ugly Number
- C++ new delete new[] delete[]重载实现
- jquery插件--增减数字
- 初步学习,简单问题:No resource found that matches the given name (at 'background' with value '@drawable/ con
- C语言宏中"#"和"##"的用法
- 增加swap空间大小
- System.ArgumentException 位于 System.Windows.Forms.dll 源对象不公开“IPropertyNotifySink”事件接口
- 程序员创业注意事项
- 深入理解Java:注解(Annotation)自定义注解入门
- 線性內插(Interpolation)
- NFS的一个BUG