slider.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /**
  2. @Title: layui.slider 滑块
  3. @Author: star1029
  4. @License:MIT
  5. */
  6. layui.define('jquery', function(exports){
  7. "use strict";
  8. var $ = layui.jquery
  9. //外部接口
  10. ,slider = {
  11. config: {}
  12. ,index: layui.slider ? (layui.slider.index + 10000) : 0
  13. //设置全局项
  14. ,set: function(options){
  15. var that = this;
  16. that.config = $.extend({}, that.config, options);
  17. return that;
  18. }
  19. //事件监听
  20. ,on: function(events, callback){
  21. return layui.onevent.call(this, MOD_NAME, events, callback);
  22. }
  23. }
  24. //操作当前实例
  25. ,thisSlider = function(){
  26. var that = this
  27. ,options = that.config;
  28. return {
  29. setValue: function(value, index){ //设置值
  30. options.value = value;
  31. return that.slide('set', value, index || 0);
  32. }
  33. ,config: options
  34. }
  35. }
  36. //字符常量
  37. ,MOD_NAME = 'slider', DISABLED = 'layui-disabled', ELEM_VIEW = 'layui-slider', SLIDER_BAR = 'layui-slider-bar', SLIDER_WRAP = 'layui-slider-wrap', SLIDER_WRAP_BTN = 'layui-slider-wrap-btn', SLIDER_TIPS = 'layui-slider-tips', SLIDER_INPUT = 'layui-slider-input', SLIDER_INPUT_TXT = 'layui-slider-input-txt', SLIDER_INPUT_BTN = 'layui-slider-input-btn', ELEM_HOVER = 'layui-slider-hover'
  38. //构造器
  39. ,Class = function(options){
  40. var that = this;
  41. that.index = ++slider.index;
  42. that.config = $.extend({}, that.config, slider.config, options);
  43. that.render();
  44. };
  45. //默认配置
  46. Class.prototype.config = {
  47. type: 'default' //滑块类型,垂直:vertical
  48. ,min: 0 //最小值
  49. ,max: 100 //最大值,默认100
  50. ,value: 0 //初始值,默认为0
  51. ,step: 1 //间隔值
  52. ,showstep: false //间隔点开启
  53. ,tips: true //文字提示,开启
  54. ,input: false //输入框,关闭
  55. ,range: false //范围选择,与输入框不能同时开启,默认关闭
  56. ,height: 200 //配合 type:"vertical" 使用,默认200px
  57. ,disabled: false //滑块禁用,默认关闭
  58. ,theme: '#009688' //主题颜色
  59. };
  60. //滑块渲染
  61. Class.prototype.render = function(){
  62. var that = this
  63. ,options = that.config;
  64. //间隔值不能小于 1
  65. if(options.step < 1) options.step = 1;
  66. //最大值不能小于最小值
  67. if(options.max < options.min) options.max = options.min + options.step;
  68. //判断是否开启双滑块
  69. if(options.range){
  70. options.value = typeof(options.value) == 'object' ? options.value : [options.min, options.value];
  71. var minValue = Math.min(options.value[0], options.value[1])
  72. ,maxValue = Math.max(options.value[0], options.value[1]);
  73. options.value[0] = minValue > options.min ? minValue : options.min;
  74. options.value[1] = maxValue > options.min ? maxValue : options.min;
  75. options.value[0] = options.value[0] > options.max ? options.max : options.value[0];
  76. options.value[1] = options.value[1] > options.max ? options.max : options.value[1];
  77. var scaleFir = Math.floor((options.value[0] - options.min) / (options.max - options.min) * 100)
  78. ,scaleSec = Math.floor((options.value[1] - options.min) / (options.max - options.min) * 100)
  79. ,scale = scaleSec - scaleFir + '%';
  80. scaleFir = scaleFir + '%';
  81. scaleSec = scaleSec + '%';
  82. } else {
  83. //如果初始值是一个数组,则获取数组的最小值
  84. if(typeof options.value == 'object'){
  85. options.value = Math.min.apply(null, options.value);
  86. }
  87. //初始值不能小于最小值且不能大于最大值
  88. if(options.value < options.min) options.value = options.min;
  89. if(options.value > options.max) options.value = options.max;
  90. var scale = Math.floor((options.value - options.min) / (options.max - options.min) * 100) + '%';
  91. };
  92. //如果禁用,颜色为统一的灰色
  93. var theme = options.disabled ? '#c2c2c2' : options.theme;
  94. //滑块
  95. var temp = '<div class="layui-slider '+ (options.type === 'vertical' ? 'layui-slider-vertical' : '') +'">'+ (options.tips ? '<div class="layui-slider-tips"></div>' : '') +
  96. '<div class="layui-slider-bar" style="background:'+ theme +'; '+ (options.type === 'vertical' ? 'height' : 'width') +':'+ scale +';'+ (options.type === 'vertical' ? 'bottom' : 'left') +':'+ (scaleFir || 0) +';"></div><div class="layui-slider-wrap" style="'+ (options.type === 'vertical' ? 'bottom' : 'left') +':'+ (scaleFir || scale) +';">' +
  97. '<div class="layui-slider-wrap-btn" style="border: 2px solid '+ theme +';"></div></div>'+ (options.range ? '<div class="layui-slider-wrap" style="'+ (options.type === 'vertical' ? 'bottom' : 'left') +':'+ scaleSec +';"><div class="layui-slider-wrap-btn" style="border: 2px solid '+ theme +';"></div></div>' : '') +'</div>';
  98. var othis = $(options.elem)
  99. ,hasRender = othis.next('.' + ELEM_VIEW);
  100. //生成替代元素
  101. hasRender[0] && hasRender.remove(); //如果已经渲染,则Rerender
  102. that.elemTemp = $(temp);
  103. //把数据缓存到滑块上
  104. if(options.range){
  105. that.elemTemp.find('.' + SLIDER_WRAP).eq(0).data('value', options.value[0]);
  106. that.elemTemp.find('.' + SLIDER_WRAP).eq(1).data('value', options.value[1]);
  107. }else{
  108. that.elemTemp.find('.' + SLIDER_WRAP).data('value', options.value);
  109. };
  110. //插入替代元素
  111. othis.html(that.elemTemp);
  112. //垂直滑块
  113. if(options.type === 'vertical'){
  114. that.elemTemp.height(options.height + 'px');
  115. };
  116. //显示间断点
  117. if(options.showstep){
  118. var number = (options.max - options.min) / options.step, item = '';
  119. for(var i = 1; i < number + 1; i++) {
  120. var step = i * 100 / number;
  121. if(step < 100){
  122. item += '<div class="layui-slider-step" style="'+ (options.type === 'vertical' ? 'bottom' : 'left') +':'+ step +'%"></div>'
  123. }
  124. };
  125. that.elemTemp.append(item);
  126. };
  127. //插入输入框
  128. if(options.input && !options.range){
  129. var elemInput = $('<div class="layui-slider-input layui-input"><div class="layui-slider-input-txt"><input type="text" class="layui-input"></div><div class="layui-slider-input-btn"><i class="layui-icon layui-icon-up"></i><i class="layui-icon layui-icon-down"></i></div></div>');
  130. othis.css("position","relative");
  131. othis.append(elemInput);
  132. othis.find('.' + SLIDER_INPUT_TXT).children('input').val(options.value);
  133. if(options.type === 'vertical'){
  134. elemInput.css({
  135. left: 0
  136. ,top: -48
  137. });
  138. } else {
  139. that.elemTemp.css("margin-right", elemInput.outerWidth() + 15);
  140. }
  141. };
  142. //给未禁止的滑块滑动事件
  143. if(!options.disabled){
  144. that.slide();
  145. }else{
  146. that.elemTemp.addClass(DISABLED);
  147. that.elemTemp.find('.' + SLIDER_WRAP_BTN).addClass(DISABLED);
  148. };
  149. //划过滑块显示数值
  150. that.elemTemp.find('.' + SLIDER_WRAP_BTN).on('mouseover', function(){
  151. var sliderWidth = options.type === 'vertical' ? options.height : that.elemTemp[0].offsetWidth
  152. ,sliderWrap = that.elemTemp.find('.' + SLIDER_WRAP)
  153. ,tipsLeft = options.type === 'vertical' ? (sliderWidth - $(this).parent()[0].offsetTop - sliderWrap.height()) : $(this).parent()[0].offsetLeft
  154. ,left = tipsLeft / sliderWidth * 100
  155. ,value = $(this).parent().data('value')
  156. ,tipsTxt = options.setTips ? options.setTips(value) : value;
  157. that.elemTemp.find('.' + SLIDER_TIPS).html(tipsTxt);
  158. if(options.type === 'vertical'){
  159. that.elemTemp.find('.' + SLIDER_TIPS).css({"bottom":left + '%', "margin-bottom":"20px", "display":"inline-block"});
  160. }else{
  161. that.elemTemp.find('.' + SLIDER_TIPS).css({"left":left + '%', "display":"inline-block"});
  162. };
  163. }).on('mouseout', function(){
  164. that.elemTemp.find('.' + SLIDER_TIPS).css("display", "none");
  165. });
  166. };
  167. //滑块滑动
  168. Class.prototype.slide = function(setValue, value, i){
  169. var that = this
  170. ,options = that.config
  171. ,sliderAct = that.elemTemp
  172. ,sliderWidth = function(){
  173. return options.type === 'vertical' ? options.height : sliderAct[0].offsetWidth
  174. }
  175. ,sliderWrap = sliderAct.find('.' + SLIDER_WRAP)
  176. ,sliderTxt = sliderAct.next('.' + SLIDER_INPUT)
  177. ,inputValue = sliderTxt.children('.' + SLIDER_INPUT_TXT).children('input').val()
  178. ,step = 100 / ((options.max - options.min) / Math.ceil(options.step))
  179. ,change = function(offsetValue, index){
  180. if(Math.ceil(offsetValue) * step > 100){
  181. offsetValue = Math.ceil(offsetValue) * step
  182. }else{
  183. offsetValue = Math.round(offsetValue) * step
  184. };
  185. offsetValue = offsetValue > 100 ? 100: offsetValue;
  186. sliderWrap.eq(index).css((options.type === 'vertical' ?'bottom':'left'), offsetValue + '%');
  187. var firLeft = valueTo(sliderWrap[0].offsetLeft)
  188. ,secLeft = options.range ? valueTo(sliderWrap[1].offsetLeft) : 0;
  189. if(options.type === 'vertical'){
  190. sliderAct.find('.' + SLIDER_TIPS).css({"bottom":offsetValue + '%', "margin-bottom":"20px"});
  191. firLeft = valueTo(sliderWidth() - sliderWrap[0].offsetTop - sliderWrap.height());
  192. secLeft = options.range ? valueTo(sliderWidth() - sliderWrap[1].offsetTop - sliderWrap.height()) : 0;
  193. }else{
  194. sliderAct.find('.' + SLIDER_TIPS).css("left",offsetValue + '%');
  195. };
  196. firLeft = firLeft > 100 ? 100: firLeft;
  197. secLeft = secLeft > 100 ? 100: secLeft;
  198. var minLeft = Math.min(firLeft, secLeft)
  199. ,wrapWidth = Math.abs(firLeft - secLeft);
  200. if(options.type === 'vertical'){
  201. sliderAct.find('.' + SLIDER_BAR).css({"height":wrapWidth + '%', "bottom":minLeft + '%'});
  202. }else{
  203. sliderAct.find('.' + SLIDER_BAR).css({"width":wrapWidth + '%', "left":minLeft + '%'});
  204. };
  205. var selfValue = options.min + Math.round((options.max - options.min) * offsetValue / 100);
  206. inputValue = selfValue;
  207. sliderTxt.children('.' + SLIDER_INPUT_TXT).children('input').val(inputValue);
  208. sliderWrap.eq(index).data('value', selfValue);
  209. sliderAct.find('.' + SLIDER_TIPS).html(options.setTips ? options.setTips(selfValue) : selfValue);
  210. //如果开启范围选择,则返回数组值
  211. if(options.range){
  212. var arrValue = [
  213. sliderWrap.eq(0).data('value')
  214. ,sliderWrap.eq(1).data('value')
  215. ];
  216. if(arrValue[0] > arrValue[1]) arrValue.reverse(); //如果前面的圆点超过了后面的圆点值,则调换顺序
  217. }
  218. //回调
  219. options.change && options.change(options.range ? arrValue : selfValue);
  220. }
  221. ,valueTo = function(value){
  222. var oldLeft = value / sliderWidth() * 100 / step
  223. ,left = Math.round(oldLeft) * step;
  224. if(value == sliderWidth()){
  225. left = Math.ceil(oldLeft) * step;
  226. };
  227. return left;
  228. }
  229. //拖拽元素
  230. ,elemMove = $(['<div class="layui-auxiliar-moving" id="LAY-slider-moving"></div'].join(''))
  231. ,createMoveElem = function(move, up){
  232. var upCall = function(){
  233. up && up();
  234. elemMove.remove();
  235. };
  236. $('#LAY-slider-moving')[0] || $('body').append(elemMove);
  237. elemMove.on('mousemove', move);
  238. elemMove.on('mouseup', upCall).on('mouseleave', upCall);
  239. };
  240. //动态赋值
  241. if(setValue === 'set') return change(value, i);
  242. //滑块滑动
  243. sliderAct.find('.' + SLIDER_WRAP_BTN).each(function(index){
  244. var othis = $(this);
  245. othis.on('mousedown', function(e){
  246. e = e || window.event;
  247. var oldleft = othis.parent()[0].offsetLeft
  248. ,oldx = e.clientX;
  249. if(options.type === 'vertical'){
  250. oldleft = sliderWidth() - othis.parent()[0].offsetTop - sliderWrap.height()
  251. oldx = e.clientY;
  252. };
  253. var move = function(e){
  254. e = e || window.event;
  255. var left = oldleft + (options.type === 'vertical' ? (oldx - e.clientY) : (e.clientX - oldx));
  256. if(left < 0)left = 0;
  257. if(left > sliderWidth())left = sliderWidth();
  258. var reaLeft = left / sliderWidth() * 100 / step;
  259. change(reaLeft, index);
  260. othis.addClass(ELEM_HOVER);
  261. sliderAct.find('.' + SLIDER_TIPS).show();
  262. e.preventDefault();
  263. };
  264. var up = function(){
  265. othis.removeClass(ELEM_HOVER);
  266. sliderAct.find('.' + SLIDER_TIPS).hide();
  267. };
  268. createMoveElem(move, up)
  269. });
  270. });
  271. //点击滑块
  272. sliderAct.on('click', function(e){
  273. var main = $('.' + SLIDER_WRAP_BTN);
  274. if(!main.is(event.target) && main.has(event.target).length === 0 && main.length){
  275. var left = options.type === 'vertical' ? (sliderWidth() - e.clientY + $(this).offset().top):(e.clientX - $(this).offset().left), index;
  276. if(left < 0)left = 0;
  277. if(left > sliderWidth())left = sliderWidth();
  278. var reaLeft = left / sliderWidth() * 100 / step;
  279. if(options.range){
  280. if(options.type === 'vertical'){
  281. index = Math.abs(left - parseInt($(sliderWrap[0]).css('bottom'))) > Math.abs(left - parseInt($(sliderWrap[1]).css('bottom'))) ? 1 : 0;
  282. }else{
  283. index = Math.abs(left - sliderWrap[0].offsetLeft) > Math.abs(left - sliderWrap[1].offsetLeft) ? 1 : 0;
  284. }
  285. }else{
  286. index = 0;
  287. };
  288. change(reaLeft, index);
  289. e.preventDefault();
  290. }
  291. });
  292. //点击加减输入框
  293. sliderTxt.children('.' + SLIDER_INPUT_BTN).children('i').each(function(index){
  294. $(this).on('click', function(){
  295. inputValue = sliderTxt.children('.' + SLIDER_INPUT_TXT).children('input').val();
  296. if(index == 1){ //减
  297. inputValue = inputValue - options.step < options.min
  298. ? options.min
  299. : Number(inputValue) - options.step;
  300. }else{
  301. inputValue = Number(inputValue) + options.step > options.max
  302. ? options.max
  303. : Number(inputValue) + options.step;
  304. };
  305. var inputScale = (inputValue - options.min) / (options.max - options.min) * 100 / step;
  306. change(inputScale, 0);
  307. });
  308. });
  309. //获取输入框值
  310. var getInputValue = function(){
  311. var realValue = this.value;
  312. realValue = isNaN(realValue) ? 0 : realValue;
  313. realValue = realValue < options.min ? options.min : realValue;
  314. realValue = realValue > options.max ? options.max : realValue;
  315. this.value = realValue;
  316. var inputScale = (realValue - options.min) / (options.max - options.min) * 100 / step;
  317. change(inputScale, 0);
  318. };
  319. sliderTxt.children('.' + SLIDER_INPUT_TXT).children('input').on('keydown', function(e){
  320. if(e.keyCode === 13){
  321. e.preventDefault();
  322. getInputValue.call(this);
  323. }
  324. }).on('change', getInputValue);
  325. };
  326. //事件处理
  327. Class.prototype.events = function(){
  328. var that = this
  329. ,options = that.config;
  330. };
  331. //核心入口
  332. slider.render = function(options){
  333. var inst = new Class(options);
  334. return thisSlider.call(inst);
  335. };
  336. exports(MOD_NAME, slider);
  337. })