<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>jQuery仿Android锁屏图案应用插件DEMO演示</title>
<link href="css/patternLock.css" rel="stylesheet" type="text/css" />
<script src="js/jquery.min.js"></script>
<script src="js/patternLock.js"></script>
<style type="text/css">
#warp{width:600px; margin:auto;}
#warp div{margin-bottom:50px;}
</style>
</head>
<body>
<div id="warp">
<h1>图案锁实例</h1>
<p>1、基础初始化</p>
<div id="patternContainer"></div>
<p>2、两点间到达目标点才画线</p>
<div id="patternContainer1"></div>
<p>3、当图案连线完成后才显示连线</p>
<div id="patternContainer2"></div>
<p>4、改变两个点之间的距</p>
<div id="patternContainer3"></div>
<p>5、自定义距阵</p>
<div id="patternContainer4"></div>
<p>6、使用映射</p>
<div id="patternContainer5"></div>
<p>7、作为验证码使用,这个在服务端需用到 patternCaptha,有兴趣的朋友可以研究下,这里就不多说了。</p>
<div id="patternContainer6"></div>
</div>
</body>
</html>
<script>
$(function(){
var lock = new PatternLock("#patternContainer"); var lock1 = new PatternLock("#patternContainer1",{lineOnMove:false}); var lock2 = new PatternLock("#patternContainer2",{patternVisible:false}); var lock3 = new PatternLock("#patternContainer3",{radius:30,margin:20}); var lock4 = new PatternLock("#patternContainer4",{matrix:[4,4]}); var lock5 = new PatternLock("#patternContainer5",
{
mapper: function(idx){
return (idx%9) + 1;
}
}); }) </script>

CSS

/*
patternLock.js v 0.2.0
Author: Sudhanshu Yadav
Copyright (c) 2013 Sudhanshu Yadav - ignitersworld.com , released under the MIT license.
Demo on: ignitersworld.com/lab/patternLock.html
*/ .patt-holder{background:#3382c0;}
.patt-wrap{position:relative; cursor:pointer;}
.patt-wrap ul, .patt-wrap li{
list-style: none;
margin:;
padding:;
}
.patt-circ{
position:relative;
float: left;
box-sizing: border-box;
-moz-box-sizing: border-box;
}
.patt-circ.hovered{
border:3px solid #009900;
} .patt-error .patt-circ.hovered{
border:3px solid #BA1B26;
} .patt-hidden .patt-circ.hovered{border:;} .patt-dots{
background: #FFF;
width: 10px;height: 10px;
border-radius:5px;
position:absolute;
top:50%;
left:50%;
margin-top:-5px;
margin-left:-5px;
}
.patt-lines{
border-radius:5px;
height:10px;
background:rgba(255,255,255,.7);
position:absolute;
transform-origin:5px 5px;
-ms-transform-origin:5px 5px; /* IE 9 */
-webkit-transform-origin:5px 5px;
} .patt-hidden .patt-lines{
display:none;
}

JS

/*
patternLock.js v 0.2.0
Author: Sudhanshu Yadav
Copyright (c) 2013 Sudhanshu Yadav - ignitersworld.com , released under the MIT license.
Demo on: ignitersworld.com/lab/patternLock.html
*/
(function ($, window, document, undefined) {
"use strict"; var isTouchSupported = !!('ontouchstart' in window),
touchStart = isTouchSupported ? "touchstart" : "mousedown",
touchEnd = isTouchSupported ? "touchend" : "mouseup",
touchMove = isTouchSupported ? "touchmove" : "mousemove",
nullFunc = function () {},
objectHolder = {}; //internal functions
function readyDom(iObj) {
var holder = iObj.holder,
option = iObj.option,
matrix = option.matrix,
margin = option.margin,
radius = option.radius,
html = ['<ul class="patt-wrap" style="padding:' + margin + 'px">'];
for (var i = 0, ln = matrix[0] * matrix[1]; i < ln; i++) {
html.push('<li class="patt-circ" style="margin:' + margin + 'px; width : ' + (radius * 2) + 'px; height : ' + (radius * 2) + 'px; -webkit-border-radius: ' + radius + 'px; -moz-border-radius: ' + radius + 'px; border-radius: ' + radius + 'px; "><div class="patt-dots"></div></li>');
}
html.push('</ul>');
holder.html(html.join('')).css({
'width': (matrix[1] * (radius * 2 + margin * 2) + margin * 2) + 'px',
'height': (matrix[0] * (radius * 2 + margin * 2) + margin * 2) + 'px'
}); //select pattern circle
iObj.pattCircle = iObj.holder.find('.patt-circ'); } //return height and angle for lines
function getLengthAngle(x1, x2, y1, y2) {
var xDiff = x2 - x1,
yDiff = y2 - y1; return {
length: Math.ceil(Math.sqrt(xDiff * xDiff + yDiff * yDiff)),
angle: Math.round((Math.atan2(yDiff, xDiff) * 180) / Math.PI)
};
} var startHandler = function (e, obj) {
e.preventDefault();
var iObj = objectHolder[obj.token]; //check if pattern is visible or not
if (!iObj.option.patternVisible) {
iObj.holder.addClass('patt-hidden');
} //assign events
$(this).on(touchMove + '.pattern-move', function (e) {
moveHandler.call(this, e, obj);
});
$(document).one(touchEnd, function () {
endHandler.call(this, e, obj);
});
//set pattern offset
var wrap = iObj.holder.find('.patt-wrap'),
offset = wrap.offset();
iObj.wrapTop = offset.top;
iObj.wrapLeft = offset.left; //reset pattern
obj.reset(); },
moveHandler = function (e, obj) {
e.preventDefault();
var x = e.pageX || e.originalEvent.touches[0].pageX,
y = e.pageY || e.originalEvent.touches[0].pageY,
iObj = objectHolder[obj.token],
li = iObj.pattCircle,
patternAry = iObj.patternAry,
lineOnMove = iObj.option.lineOnMove,
posObj = iObj.getIdxFromPoint(x, y),
idx = posObj.idx,
pattId = iObj.mapperFunc(idx) || idx; if (patternAry.length > 0) {
var laMove = getLengthAngle(iObj.lineX1, posObj.x, iObj.lineY1, posObj.y);
iObj.line.css({
'width': (laMove.length + 10) + 'px',
'transform': 'rotate(' + laMove.angle + 'deg)'
});
} if (idx) {
if (patternAry.indexOf(pattId) == -1) {
var elm = $(li[idx - 1]);
elm.addClass('hovered');
//push pattern on array
patternAry.push(pattId); //add start point for line
var margin = iObj.option.margin,
radius = iObj.option.radius,
newX = (posObj.i - 1) * (2 * margin + 2 * radius) + 2 * margin + radius,
newY = (posObj.j - 1) * (2 * margin + 2 * radius) + 2 * margin + radius; if (patternAry.length != 1) {
//to fix line
var lA = getLengthAngle(iObj.lineX1, newX, iObj.lineY1, newY);
iObj.line.css({
'width': (lA.length + 10) + 'px',
'transform': 'rotate(' + lA.angle + 'deg)'
}); if (!lineOnMove) iObj.line.show();
} //to create new line
var line = $('<div class="patt-lines" style="top:' + (newY - 5) + 'px; left:' + (newX - 5) + 'px"></div>');
iObj.line = line;
iObj.lineX1 = newX;
iObj.lineY1 = newY;
//add on dom
iObj.holder.append(line);
if (!lineOnMove) iObj.line.hide();
}
} },
endHandler = function (e, obj) {
e.preventDefault();
var iObj = objectHolder[obj.token],
pattern = iObj.patternAry.join(''); //remove hidden pattern class and remove event
iObj.holder.off('.pattern-move').removeClass('patt-hidden'); if (!pattern) return; iObj.option.onDraw(pattern); //to remove last line
iObj.line.remove(); if (iObj.rightPattern) {
if (pattern == iObj.rightPattern) {
iObj.onSuccess();
} else {
iObj.onError();
obj.error();
}
}
}; function InternalMethods() {} InternalMethods.prototype = {
constructor: InternalMethods,
getIdxFromPoint: function (x, y) {
var option = this.option,
matrix = option.matrix,
xi = x - this.wrapLeft,
yi = y - this.wrapTop,
idx = null,
margin = option.margin,
plotLn = option.radius * 2 + margin * 2,
qsntX = Math.ceil(xi / plotLn),
qsntY = Math.ceil(yi / plotLn),
remX = xi % plotLn,
remY = yi % plotLn; if (qsntX <= matrix[1] && qsntY <= matrix[0] && remX > margin * 2 && remY > margin * 2) {
idx = (qsntY - 1) * matrix[1] + qsntX;
}
return {
idx: idx,
i: qsntX,
j: qsntY,
x: xi,
y: yi
};
}
}; function PatternLock(selector, option) {
var self = this,
token = self.token = Math.random(),
iObj = objectHolder[token] = new InternalMethods(),
holder = iObj.holder = $(selector); //if holder is not present return
if (holder.length == 0) return; iObj.object = self;
option = iObj.option = $.extend({}, PatternLock.defaults, option);
readyDom(iObj); //add class on holder
holder.addClass('patt-holder'); //change offset property of holder if it does not have any property
if (holder.css('position') == "static") holder.css('position', 'relative'); //assign event
holder.on(touchStart, function (e) {
startHandler.call(this, e, self);
}); //handeling callback
iObj.option.onDraw = option.onDraw || nullFunc; //adding a mapper function
var mapper = option.mapper;
if (typeof mapper == "object") {
iObj.mapperFunc = function (idx) {
return mapper[idx];
};
} else if (typeof mapper == "function") {
iObj.mapperFunc = mapper;
} else {
iObj.mapperFunc = nullFunc;
} //to delete from option object
iObj.option.mapper = null;
} PatternLock.prototype = {
constructor: PatternLock,
option: function (key, val) {
var iObj = objectHolder[this.token],
option = iObj.option;
//for set methods
if (!val) {
return option[key];
}
//for setter
else {
option[key] = val;
if (key == "margin" || key == "matrix" || key == "radius") {
readyDom(iObj);
}
}
},
getPattern: function () {
return objectHolder[this.token].patternAry.join('');
},
reset: function () {
var iObj = objectHolder[this.token];
//to remove lines
iObj.pattCircle.removeClass('hovered');
iObj.holder.find('.patt-lines').remove(); //add/reset a array which capture pattern
iObj.patternAry = []; //remove error class if added
iObj.holder.removeClass('patt-error'); },
error: function () {
objectHolder[this.token].holder.addClass('patt-error');
},
checkForPattern: function (pattern, success, error) {
var iObj = objectHolder[this.token];
iObj.rightPattern = pattern;
iObj.onSuccess = success || nullFunc;
iObj.onError = error || nullFunc;
}
}; PatternLock.defaults = {
matrix: [3, 3],
margin: 20,
radius: 25,
patternVisible: true,
lineOnMove: true
}; window.PatternLock = PatternLock;
}(jQuery, window, document));

jQuery仿Android锁屏图案应用插件的更多相关文章

  1. jQuery仿Android锁屏图案应用

    在线演示 本地下载

  2. Android 锁屏状态/锁屏密码等相关

    Android 锁屏状态/锁屏密码等相关 开始是在设备管理器方面找方法,但一直不行,可能在公司系统组同事的帮助下,知道KeyguardManager这个类 /** * 当前系统锁屏是否有密码 * @p ...

  3. [Android] Android 锁屏实现与总结 (三)

    上接: Android 锁屏实现与总结 (二) 系列文章链接如下: [Android] Android 锁屏实现与总结 (一) [Android] Android 锁屏实现与总结 (二) [Andro ...

  4. [Android] Android 锁屏实现与总结 (二)

    上接: [Android] Android 锁屏实现与总结 (一) 系列文章链接如下: [Android] Android 锁屏实现与总结 (一) [Android] Android 锁屏实现与总结 ...

  5. [Android] Android 锁屏实现与总结 (一)

    实现锁屏的方式有多种(锁屏应用.悬浮窗.普通Activity伪造锁屏等等).但国内比较主流并且被广泛应用的Activity伪造锁屏方式. 实例演示图片如下: 系列文章链接如下: [Android] A ...

  6. Java计算手机九宫格锁屏图案连接9个点的方案总数

    (一)问题 九宫格图案解锁连接9个点共有多少种方案? (二)初步思考 可以把问题抽象为求满足一定条件的1-9的排列数(类似于“八皇后问题”),例如123456789和987654321都是合法的(按照 ...

  7. Android锁屏后数据改变的解决方案

    如果一个界面设置成横屏,那么锁屏再开启之后,会重新执行一遍onCreate()方法.对于这个问题的解决方案如下: 只需要在Menifest文件的activity相应标签下添加这行代码即可: andro ...

  8. Android 锁屏软件MemoryDebris测试报告

    目 录 项目基本信息 第1章         引言 1.1        编写目的 1.2        项目背景 1.3        参考资料 1.4        术语和缩略语 第2章      ...

  9. Android锁屏软件

    需求描述:锁屏软件就是点击应用然后直接锁屏,不会弹出任何界面 设计思路:应用启动以后通过getSystemService获取系统的服务,调用安卓系统的DevicePolicyManager和Compo ...

随机推荐

  1. Checklist: 2019 05.01 ~ 06.30

    Golang Lessons learned porting 50k loc from Java to Go Five things that make Go fast Simple techniqu ...

  2. ASE Alpha Sprint - backend scrum 6

    本次scrum于2019.11.11在sky garden进行,持续30分钟. 参与人: Zhikai Chen, Jia Ning, Hao Wang 请假: Xin Kang, Lihao Ran ...

  3. win10 护眼

  4. 九、Rxjs请求对Observable进行封装

    1.引入 Http.Jsonp.Rxjs 三个模块 2.请求中添加一个 .map(res => res.json) 问题 1.Property 'map' does not exist on t ...

  5. C++ KMP文本匹配

    代码如下: 环境为VC #include <iostream> #include <algorithm> #include <string> #include &l ...

  6. Java打印流学习

    打印流 打印流的主要功能是用于输出,在整个IO包打印流分为两种类型,打印流可以很方便的进行输出. 1.字节打印流:PrintStream(在字节输出时,可以增强输出功能) 2.字符打印流:PrintW ...

  7. Spring---Spring Aware

    1.概述 1.1.Spring中的  所有Bean  对Spring容器的存在  是没有意识的(即你可以将容器换成别的容器,这样使用容器与Bean之间的耦合度很低): 但在实际项目中,不可避免的要用到 ...

  8. 人生苦短_我用Python_Try_Exception异常捕捉_007

    # coding=utf-8 ''' request+try__异常处理 ''' import requests class HttpRequests: def __init__(self, url, ...

  9. maven插件之maven-surefire-plugin,junit单元测试报告和sonar测试覆盖率的整合说明

    POM中配置的如下: <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId> ...

  10. pgsql SQL监控,查询SQL执行情况

    SELECT procpid, START, now() - START AS lap, current_query FROM ( SELECT backendid, pg_stat_get_back ...