---
title: "JavaScript图片拖动效果（ondragstart和ondrag)"
author: "piperzero"
date: "2013-06-13T18:19:00Z"
tags: []
slug: "javascript-ondragstart-ondrag"
draft: false
lastmod: "2013-07-16T08:32:19Z"
id: 1906916
url: "https://piperzero.iteye.com/blog/1906916"
---

&lt;html&gt;  
&lt;head&gt;  
&lt;style type='text/css'&gt;  
.wen p{  
text-align:left;  
margin:15px 0 0 5px;  
}  
&lt;/style&gt;  
&lt;script type='text/javascript'&gt;  
var xx=0,yy=0;  
function movea(v){  
xx=event.x-v.offsetLeft;  
yy=event.y-v.offsetTop;  
}  
function moveb(v){  
v.style.left=event.x-xx;  
v.style.top=event.y-yy;  
}  
&lt;/script&gt;  
&lt;/head&gt;  
&lt;body&gt;  
&lt;img id='img' src='01.jpg' style='position:absolute'  
ondragstart='movea(this);' ondrag='moveb(this);'&gt;  
&lt;/body&gt;  
&lt;/html&gt;

```

```

&lt;wbr&gt;&lt;/wbr&gt;