最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

jQuery实现获取当前鼠标位置并输出功能示例

来源:动视网 责编:小采 时间:2020-11-27 22:02:18
文档

jQuery实现获取当前鼠标位置并输出功能示例

jQuery实现获取当前鼠标位置并输出功能示例:本文实例讲述了jQuery实现获取当前鼠标位置并输出功能。分享给大家供大家参考,具体如下: jQuery获取当前鼠标位置并输出 1.html <body onmousemove=mousemove(event)></body> 2.css html, body { width
推荐度:
导读jQuery实现获取当前鼠标位置并输出功能示例:本文实例讲述了jQuery实现获取当前鼠标位置并输出功能。分享给大家供大家参考,具体如下: jQuery获取当前鼠标位置并输出 1.html <body onmousemove=mousemove(event)></body> 2.css html, body { width


本文实例讲述了jQuery实现获取当前鼠标位置并输出功能。分享给大家供大家参考,具体如下:

jQuery获取当前鼠标位置并输出

1.html

<body onmousemove="mousemove(event)"></body>

2.css

html,
body {
 width: 100%;
 height: 100%;
 background: #A5CEDB;
 position: relative;
}
.newDiv {
 position: absolute;
 background: red;
 color: white;
 width: 100px;
 height: 50px;
}

3.js

var movex;
var movey; //用来接受鼠标位置的全局变量
function mousemove(e) {
 e = e || window.event;
 if(e.pageX || e.pageY) {
 movex = e.pageX;
 movey = e.pageY
 }
 creatDiv(movex, movey);
}
function creatDiv(x, y) {
 $(".newDiv").remove();
 var str = ("<div class=\'newDiv\'>" + x + "," + y + "</div>");
 $("body").append(str);
 $(".newDiv").css("left", x + "px").css("top", y + "px");
}

完整示例代码如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>www.gxlcms.com js获取当前鼠标位置</title>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<script>
var movex;
var movey; //用来接受鼠标位置的全局变量
function mousemove(e) {
 e = e || window.event;
 if(e.pageX || e.pageY) {
 movex = e.pageX;
 movey = e.pageY
 }
 creatDiv(movex, movey);
}
function creatDiv(x, y) {
 $(".newDiv").remove();
 var str = ("<div class=\'newDiv\'>" + x + "," + y + "</div>");
 $("body").append(str);
 $(".newDiv").css("left", x + "px").css("top", y + "px");
}
</script>
<style>
html,
body {
 width: 100%;
 height: 100%;
 background: #A5CEDB;
 position: relative;
}
.newDiv {
 position: absolute;
 background: red;
 color: white;
 width: 100px;
 height: 50px;
}
</style>
</head>
<body onmousemove="mousemove(event)"></body>
</html>

效果:

(提示:可以在creatDiv方法里面酌情加入想要的偏移量)

PS:感兴趣的朋友可以使用如下工具测试上述代码的运行效果:

在线HTML/CSS/JavaScript代码运行工具:
http://tools.jb51.net/code/HtmlJsRun

在线HTML/CSS/JavaScript前端代码调试运行工具:
http://tools.jb51.net/code/WebCodeRun

更多关于jQuery相关内容感兴趣的读者可查看本站专题:《jQuery拖拽特效与技巧总结》、《jQuery常用插件及用法总结》、《jquery中Ajax用法总结》、《jQuery表格(table)操作技巧汇总》、《jQuery扩展技巧总结》、《jQuery常见经典特效汇总》、《jQuery动画与特效用法总结》及《jquery选择器用法总结》

希望本文所述对大家jQuery程序设计有所帮助。

文档

jQuery实现获取当前鼠标位置并输出功能示例

jQuery实现获取当前鼠标位置并输出功能示例:本文实例讲述了jQuery实现获取当前鼠标位置并输出功能。分享给大家供大家参考,具体如下: jQuery获取当前鼠标位置并输出 1.html <body onmousemove=mousemove(event)></body> 2.css html, body { width
推荐度:
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top