From 1b5c0b4fcc5dc3dbc269a2c9acc0b378746b3caf Mon Sep 17 00:00:00 2001 From: sight <1453017105@qq.com> Date: Sun, 10 Dec 2023 19:37:40 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=20slider=20=E7=A7=BB?= =?UTF-8?q?=E5=8A=A8=E7=AB=AF=E6=BB=91=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/slider.js | 149 ++++++++++++++++++++++++------------------ 1 file changed, 86 insertions(+), 63 deletions(-) diff --git a/src/modules/slider.js b/src/modules/slider.js index a833d6ef..ea0dadd7 100644 --- a/src/modules/slider.js +++ b/src/modules/slider.js @@ -10,26 +10,26 @@ layui.define(['jquery', 'lay'], function(exports){ // 外部接口 var slider = { - config: {} - ,index: layui.slider ? (layui.slider.index + 10000) : 0 + config: {}, + index: layui.slider ? (layui.slider.index + 10000) : 0, // 设置全局项 - ,set: function(options){ + set: function(options){ var that = this; that.config = $.extend({}, that.config, options); return that; - } + }, // 事件 - ,on: function(events, callback){ + on: function(events, callback){ return layui.onevent.call(this, MOD_NAME, events, callback); } }; // 操作当前实例 var thisSlider = function(){ - var that = this - ,options = that.config; + var that = this; + var options = that.config; return { setValue: function(value, index){ // 设置值 @@ -37,8 +37,8 @@ layui.define(['jquery', 'lay'], function(exports){ value = value < options.min ? options.min : value; options.value = value; return that.slide('set', value, index || 0); - } - ,config: options + }, + config: options } }; @@ -65,18 +65,18 @@ layui.define(['jquery', 'lay'], function(exports){ // 默认配置 Class.prototype.config = { - type: 'default' //滑块类型,垂直:vertical - ,min: 0 //最小值 - ,max: 100 //最大值,默认100 - ,value: 0 //初始值,默认为0 - ,step: 1 //间隔值 - ,showstep: false //间隔点开启 - ,tips: true //文字提示,开启 - ,input: false //输入框,关闭 - ,range: false //范围选择,与输入框不能同时开启,默认关闭 - ,height: 200 //配合 type:"vertical" 使用,默认200px - ,disabled: false //滑块禁用,默认关闭 - ,theme: '#16baaa' //主题颜色 + type: 'default', //滑块类型,垂直:vertical + min: 0, //最小值 + max: 100, //最大值,默认100 + value: 0, //初始值,默认为0 + step: 1, //间隔值 + showstep: false, //间隔点开启 + tips: true, //文字提示,开启 + input: false, //输入框,关闭 + range: false, //范围选择,与输入框不能同时开启,默认关闭 + height: 200, //配合 type:"vertical" 使用,默认200px + disabled: false, //滑块禁用,默认关闭 + theme: '#16baaa' //主题颜色 }; //滑块渲染 @@ -116,9 +116,9 @@ layui.define(['jquery', 'lay'], function(exports){ options.value[0] = Math.min(options.value[0],options.max); options.value[1] = Math.min(options.value[1],options.max); - var scaleFir = Math.floor((options.value[0] - options.min) / (options.max - options.min) * 100) - ,scaleSec = Math.floor((options.value[1] - options.min) / (options.max - options.min) * 100) - ,scale = scaleSec - scaleFir + '%'; + var scaleFir = Math.floor((options.value[0] - options.min) / (options.max - options.min) * 100); + var scaleSec = Math.floor((options.value[1] - options.min) / (options.max - options.min) * 100); + var scale = scaleSec - scaleFir + '%'; scaleFir = scaleFir + '%'; scaleSec = scaleSec + '%'; } else { @@ -143,8 +143,8 @@ layui.define(['jquery', 'lay'], function(exports){ '
' + '
'+ (options.range ? '
' : '') +''; - var othis = $(options.elem) - ,hasRender = othis.next('.' + ELEM_VIEW); + var othis = $(options.elem); + var hasRender = othis.next('.' + ELEM_VIEW); //生成替代元素 hasRender[0] && hasRender.remove(); //如果已经渲染,则Rerender that.elemTemp = $(temp); @@ -204,12 +204,12 @@ layui.define(['jquery', 'lay'], function(exports){ //划过滑块显示数值 var timer; that.elemTemp.find('.' + SLIDER_WRAP_BTN).on('mouseover', function(){ - var sliderWidth = options.type === 'vertical' ? options.height : that.elemTemp[0].offsetWidth - ,sliderWrap = that.elemTemp.find('.' + SLIDER_WRAP) - ,tipsLeft = options.type === 'vertical' ? (sliderWidth - $(this).parent()[0].offsetTop - sliderWrap.height()) : $(this).parent()[0].offsetLeft - ,left = tipsLeft / sliderWidth * 100 - ,value = $(this).parent().data('value') - ,tipsTxt = options.setTips ? options.setTips(value) : value; + var sliderWidth = options.type === 'vertical' ? options.height : that.elemTemp[0].offsetWidth; + var sliderWrap = that.elemTemp.find('.' + SLIDER_WRAP); + var tipsLeft = options.type === 'vertical' ? (sliderWidth - $(this).parent()[0].offsetTop - sliderWrap.height()) : $(this).parent()[0].offsetLeft; + var left = tipsLeft / sliderWidth * 100; + var value = $(this).parent().data('value'); + var tipsTxt = options.setTips ? options.setTips(value) : value; that.elemTemp.find('.' + SLIDER_TIPS).html(tipsTxt); clearTimeout(timer); @@ -235,17 +235,17 @@ layui.define(['jquery', 'lay'], function(exports){ //滑块滑动 Class.prototype.slide = function(setValue, value, i){ - var that = this - ,options = that.config - ,sliderAct = that.elemTemp - ,sliderWidth = function(){ + var that = this; + var options = that.config; + var sliderAct = that.elemTemp; + var sliderWidth = function(){ return options.type === 'vertical' ? options.height : sliderAct[0].offsetWidth - } - ,sliderWrap = sliderAct.find('.' + SLIDER_WRAP) - ,sliderTxt = sliderAct.next('.' + SLIDER_INPUT) - ,inputValue = sliderTxt.children('.' + SLIDER_INPUT_TXT).children('input').val() - ,step = 100 / ((options.max - options.min) / Math.ceil(options.step)) - ,change = function(offsetValue, index, from){ + }; + var sliderWrap = sliderAct.find('.' + SLIDER_WRAP); + var sliderTxt = sliderAct.next('.' + SLIDER_INPUT); + var inputValue = sliderTxt.children('.' + SLIDER_INPUT_TXT).children('input').val(); + var step = 100 / ((options.max - options.min) / Math.ceil(options.step)); + var change = function(offsetValue, index, from){ if(Math.ceil(offsetValue) * step > 100){ offsetValue = Math.ceil(offsetValue) * step }else{ @@ -254,8 +254,8 @@ layui.define(['jquery', 'lay'], function(exports){ offsetValue = offsetValue > 100 ? 100: offsetValue; offsetValue = offsetValue < 0 ? 0: offsetValue; sliderWrap.eq(index).css((options.type === 'vertical' ?'bottom':'left'), offsetValue + '%'); - var firLeft = valueTo(sliderWrap[0].offsetLeft) - ,secLeft = options.range ? valueTo(sliderWrap[1].offsetLeft) : 0; + var firLeft = valueTo(sliderWrap[0].offsetLeft); + var secLeft = options.range ? valueTo(sliderWrap[1].offsetLeft) : 0; if(options.type === 'vertical'){ sliderAct.find('.' + SLIDER_TIPS).css({"bottom":offsetValue + '%', "margin-bottom":"20px"}); firLeft = valueTo(sliderWidth() - sliderWrap[0].offsetTop - sliderWrap.height()); @@ -281,8 +281,8 @@ layui.define(['jquery', 'lay'], function(exports){ //如果开启范围选择,则返回数组值 if(options.range){ var arrValue = [ - sliderWrap.eq(0).data('value') - ,sliderWrap.eq(1).data('value') + sliderWrap.eq(0).data('value'), + sliderWrap.eq(1).data('value') ]; if(arrValue[0] > arrValue[1]) arrValue.reverse(); //如果前面的圆点超过了后面的圆点值,则调换顺序 } @@ -292,27 +292,40 @@ layui.define(['jquery', 'lay'], function(exports){ // 值完成选中的事件 if(from === 'done') options.done && options.done(that.value); - } - ,valueTo = function(value){ - var oldLeft = value / sliderWidth() * 100 / step - ,left = Math.round(oldLeft) * step; + }; + var valueTo = function(value){ + var oldLeft = value / sliderWidth() * 100 / step; + var left = Math.round(oldLeft) * step; if(value == sliderWidth()){ left = Math.ceil(oldLeft) * step; } return left; - } + }; //拖拽元素 - ,elemMove = $(['
sliderWidth())left = sliderWidth(); @@ -343,12 +364,14 @@ layui.define(['jquery', 'lay'], function(exports){ e.preventDefault(); }; - var up = function(){ + var up = function(delay){ othis.removeClass(ELEM_HOVER); - sliderAct.find('.' + SLIDER_TIPS).hide(); + setTimeout(function(){ + sliderAct.find('.' + SLIDER_TIPS).hide(); + }, delay); }; - createMoveElem(move, up) + createMoveElem(othis, move, up) }); }); @@ -417,8 +440,8 @@ layui.define(['jquery', 'lay'], function(exports){ //事件处理 Class.prototype.events = function(){ - var that = this - ,options = that.config; + var that = this; + var options = that.config; }; //核心入口