jquery点击页面任何区域实现鼠标焦点十字效果

 更新时间:2013年06月21日 16:52:01   作者:  
鼠标点击聚焦,地图定位,在图片上突出显示,焦点定位页面元素,这些都是在系统开发是经常需要用到的,下面为大家介绍下具体的实现,感兴趣的朋友可以参考下哈
系统开发时很多地方需要有焦点效果,例如:鼠标点击聚焦,地图定位,在图片上突出显示,焦点定位页面元素。
本小功能通过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 = "#mapdiv";
var _point = point;
var canvas = $("#canvas");
var vLine = $("#vline");
var hLine = $("#hline");
//焦点隐藏或显示
if (this.config["circle_dis"] == true) {
if (!$("#canvas").attr("id")) {
canvas = '<div 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 = "<div 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 = "<div 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" "http://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>

效果图片:

相关文章

  • jquery 简单应用示例总结

    jquery 简单应用示例总结

    jquery 想必大家早已如雷贯耳,本文整理了一些在实际应用中比较常见的功能片段,感兴趣的朋友可以学习下哦,需要改进的地方希望大家不惜指教
    2013-08-08
  • jQuery仿IOS弹出框插件

    jQuery仿IOS弹出框插件

    这款弹出框插件是本人自己模仿IOS原生弹出框写的一个自定义插件,包括Alert弹出框和Confirm确认框,支持标题,内容,按钮文字以及按钮回调函数自定义,使用非常简单,本文给大家介绍了jQuery仿IOS弹出框插件,需要的朋友可以参考下
    2017-02-02
  • jquery multiSelect 多选下拉框

    jquery multiSelect 多选下拉框

    jquery multiSelect 多选下拉框实现挨骂
    2010-07-07
  • jquery实现的回旋滚动效果完整实例【附demo源码下载】

    jquery实现的回旋滚动效果完整实例【附demo源码下载】

    这篇文章主要介绍了jquery实现的回旋滚动效果,可实现点击后侧图片呈现立体翻转切换的功能,涉及jQuery插件roundabout.js的使用,并附带了完整实例demo源码供读者下载参考,需要的朋友可以参考下
    2016-09-09
  • jQuery实现图片上传预览效果功能完整实例【测试可用】

    jQuery实现图片上传预览效果功能完整实例【测试可用】

    这篇文章主要介绍了jQuery实现图片上传预览效果功能,结合完整实例形式分析了jQuery处理图片上传预览相关事件响应、浏览器判断、图片及页面元素动态操作相关实现技巧,需要的朋友可以参考下
    2018-05-05
  • jQuery多级联动下拉插件chained用法示例

    jQuery多级联动下拉插件chained用法示例

    这篇文章主要介绍了jQuery多级联动下拉插件chained用法,结合实例形式分析了jQuery多级联动下拉插件chained的功能与基本使用技巧,需要的朋友可以参考下
    2016-08-08
  • 用jquery中插件dialog实现弹框效果实例代码

    用jquery中插件dialog实现弹框效果实例代码

    这篇文章介绍了jquery中插件dialog实现弹框效果实例代码,有需要的朋友可以参考一下
    2013-11-11
  • jquery.mousewheel实现整屏翻屏效果

    jquery.mousewheel实现整屏翻屏效果

    jQuery Mousewheel 用于添加跨浏览器的鼠标滚轮支持。 mousewheel事件的处理函数有一点小小的变化,它除了第一个参数event 外,还接收到第二个参数delta。通过参数delta可以获取鼠标滚轮的方向和速度。
    2015-08-08
  • Query常用DIV操作获取和设置长度宽度的实现方法

    Query常用DIV操作获取和设置长度宽度的实现方法

    下面小编就为大家带来一篇Query常用DIV操作获取和设置长度宽度的实现方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-09-09
  • 拉动滚动条加载数据的jquery代码

    拉动滚动条加载数据的jquery代码

    拉动滚动条加载数据的jquery代码,需要的朋友可以参考下
    2012-05-05

最新评论