<div class="layui-sliderVerify">
<div id="j_sliderVerify" class="slider"></div>
<input type="text" name="randCode" id="LAY-user-login-sliderVerify"
lay-verify="loginSilder" placeholder="滑块验证码"
class="layui-input hide">
</div>
//调用代码
$("#j_sliderVerify").slider({
width: 307, // width
height: 30, // height
sliderBg: "#F6F6F6", // 滑块背景颜色
color: "#989898", // 文字颜色
fontSize: 14, // 文字大小
bgColor: "#F6F6F6", // 背景颜色
textMsg: "请按住滑块,拖动到最右边", // 提示文字
successMsg: "验证通过", // 验证成功提示文字
successColor: "#fff", // 滑块验证成功提示文字颜色
time: 400, // 返回时间
callback: function(result) { // 回调函数,true(成功),false(失败)
if (result) {
//成功代码
$bw.bwUtil.req({
url: $bw.setter.apis + '/jssbdSysUser/getLoginCode.do',
selfDialog: true,
success: function (res) {
if (res.code == 0) {
$("#LAY-user-login-sliderVerify").val(res.data);
window.uuid = res.msg;
} else {
restoreSlider();
}
},
fail: function(e,code){
if(e.status != 200){
restoreSlider();
}
// 还原
// $("#j_sliderVerify").slider("restore");
}
});
}
}
});
function restoreSlider(){
$("#LAY-user-login-sliderVerify").val('');
$("#j_sliderVerify").slider("restore");
$bw.bwUtil.msg('滑块验证失败');
}
插件js
//插件代码
(function(t, i, s, e) {
var l = function(i, s) {
this.ele = i;
this.defaults = {
width: 307,
height: 30,
sliderBg: "#F6F6F6",
color: "#989898",
fontSize: 14,
bgColor: "#F6F6F6",
textMsg: "请按住滑块,拖动到最右边",
successMsg: "验证成功",
successColor: "#fff",
time: 160,
callback: function(t) {}
};
this.opts = t.extend({}, this.defaults, s);
this.init()
};
l.prototype = {
init: function() {
this.result = false;
this.sliderBtn_left = 0;
this.maxLeft = this.defaults.width - this.defaults.height;
this.render();
this.eventBind()
},
render: function() {
var t = '<div class="ui-slider-wrap">' + '<div class="ui-slider-text ui-slider-no-select">' + this.opts.textMsg + "</div>" + '<div class="ui-slider-btn init ui-slider-no-select"></div>' + '<div class="ui-slider-bg"></div>' + "</div>";
this.ele.html(t);
this.initStatus()
},
initStatus: function() {
var t = this;
var i = this.ele;
this.slider = i.find(".ui-slider-wrap");
this.sliderBtn = i.find(".ui-slider-btn");
this.bgColor = i.find(".ui-slider-bg");
this.sliderText = i.find(".ui-slider-text");
this.slider.css({
width: t.opts.width,
height: t.opts.height,
backgroundColor: t.opts.sliderBg
});
this.sliderBtn.css({
width: t.opts.height,
height: t.opts.height,
lineHeight: t.opts.height + "px"
});
this.bgColor.css({
height: t.opts.height,
backgroundColor: '#7AC14C'
});
this.sliderText.css({
lineHeight: t.opts.height + "px",
fontSize: t.opts.fontSize,
color: t.opts.color
})
},
restore: function() {
var t = this;
var i = t.opts.time;
this.result = false;
this.initStatus();
this.sliderBtn.removeClass("success").animate({
left: 0
}, i);
this.bgColor.animate({
width: 0
}, i, function() {
t.sliderText.text(t.opts.textMsg)
})
},
eventBind: function() {
var t = this;
this.ele.on("mousedown", ".ui-slider-btn", function(i) {
if (t.result) {
return
}
t.sliderMousedown(i)
})
},
sliderMousedown: function(t) {
var i = this;
var s = t.clientX;
var e = s - this.sliderBtn.offset().left;
i.sliderMousemove(s, e);
i.sliderMouseup()
},
sliderMousemove: function(i, e) {
var l = this;
t(s).on("mousemove.slider", function(t) {
l.sliderBtn_left = t.clientX - i - e;
if (l.sliderBtn_left < 0) {
return
}
if (l.sliderBtn_left > l.maxLeft) {
l.sliderBtn_left = l.maxLeft
}
l.sliderBtn.css("left", l.sliderBtn_left);
l.bgColor.width(l.sliderBtn_left)
})
},
sliderMouseup: function() {
var i = this;
t(s).on("mouseup.slider", function() {
if (i.sliderBtn_left != i.maxLeft) {
i.sliderBtn_left = 0
} else {
i.ele.find(".ui-slider-text").text(i.opts.successMsg).css({
color: i.opts.successColor
});
i.ele.find(".ui-slider-btn").addClass("success");
i.result = true
}
i.sliderBtn.animate({
left: i.sliderBtn_left
}, i.opts.time);
i.bgColor.animate({
width: i.sliderBtn_left
}, i.opts.time);
t(this).off("mousemove.slider mouseup.slider");
if (i.opts.callback && typeof i.opts.callback === "function") {
i.opts.callback(i.result)
}
})
}
};
t.fn.slider = function(i) {
return this.each(function() {
var s = t(this);
var e = s.data("slider");
if (!e) {
e = new l(s, i);
s.data("slider", e)
}
if (typeof i === "string") {
e[i]()
}
})
}
})(jQuery, window, document);