Javascript 防止对象链接在拖动时工作

Javascript 防止对象链接在拖动时工作,javascript,drag,onmouseout,Javascript,Drag,Onmouseout,我的网站上有一个可拖动的图像,如果点击,我想链接到另一个页面。问题是,我不希望它被“点击”,如果它被拖动,只有当它被点击,而不是被拖动。所以我大概需要取消mouseout上的链接,不是吗?有人能帮忙吗 脚本: var dragobject={ z: 0, x: 0, y: 0, offsetx : null, offsety : null, targetobj : null, dragapproved : 0, initialize:function(){ document.onmousedow

我的网站上有一个可拖动的图像,如果点击,我想链接到另一个页面。问题是,我不希望它被“点击”,如果它被拖动,只有当它被点击,而不是被拖动。所以我大概需要取消mouseout上的链接,不是吗?有人能帮忙吗

脚本:

var dragobject={
z: 0, x: 0, y: 0, offsetx : null, offsety : null, targetobj : null, dragapproved : 0,
initialize:function(){
document.onmousedown=this.drag
document.onmouseup=function(){this.dragapproved=0}
},
drag:function(e){
var evtobj=window.event? window.event : e
this.targetobj=window.event? event.srcElement : e.target
if (this.targetobj.className=="drag"){
this.dragapproved=1
if (isNaN(parseInt(this.targetobj.style.left))){this.targetobj.style.left=0}
if (isNaN(parseInt(this.targetobj.style.top))){this.targetobj.style.top=0}
this.offsetx=parseInt(this.targetobj.style.left)
this.offsety=parseInt(this.targetobj.style.top)
this.x=evtobj.clientX
this.y=evtobj.clientY
if (evtobj.preventDefault)
evtobj.preventDefault()
document.onmousemove=dragobject.moveit
}
},
moveit:function(e){
var evtobj=window.event? window.event : e
if (this.dragapproved==1){
this.targetobj.style.left=this.offsetx+evtobj.clientX-this.x+"px"
this.targetobj.style.top=this.offsety+evtobj.clientY-this.y+"px"
return false
}
}
}

dragobject.initialize()
图片:

<a href="#"><img src="image1" class="drag" STYLE="position:relative; TOP:-216px; LEFT:433px; width:60px"></a>

您可以使用div创建一些边框,并通过以下方式覆盖代码:

<style type="text/css">
    #cover{
      height:sze px;/*enter a bigger height and width than image instead of sze*/
      width:sze px;
    } 
</style>
<script type="text/javascript">
var dragobject={
z: 0, x: 0, y: 0, offsetx : null, offsety : null, targetobj : null, dragapproved : 0,
initialize:function(){
document.onmousedown=this.drag
document.onmouseup=function(){this.dragapproved=0}
},
drag:function(e){
var evtobj=window.event? window.event : e
this.targetobj=window.event? event.srcElement : e.target
if (this.targetobj.className=="drag"){
this.dragapproved=1
if (isNaN(parseInt(this.targetobj.style.left))){this.targetobj.style.left=0}
if (isNaN(parseInt(this.targetobj.style.top))){this.targetobj.style.top=0}
this.offsetx=parseInt(this.targetobj.style.left)
this.offsety=parseInt(this.targetobj.style.top)
this.x=evtobj.clientX
this.y=evtobj.clientY
if (evtobj.preventDefault)
evtobj.preventDefault()
document.onmousemove=dragobject.moveit
}
},
moveit:function(e){
var evtobj=window.event? window.event : e
if (this.dragapproved==1){
this.targetobj.style.left=this.offsetx+evtobj.clientX-this.x+"px"
this.targetobj.style.top=this.offsety+evtobj.clientY-this.y+"px"
return false
}
}
}
dragobject.initialize()
</script>
<div id="cover" class="drag" style="position:relative; TOP:-216px; LEFT:433px; width:60px"><a href="#"><img id="myimg" src="image1"></a></div>

#掩护{
高度:sze px;/*输入比图像大的高度和宽度,而不是sze*/
宽度:sze-px;
} 
var排水管={
z:0,x:0,y:0,偏移量x:null,偏移量:null,目标对象:null,dragapproved:0,
初始化:函数(){
document.onmousedown=this.drag
document.onmouseup=function(){this.dragapproved=0}
},
拖动:函数(e){
var evtobj=window.event?window.event:e
this.targetobj=window.event?event.src元素:e.target
if(this.targetobj.className==“拖动”){
此。dragapproved=1
if(isNaN(parseInt(this.targetobj.style.left)){this.targetobj.style.left=0}
if(isNaN(parseInt(this.targetobj.style.top)){this.targetobj.style.top=0}
this.offsetx=parseInt(this.targetobj.style.left)
this.offsety=parseInt(this.targetobj.style.top)
this.x=evtobj.clientX
this.y=evtobj.clientY
如果(evtobj.preventDefault)
evtobj.preventDefault()
document.onmousemove=dragobject.moveit
}
},
moveit:函数(e){
var evtobj=window.event?window.event:e
if(this.dragapproved==1){
this.targetobj.style.left=this.offsetx+evtobj.clientX this.x+“px”
this.targetobj.style.top=this.offsety+evtobj.clientY this.y+“px”
返回错误
}
}
}
dragobject.initialize()
或者您可以使用js代码更改css:

<script>
function coverage(){
        var img=document.getElementByid('myimg'),
            cov=document.getElementByid('cover'),
            imgh=img.offsetHeight(),
            imgw=img.offsetWidth();
        cov.style.cssText="height:"+(imgh+50)+";width:"+(imgw+50)+";padding:3px;";
     }
     window.onload=coverage;
</script>

函数覆盖率(){
var img=document.getElementByid('myimg'),
cov=document.getElementByid('cover'),
imgh=img.offsetHeight(),
imgw=img.offsetWidth();
cov.style.cssText=“高度:”+(imgh+50)+“宽度:”+(imgw+50)+“填充:3px;”;
}
window.onload=覆盖范围;