jquery点击页面任何区域实现鼠标焦点十字效果
开发时很多地方需要有焦点效果,例如:鼠标点击聚焦,地图定位,在图片上突出显示,焦点定位页面元素。
本小功能通过jquery和graphics二次开发,实现通过鼠标点击页面任何区域,聚焦当前点击位置。适用于页面任何元素的位置效果。
首先引入jquery引擎包:jquery-1.4.2.min.js和graphics.js
编写实现效果js文件,qfocus.js,如下:
. 代码如下:
var qfocus = {
config:{
"bar_dis":true,//横竖条显示或隐藏
"circle_dis":true,//焦点隐藏
"bar_color":"black",//线条颜色
"circle_color":"red",//圆圈颜色
"rect_color":"green"//方块颜色
},
locationtimer: null,//时间控制标识符
onmouseclick: function(ev){//鼠标点击获取鼠标位置画聚焦效果
var point = this.mouseposition(ev);
this.showfocus(point);
},
onclickelement:function(obj) {//鼠标点击获取坐标做焦点
var _point = this.elementposition(obj);
this.showfocus(_point);
},
showfocus:function (point) {//显示焦点效果
if (this.locationtimer) {
cleartimeout(this.locationtimer);
} //清除定时器
var mapdiv = "#mapp";
var _point = point;
var canvas = $("#canvas");
var vline = $("#vline");
var hline = $("#hline");
//焦点隐藏或显示
if (this.config["circle_dis"] == true) {
if (!$("#canvas").attr("id")) {
canvas = '<p id="canvas" style="left:' + (_point.x - 25) + 'px;top:' + (_point.y - 25) + 'px;width:50px;height:50px;overflow:hidden;position:absolute;border:solid 0px red;"/>';
$(canvas).appendto("body");
} else {
canvas.css("left", (_point.x - 25) + "px");
canvas.css("top", (_point.y - 25) + "px");
canvas.show();
}
paper = raphael("canvas");
paper.clear();
var rect = paper.rect(20, 20, 10, 10, 0);
rect.attr("stroke", this.config["rect_color"]);
rect.attr("stroke-width", 1);
}
//是否显示横竖条
if (this.config["bar_dis"] == true) {
if (!$("#vline").attr("id")) {
vline = "<p id='vline' style='background-color:"+this.config["bar_color"]+";height:100%;width:1px;position:absolute;top:0px;left:" + (_point.x) + "px;'/>";
$(vline).appendto("body");
} else {
$(vline).css("left",(_point.x) + "px");
vline.show();
}
if (!$("#hline").attr("id")) {
var hline = "<p id='hline' style='overflow:hidden;background-color:"+this.config["bar_color"]+";height:1px;width:100%;position:absolute;left:0px;top:" + (_point.y ) + "px;'/>";
$(hline).appendto("body");
} else {
$("#hline").css("top",(_point.y ) + "px");
hline.show();
}
}
this.hidefocus();
return true;
}, hidefocus:function() {//隐藏焦点效果
if (paper != null) {
var circle = paper.circle(25, 25, 30);
circle.attr("stroke", this.config["circle_color"]);
circle.attr("stroke-width", 1);
var anim = raphael.animation({
r: 5
}, 900, null, function(){
this.locationtimer = settimeout(function(){
$("#canvas").hide(); //焦点
$("#vline").hide(); //横条
$("#hline").hide(); //竖条
cleartimeout(this.locationtimer);
}, 500);
});
circle.animate(anim);
} else {
this.locationtimer = settimeout(function(){
$("#canvas").hide(); //焦点
$("#vline").hide(); //横条
$("#hline").hide(); //竖条
cleartimeout(this.locationtimer);
}, 500);
}
},mouseposition:function (e) {
var x,y;
var e = e||window.event;
return {
x:e.clientx+document.body.scrollleft+document.documentelement.scrollleft,
y:e.clienty+document.body.scrolltop+document.documentelement.scrolltop
}
},elementposition:function( oelement ) {
var x2 = 0;
var y2 = 0;
var width = oelement.offsetwidth;
var height = oelement.offsetheight;
var postion = "";
if( typeof( oelement.offsetparent ) != 'undefined' ){
for( var posx = 0, posy = 0; oelement; oelement = oelement.offsetparent ) {
posx += oelement.offsetleft;
posy += oelement.offsettop;
}
x2 = posx + width;
y2 = posy + height;
postion = [ posx, posy ,x2, y2];
} else{
x2 = oelement.x + width;
y2 = oelement.y + height;
postion = [ oelement.x, oelement.y, x2, y2];
}
var x = postion[0] + ((postion[2] - postion[0])/2);
var y = postion[1] + ((postion[3] - postion[1])/2);
return {"x":x,"y":y};
}
}
html页面调用源码:
. 代码如下:
<!doctype html public "-//w3c//dtd html 4.01 transitional//en" "https://www.w3.org/tr/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="js/graphics.js"></script>
<script type="text/javascript" src="js/qfocus.js"></script>
<title>qfocus</title>
<script type="text/javascript">
function forward(ev){
qfocus.onmouseclick(ev);
}
document.onmousedown=forward;
</script>
</head>
<body>
</body>
</html>
效果图片: