2017-01-26 4 views
0

私は個人的なウェブサイト上にドラッグ可能なオブジェクトを2つ使用していますが、完全に動いています。 唯一の問題は、最初にドラッグしたときに左上に戻ります...私はポジションに%を使用しているので、これを行っていると感じています...ドラッグ可能なオブジェクトが左上に戻る

私のウェブサイト:ここではarnaudaubry.info

は、ドラッグ&ドロップのための私のコードです:ここでは

<script type="text/javascript"> 
 

 
/*********************************************** 
 
* Drag and Drop Script: © Dynamic Drive (http://www.dynamicdrive.com) 
 
* This notice MUST stay intact for legal use 
 
* Visit http://www.dynamicdrive.com/ for this script and 100s more. 
 
***********************************************/ 
 

 
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>

はイメージのために私のコードです:

<img src="http://arnaudaubry.info/wp-content/uploads/2016/11/drag_lookingforwork-03.png" style="position:fixed; top:70%; left:75%; width:11%;" class="drag">

答えて

0

変更この行:

this.offsetx=this.targetobj.offsetLeft; 
this.offsety=this.targetobj.offsetTop; 

this.offsetx=parseInt(this.targetobj.style.left) 
this.offsety=parseInt(this.targetobj.style.top) 

関連する問題