繁体   English   中英

可拖动对象回到左上方

[英]Draggable object coming back to top left

我在个人网站上使用了两个可拖动对象,它移动的非常好。 唯一的问题是,当我第一次拖动它时,它返回到左上角...我感觉它正在执行此操作,因为我将%用作位置...

我的网站: 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"> 

更改此行:

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM