jquery點擊頁面任何區(qū)域?qū)崿F(xiàn)鼠標(biāo)焦點十字效果
更新時間:2013年06月21日 16:52:01 作者:
鼠標(biāo)點擊聚焦,地圖定位,在圖片上突出顯示,焦點定位頁面元素,這些都是在系統(tǒng)開發(fā)是經(jīng)常需要用到的,下面為大家介紹下具體的實現(xiàn),感興趣的朋友可以參考下哈
系統(tǒng)開發(fā)時很多地方需要有焦點效果,例如:鼠標(biāo)點擊聚焦,地圖定位,在圖片上突出顯示,焦點定位頁面元素。
本小功能通過jquery和graphics二次開發(fā),實現(xiàn)通過鼠標(biāo)點擊頁面任何區(qū)域,聚焦當(dāng)前點擊位置。適用于頁面任何元素的位置效果。
首先引入jquery引擎包:jquery-1.4.2.min.js和graphics.js
源碼下載地址
編寫實現(xiàn)效果js文件,qfocus.js,源碼如下:
var qfocus = {
config:{
"bar_dis":true,//橫豎條顯示或隱藏
"circle_dis":true,//焦點隱藏
"bar_color":"black",//線條顏色
"circle_color":"red",//圓圈顏色
"rect_color":"green"http://方塊顏色
},
locationTimer: null,//時間控制標(biāo)識符
onmouseClick: function(ev){//鼠標(biāo)點擊獲取鼠標(biāo)位置畫聚焦效果
var point = this.mousePosition(ev);
this.showFocus(point);
},
onclickElement:function(obj) {//鼠標(biāo)點擊獲取坐標(biāo)做焦點
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頁面調(diào)用源碼:
<!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和graphics二次開發(fā),實現(xiàn)通過鼠標(biāo)點擊頁面任何區(qū)域,聚焦當(dāng)前點擊位置。適用于頁面任何元素的位置效果。
首先引入jquery引擎包:jquery-1.4.2.min.js和graphics.js
源碼下載地址
編寫實現(xiàn)效果js文件,qfocus.js,源碼如下:
復(fù)制代碼 代碼如下:
var qfocus = {
config:{
"bar_dis":true,//橫豎條顯示或隱藏
"circle_dis":true,//焦點隱藏
"bar_color":"black",//線條顏色
"circle_color":"red",//圓圈顏色
"rect_color":"green"http://方塊顏色
},
locationTimer: null,//時間控制標(biāo)識符
onmouseClick: function(ev){//鼠標(biāo)點擊獲取鼠標(biāo)位置畫聚焦效果
var point = this.mousePosition(ev);
this.showFocus(point);
},
onclickElement:function(obj) {//鼠標(biāo)點擊獲取坐標(biāo)做焦點
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頁面調(diào)用源碼:
復(fù)制代碼 代碼如下:
<!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實現(xiàn)鼠標(biāo)點擊導(dǎo)航菜單水波動畫效果附源碼下載
- jquery實現(xiàn)鼠標(biāo)點擊后展開列表內(nèi)容的導(dǎo)航欄效果
- jquery實現(xiàn)可點擊伸縮與展開的菜單效果代碼
- jquery實現(xiàn)點擊彈出層效果的簡單實例
- js點擊出現(xiàn)懸浮窗效果不使用JQuery插件
- jQuery實現(xiàn)點擊圖片翻頁展示效果的方法
- 基于jQuery的煙花效果(運動相關(guān))點擊屏幕出煙花
- jquery實現(xiàn)的點擊翻書效果代碼
- jquery實現(xiàn)的圖片點擊滾動效果
- jquery實現(xiàn)漂浮在網(wǎng)頁右側(cè)的qq在線客服插件示例
- jQuery實現(xiàn)鼠標(biāo)點擊處心形漂浮的炫酷效果示例
相關(guān)文章
jquery實現(xiàn)的回旋滾動效果完整實例【附demo源碼下載】
這篇文章主要介紹了jquery實現(xiàn)的回旋滾動效果,可實現(xiàn)點擊后側(cè)圖片呈現(xiàn)立體翻轉(zhuǎn)切換的功能,涉及jQuery插件roundabout.js的使用,并附帶了完整實例demo源碼供讀者下載參考,需要的朋友可以參考下2016-09-09jQuery實現(xiàn)圖片上傳預(yù)覽效果功能完整實例【測試可用】
這篇文章主要介紹了jQuery實現(xiàn)圖片上傳預(yù)覽效果功能,結(jié)合完整實例形式分析了jQuery處理圖片上傳預(yù)覽相關(guān)事件響應(yīng)、瀏覽器判斷、圖片及頁面元素動態(tài)操作相關(guān)實現(xiàn)技巧,需要的朋友可以參考下2018-05-05jQuery多級聯(lián)動下拉插件chained用法示例
這篇文章主要介紹了jQuery多級聯(lián)動下拉插件chained用法,結(jié)合實例形式分析了jQuery多級聯(lián)動下拉插件chained的功能與基本使用技巧,需要的朋友可以參考下2016-08-08用jquery中插件dialog實現(xiàn)彈框效果實例代碼
這篇文章介紹了jquery中插件dialog實現(xiàn)彈框效果實例代碼,有需要的朋友可以參考一下2013-11-11jquery.mousewheel實現(xiàn)整屏翻屏效果
jQuery Mousewheel 用于添加跨瀏覽器的鼠標(biāo)滾輪支持。 mousewheel事件的處理函數(shù)有一點小小的變化,它除了第一個參數(shù)event 外,還接收到第二個參數(shù)delta。通過參數(shù)delta可以獲取鼠標(biāo)滾輪的方向和速度。2015-08-08Query常用DIV操作獲取和設(shè)置長度寬度的實現(xiàn)方法
下面小編就為大家?guī)硪黄猀uery常用DIV操作獲取和設(shè)置長度寬度的實現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-09-09