日期:2014-05-16 浏览次数:20495 次
<html>
<head>
<style>
a{
position:relative;
}
</style>
<script type="text/javascript">
mouseover=true
function coordinates(event){
if (!moveMe){
alert("return");
return;
}
var event = event || window.event;
var eventId = event.srcElement?event.srcElement.id : event.target.id;
if (eventId =="moveMe"){
mouseover=true;
pleft=document.getElementById('moveMe').style.pixelLeft;
ptop=document.getElementById('moveMe').style.pixelTop;
xcoor=event.clientX;
ycoor=event.clientY;
document.onmousemove=moveImage
}
}
function moveImage(event){
var event = event || window.event;
//alert(mouseover+"---"+event.button);
var eventButton = event.target?event.button:event.button-1;
//因为鼠标事件在不同浏览器下,取值是不同的,自己可以去查查看。
if (mouseover&&eventButton == 0){
document.getElementById('moveMe').style.pixelLeft=pleft+event.clientX-xcoor; document.getElementById('moveMe').style.pixelTop=ptop+event.clientY-ycoor;
return false;
}
}
function mouseup(){
mouseover=false;
}
document.onmouseup=mouseup;
document.onmousedown=coordinates;
</script>
</head>
<body>
<a id="moveMe" href = "http://www.baidu.com" >百度</a>
</body>
</html>
<html>
<head>
<style>
a{
position:relative;
}
</style>
<script type="text/javascript">
mouseover=true
function coordinates(event){
if (!moveMe){
alert("return");
return;
}
var event = event
------其他解决方案--------------------
window.event;
var eventId = event.srcElement?event.srcElement.id : event.target.id;
if (eventId =="moveMe"){
mouseover=true;
pleft=document.getElementById('moveMe').style.pixelLeft;
ptop=document.getElementById('moveMe').style.pixelTop;
if(!pleft){
pleft = event.pageX - 2;
}
if(!ptop){
ptop = event.pageY - 2;
}
xcoor=event.clientX;
ycoor=event.clientY;
document.onmousemove=moveImage
}
}
function moveImage(event){
var event = event