首页  >  文章  >  web前端  >  jquery实现pc端滑动验证

jquery实现pc端滑动验证

韦小宝
韦小宝原创
2017-11-21 10:39:492519浏览

jquery实现pc端滑动验证这个插件可以放在我们的项目中使用,对jquery有兴趣的朋友可以研究研究,免费提供源码~这也是我们学习jquery的一种方法。

Q4O728Z6$6]L{XG$D~PV7V4.png

代码:

<!DOCTYPE html>
<html>
<head>

<meta charset="utf-8">
<title>PHP中文网</title>
<style type="text/css">
	#drag{ 
    position: relative;
    background-color: #e8e8e8;
    width: 300px;
    height: 34px;
    line-height: 34px;
    text-align: center;
}
#drag .handler{
    position: absolute;
    top: 0px;
    left: 0px;
    width: 40px;
    height: 32px;
    border: 1px solid #ccc;
    cursor: move;
}
.handler_bg{
    background: #fff url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAA3hpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuNS1jMDIxIDc5LjE1NTc3MiwgMjAxNC8wMS8xMy0xOTo0NDowMCAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wTU09Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9tbS8iIHhtbG5zOnN0UmVmPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvc1R5cGUvUmVzb3VyY2VSZWYjIiB4bWxuczp4bXA9Imh0dHA6Ly9uc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Q9InhtcC5kaWQ6NGQ4ZTVmOTMtOTZiNC00ZTVkLThhY2ItN2U2ODhmMjE1NmU2Ii8+IDwvcmRmOkRlc2NyaXB0aW9uPiA8L3JkZjpSREY+IDwveDp4bXBtZXRhPiA8P3hwYWNrZXQgZW5kPSJyIj8+k+sHwwAAASZJREFUeNpi/P//PwMyKD8uZw+kUoDYEYgloMIvgHg/EM/ptHx0EFk9I8wAoEZ+IDUPiIMY8IN1QJwENOgj3ACo5gNAbMBAHLgAxA4gQ5igAnNJ0MwAVTsX7IKyY7L2UNuJAf+AmAmJ78AEDTBiwGYg5gbifCSxFCZoaBMCy4A4GOjnH0D6DpK4IxNSVIHAfSDOAeLraJrjgJp/AwPbHMhejiQnwYRmUzNQ4VQgDQqXK0ia/0I17wJiPmQNTNBEAgMlQIWiQA2vgWw7QppBekGxsAjIiEUSBNnsBDWEAY9mEFgMMgBk00E0iZtA7AHEctDQ58MRuA6wlLgGFMoMpIG1QFeGwAIxGZo8GUhIysmwQGSAZgwHaEZhICIzOaBkJkqyM0CAAQDGx279Jf50AAAAAABJRU5ErkJggg==") no-repeat center;
}
#drag .drag_bg{
    background-color: #7ac23c;
    height: 34px;
    width: 0px;
}
#drag .drag_text{
    position: absolute;
    top: 0px;
    width: 300px;
    -moz-user-select: none;
    -webkit-user-select: none;
    user-select: none;
    -o-user-select:none;
    -ms-user-select:none; 
}
</style>
</head>
<body>

<center><br><br><br><div id="drag"></div></center>


<script src="http://www.lanrenzhijia.com/ajaxjs/jquery.min.js"></script>

<script type="text/javascript">


/* 
 * drag 1.0
 * create by tony@jentian.com
 * date 2015-08-18
 * 拖动滑块
 */
(function($){
    $.fn.drag = function(options){
        var x, drag = this, isMove = false, defaults = {
        };
        var options = $.extend(defaults, options);
        //添加背景,文字,滑块
        var html = &#39;<div class="drag_bg"></div>&#39;+
                    &#39;<div class="drag_text" onselectstart="return false;" unselectable="on">拖动滑块验证</div>&#39;+
                    &#39;<div class="handler handler_bg"></div>&#39;;
        this.append(html);
        
        var handler = drag.find(&#39;.handler&#39;);
        var drag_bg = drag.find(&#39;.drag_bg&#39;);
        var text = drag.find(&#39;.drag_text&#39;);
        var maxWidth = drag.width() - handler.width();  //能滑动的最大间距
        
        //鼠标按下时候的x轴的位置
        handler.mousedown(function(e){
            isMove = true;
            x = e.pageX - parseInt(handler.css(&#39;left&#39;), 10);
        });
        
        //鼠标指针在上下文移动时,移动距离大于0小于最大间距,滑块x轴位置等于鼠标移动距离
        $(document).mousemove(function(e){
            var _x = e.pageX - x;
            if(isMove){
                if(_x > 0 && _x <= maxWidth){
                    handler.css({&#39;left&#39;: _x});
                    drag_bg.css({&#39;width&#39;: _x});
                }else if(_x > maxWidth){  //鼠标指针移动距离达到最大时清空事件
                    dragOk();
                }
            }
        }).mouseup(function(e){
            isMove = false;
            var _x = e.pageX - x;
            if(_x < maxWidth){ //鼠标松开时,如果没有达到最大距离位置,滑块就返回初始位置
                handler.css({&#39;left&#39;: 0});
                drag_bg.css({&#39;width&#39;: 0});
            }
        });
        
        //清空事件
        function dragOk(){
            handler.removeClass(&#39;handler_bg&#39;).addClass(&#39;handler_ok_bg&#39;);
            text.text(&#39;验证通过&#39;);
            drag.css({&#39;color&#39;: &#39;#fff&#39;});
            handler.unbind(&#39;mousedown&#39;);
            $(document).unbind(&#39;mousemove&#39;);
            $(document).unbind(&#39;mouseup&#39;);
        }
    };
})(jQuery);


$(&#39;#drag&#39;).drag();
</script>


</div>
</body>
</html>

以上就是jquery实现pc端滑动验证的源码了,有兴趣想了解更多可以到PHP中文网首页搜索哦!

相关推荐:

jquery手风琴焦点动画

jquery标题选择动画

jQuery实现div跟随鼠标移动的代码案例

以上是jquery实现pc端滑动验证的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn