实现鼠标在页面点击出现焦点及大十字星

近段时间,在完成项目进度情况显示时候,用户在操作鼠标时候,显示当鼠标所在位置对应时间如下图所示

代码实现步骤如下:

1.首先引用 jquery.1.7.js

2.再次引用raphael.js

3.然后引用graphics.js

4.最后引用mfocus.js

其中mfocus.js代码如下

var mfocus = {

config: {

"bar_dis": true, //横竖条显示或隐藏

"circle_dis": true, //焦点隐藏

"bar_color": "Yellow", //线条颜色

"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");//需引用raphael.js

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 = postion0 + ((postion2 - postion0) / 2);

var y = postion1 + ((postion3 - postion1) / 2);

return { "x": x, "y": y };

}

}

复制代码
<!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.7.js"></script>
<script type="text/javascript" src="js/raphael.js"></script>
<script type="text/javascript" src="js/graphics.js"></script>
<script type="text/javascript" src="js/mfocus.js"></script>
<title>qfocus</title>
<script type="text/javascript">
function forward(ev){ 
	mfocus.onmouseClick(ev);
}
document.οnmοusedοwn=forward;
</script>
</head>
<body>
</body>
</html>
相关推荐
bai2731 小时前
path.join('C:\a', '\b') 给我 C:\a\b,path.resolve 给 C:\b:Windows 上 51 行路径实测
javascript·后端
kyriewen1 小时前
我写的 12 个 AI Skill,三个月后 8 个在吃空饷
javascript·人工智能·程序员
北城笑笑2 小时前
Vue 112 ,下篇:前端 dist 还原成 Vue3 + Vite 项目( krpano 全景接入与页面适配实战,Vue2 → Vue3 )
前端·javascript·vue.js·3d·前端框架·vue·webgl
我叫黑大帅3 小时前
搜“张三”把备注也带出来了?不用加高级筛选,如何让一个搜索框实现「选项直达」!
前端·javascript·面试
KevinWang_3 小时前
iOS 开发计划(三) - 打印 JavaScript 调试日志
javascript
bai27310 小时前
同一条 q=a+b:URLSearchParams 读成空格、decodeURIComponent 读成加号,11 条查询串打进裸 socket 的实测
javascript·后端
tryCbest11 小时前
Python 爬虫扩展篇(一):APP 抓包与 JS 逆向入门——从看见流量到读懂签名
javascript·爬虫·python
zhanlang021711 小时前
HTML 零基础入门到企业实战全套教程【10.1】综合实战|从零开发静态企业官网:需求分析、页面结构规划、素材准备
前端·html
狮子座的男孩11 小时前
javascript 基础 es6:4、Symbol,Symbol创建对象属性,Symbol内置属性,迭代器,迭代器自定义遍历对象
javascript·经验分享·迭代器·es6·symbol·symbol创建对象属性·symbol内置属性
金八梭11 小时前
格斗对决v4.5
前端·css·自动化·html·css3