Lấy tọa độ của con trỏ(mouse) 2007-09-18 02:02:35

Xác định tọa độ (x,y) của mouse trên màn hình:<br />
Demo: Di chuyển chuột và nhìn lên title của trình duyệt<br />
<script language="JavaScript">
var firefox=document.getElementById&&!document.all;
document.onmousemove=mouseMove;
function mouseMove(e){
var str;
if (firefox) str="x="+e.clientX+", y="+e.clientY;
else str="x="+event.clientX+", y="+event.clientY;
document.title=str;
}
</script>
Code:
<script language="JavaScript">
var firefox=document.getElementById&&!document.all;
document.onmousemove=mouseMove;
function mouseMove(e){
var str;
if (firefox) str="x="+e.clientX+", y="+e.clientY;
else str="x="+event.clientX+", y="+event.clientY;
document.title=str;
}
</script>

Tra loi 6 comment(s) TG 2007-09-18 02:02:35

minhbu 2007-11-11 10:32:49

Vậy tôi muốn 1 thẻ div chứa ký tự có thể chạy theo con trỏ thay đổi làm như thế nào giống như là tooptip đó

Tra loi

NguyenDien_Cafe 2008-05-10 04:22:40

Bạn muốn thêm div thì chỉ việc tạo Div (giả sử ID là IdDiv):
# IdDiv{position: absolute; visibility:hidden}
Sao khi có sự kiện chuột bên trên thì dùng hàm Javascript xử lý:
Cho id.style.left=e.clientX; id.style.top=e.clientY; và
id.style.visibility="visible";
Đây chỉ là mô hình thôi, còn bạn nên viết chi tiết ra, chúc may mắn

Tra loi

mvtuyen 2009-01-22 07:22:46

wá dc, thanks nhìu nhìu

Tra loi

Kenzero 2010-01-31 08:43:48

tooptip

<!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>Untitled Document</title>
<style type="text/css">
body{ position:relative;}
div#dhtmltooltip{ position:absolute; left:0px; top:0px; border:1px solid #999; padding:10px; height:auto;}
</style>
<script language="JavaScript">

function mouseMove(e){

// Chieu dai cua khung tooltip
width = 100;
document.getElementById("dhtmltooltip").style.width = width + "px";

// Kiem tra trinh duyet va lay ra toa do cua chuot
var firefox = document.getElementById && !document.all;
var str;
if (firefox){
x = e.clientX; y = e.clientY;
}
else{
x = event.clientX; y = event.clientY;
}

//
if(x > screen.width - width - 50){
x = x - width - 40;
y = y + 15;
if(y > screen.height/2){
document.getElementById("dhtmltooltip").style.top = y - 100 + "px";
}
else{
document.getElementById("dhtmltooltip").style.top = y + 15 + "px";
}
}
else{
if(y > screen.height/2){
document.getElementById("dhtmltooltip").style.top = y - 100 + "px";
}
else{
document.getElementById("dhtmltooltip").style.top = y + 15 + "px";
}
}

document.getElementById("dhtmltooltip").style.left = x + "px";
}
document.onmousemove = mouseMove;

</script>

</head>

<body>
<div id="dhtmltooltip">
     Kenzero<br>
     Kenzero<br>
     Kenzero<br>                          
    </div>
</body>
</html>

Tra loi

hai lua 2010-04-25 11:13:49

trong vi du cua ban neu toi muon khi click chuot thi se ve mot hinh vuong tai toa do chuot thi lam sao ha ban?
cam on

Tra loi

TG 2010-04-26 09:12:07

khi click bạn sẽ lấy được tọa độ chuột sau đó chỉ cần tạo 1 cái div có position: absolute và left, top bằng tọa độ x,y là bạn được 1 hình chữ nhật, định width = height cho cái div là có hình vuông

Tra loi

Y kien