滑動驗證碼

本身研究: jQuery拖拽滑動驗證碼插件 slideunlock.jsjavascript

 

原理:(別人說)css

響應時間,拖拽速度,時間,位置,軌跡,重試次數等。
這些因素可以構成一個採樣結果或者辨識特性。html

 

只獲取到滑動時間,滑動的長度。java

 

效果:jquery

 

 

 

html頁面web

 

<link href="css/slide-unlock.css" rel="stylesheet">
<script type="text/javascript" src="js/jquery-2.0.2.min.js"></script>ide

<div id="slider">this

<div id="slider_bg"></div>spa

<span id="label">>></span> <span id="labelTip">拖動滑塊驗證</span>prototype

</div>

<script>

var startTime = 0;

    var endTime = 0;

    var numTime = 0;

    $(function () {

             var slider = new SliderUnlock("#slider",{

                 successLabelTip : "歡迎註冊"

        },function(){

var  sli_width = $("#slider_bg").width();

            alert("驗證成功");

            endTime = nowTime();

            numTime = endTime-startTime;

            endTime = 0;

            startTime = 0;

                            // 獲取到滑動使用的時間 滑動的寬度

            alert( numTime );

            alert( sli_width );

});

             slider.init();

})

/**

 * 獲取時間精確到毫秒

* @type

 */

function nowTime(){

var myDate = new Date();

var H = myDate.getHours();//獲取小時

var M = myDate.getMinutes(); //獲取分鐘

var S = myDate.getSeconds();//獲取秒

var MS = myDate.getMilliseconds();//獲取毫秒

var milliSeconds = H * 3600 * 1000 + M * 60 * 1000 + S * 1000 + MS;

return milliSeconds;

}

</script>

<script src="js/jquery.slideunlock.js"></script>

 

css樣式

 


#slider {
margin: 10px 5%;
width: 90%;
height: 40px;
position: relative;
border-radius: 8px;
background-color: #dae2d0;
overflow: hidden;
text-align: center;
user-select: none;
-moz-user-select: none;
-webkit-user-select: none;
}

#slider_bg {
position: absolute;
left: 0;
top: 0;
height: 100%;
background-color: #7AC23C;
z-index: 1;
}

#label {
width: 46px;
position: absolute;
left: 0;
top: 0;
height: 38px;
line-height: 38px;
border: 1px solid #cccccc;
background: #fff;
z-index: 3;
cursor: move;
color: #ff9e77;
font-size: 16px;
font-weight: 900;
}

#labelTip {
position: absolute;
left: 0;
width: 100%;
height: 100%;
font-size: 13px;
font-family: 'Microsoft Yahei', serif;
color: #787878;
line-height: 38px;
text-align: center;
z-index: 2;
}


js效果


/**
* jquery plugin -- jquery.slideunlock.js
* www.sucaijiayuan.com
* created: March 27, 2016
*/

