html的圖片移動(js)


<!DOCTYPE html>
<html>
<style>
*{padding: 0;margin: 0}
#open{
width: 300px;
height: 300px;
background-color: brown;
position: relative;
border-radius:50%;

}
#dv {
width:100px;
height:100px;
/*background-color:blue;*/
border-radius:50%;
position:relative;
}
</style>
<body>

<!--draggable 是否禁止拖曳-->
<img id="dv" src="http://temp.im/250x250/4CD964/fff" draggable="false" />
<br>
<!--<div id="open"></div>-->

<p id="img-left"></p>
<p id="img-top"></p>
<script>
//圖像移動方法
function ImgMove(ImgId){
//獲取元素

var dv = document.getElementById(ImgId);
var x = 0;
var y = 0;
var l = 0;
var t = 0;
var isDown = false;
//鼠標按下事件
dv.onmousedown = function(e) {
//獲取x坐標和y坐標
x = e.clientX;
y = e.clientY;

//獲取左部和頂部的偏移量
l = dv.offsetLeft;
t = dv.offsetTop;
//開關打開
isDown = true;
//設置樣式
dv.style.cursor = 'move';
}
//鼠標移動
window.onmousemove = function(e) {
if (isDown == false) {
return;
}
//獲取x和y
var nx = e.clientX;
var ny = e.clientY;
//計算移動后的左偏移量和頂部的偏移量
var nl = nx - (x - l);
var nt = ny - (y - t);

dv.style.left = nl + 'px';
dv.style.top = nt + 'px';

document.getElementById("img-left").innerHTML=dv.style.left.toString();
document.getElementById("img-top").innerHTML=dv.style.top.toString();

}
//鼠標抬起事件
dv.onmouseup = function() {
//開關關閉
isDown = false;
dv.style.cursor = 'default';
}

}

ImgMove('dv');

</script>
<script>
function ImgInto(num1Id){
var num1 = document.getElementById(num1Id);

return num1.style.top.toString();
}

</script>
</body>
</html>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM