html中怎么获取当前鼠标左键点击的坐标,jQuery实现获取当前鼠标位置并输出功能示例...

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

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

1.html

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 = ("

" + x + "," + y + "
");

$("body").append(str);

$(".newDiv").css("left", x + "px").css("top", y + "px");

}

完整示例代码如下:

www.jb51.net 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 = ("

" + x + "," + y + "
");

$("body").append(str);

$(".newDiv").css("left", x + "px").css("top", y + "px");

}

html,

body {

width: 100%;

height: 100%;

background: #A5CEDB;

position: relative;

}

.newDiv {

position: absolute;

background: red;

color: white;

width: 100px;

height: 50px;

}

效果:

8579464057fcdc9b9a4f800c753437aa.gif

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

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

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值