;(function ($,window,document,undefined) {
function SliderUnlock(elm, options, success){
var me = this;
var $elm = me.checkElm(elm) ? $(elm) : $;
success = me.checkFn(success) ? success : function(){};

var opts = {
successLabelTip: "Successfully Verified",
duration: 200,
swipestart: false,
min: 0,
max: $elm.width(),
index: 0,
IsOk: false,
lableIndex: 0
};

opts = $.extend(opts, options||{});

//$elm
me.elm = $elm;
//opts
me.opts = opts;
//是否開始滑動
me.swipestart = opts.swipestart;
//最小值
me.min = opts.min;
//最大值
me.max = opts.max;
//當前滑動條所處的位置
me.index = opts.index;
//是否滑動成功
me.isOk = opts.isOk;
//滑塊寬度
me.labelWidth = me.elm.find('#label').width();
//滑塊背景
me.sliderBg = me.elm.find('#slider_bg');
//鼠標在滑動按鈕的位置
me.lableIndex = opts.lableIndex;
//success
me.success = success;
}

SliderUnlock.prototype.init = function () {
var me = this;

me.updateView();
me.elm.find("#label").on("mousedown", function (event) {
var e = event || window.event;
me.lableIndex = e.clientX - this.offsetLeft;
me.handerIn();
}).on("mousemove", function (event) {
me.handerMove(event);
}).on("mouseup", function (event) {
me.handerOut();
}).on("mouseout", function (event) {
me.handerOut();
}).on("touchstart", function (event) {
var e = event || window.event;
me.lableIndex = e.originalEvent.touches[0].pageX - this.offsetLeft;
me.handerIn();
}).on("touchmove", function (event) {
me.handerMove(event, "mobile");
}).on("touchend", function (event) {
me.handerOut();
});
};

/**
* 鼠標/手指接觸滑動按鈕
*/
SliderUnlock.prototype.handerIn = function () {
var me = this;
me.swipestart = true;

var myDate = new Date();
var H = myDate.getHours();//獲取小時
var M = myDate.getMinutes(); //獲取分鐘
var S = myDate.getSeconds();//獲取秒
var MS = myDate.getMilliseconds();//獲取毫秒
var milliSeconds = H * 3600 * 1000 + M * 60 * 1000 + S * 1000 + MS;

startTime = milliSeconds;


me.min = 0;
me.max = me.elm.width();
};

/**
* 鼠標/手指移出
*/
SliderUnlock.prototype.handerOut = function () {
var me = this;
//中止
me.swipestart = false;

weizhi = me.labelWidth;

//me.move();
if (me.index < me.max) {
me.reset();
}
};

/**
* 鼠標/手指移動
* @param event
* @param type
*/
SliderUnlock.prototype.handerMove = function (event, type) {
var me = this;
if (me.swipestart) {
event.preventDefault();
event = event || window.event;
if (type == "mobile") {
me.index = event.originalEvent.touches[0].pageX - me.lableIndex;
} else {
me.index = event.clientX - me.lableIndex;
}
me.move();
}
};

/**
* 鼠標/手指移動過程
*/
SliderUnlock.prototype.move = function () {
var me = this;
if ((me.index + me.labelWidth) >= me.max) {
me.index = me.max - me.labelWidth -2;
//中止
me.swipestart = false;
//解鎖
me.isOk = true;
}
if (me.index < 0) {
me.index = me.min;
//未解鎖
me.isOk = false;
}
if (me.index+me.labelWidth+2 == me.max && me.max > 0 && me.isOk) {
//解鎖默認操做
$('#label').unbind().next('#labelTip').
text(me.opts.successLabelTip).css({'color': '#fff'});

me.success();
}
me.updateView();
};


/**
* 更新視圖
*/
SliderUnlock.prototype.updateView = function () {
var me = this;

me.sliderBg.css('width', me.index);
me.elm.find("#label").css("left", me.index + "px")
};

/**
* 重置slide的起點
*/
SliderUnlock.prototype.reset = function () {
var me = this;

startTime = 0;

me.index = 0;
me.sliderBg .animate({'width':0},me.opts.duration);
me.elm.find("#label").animate({left: me.index}, me.opts.duration)
.next("#lableTip").animate({opacity: 1}, me.opts.duration);
me.updateView();
};

/**
* 檢測元素是否存在
* @param elm
* @returns {boolean}
*/
SliderUnlock.prototype.checkElm = function (elm) {
if($(elm).length > 0){
return true;
}else{
throw "this element does not exist.";
}
};

/**
* 檢測傳入參數是不是function
* @param fn
* @returns {boolean}
*/
SliderUnlock.prototype.checkFn = function (fn) {
if(typeof fn === "function"){
return true;
}else{
throw "the param is not a function.";
}
};


window['SliderUnlock'] = SliderUnlock;
})(jQuery, window, document);





 

 

兩個連接

https://www.zhihu.com/question/32209043/answer/55252171

http://www.zhihu.com/question/35538123

相關文章
相關標籤/搜索