本文實例為大家分享了js實現(xiàn)圖片拖曳效果的具體代碼,供大家參考,具體內(nèi)容如下
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title> <style> #pbox{ width: 100%; height:100%; } #box{ width: 200px; height: 200px; background:red; position: absolute; } </style></head><body><input type="button" id="btn" value="隨機生成"><div id="pbox"><div id="box"></div></div></body><script> var btn=document.getElementById("btn");//獲取按鈕 var box=document.getElementById("box");//獲取box var pbox=document.getElementById("pbox");//獲取pbox var arr=['#fff143','#ff7500','#a3d900','#eedeb0','#ae7000','#b35c44','#392f41','#ff461f','#44cef6','#edd1db','#003371'];//隨機顏色 //給btn注冊點擊事件ain btn.onclick=function(){ pbox.innerHTML="";//清空pbo for(var i=0;i<=10;i++){ var newTip =box.cloneNode(true); pbox.appendChild(newTip); var left=parseInt(Math.random()*(900-100+1) + 100);//隨機生成左邊距 var top=parseInt(Math.random()*(500-100+1) + 100);//隨機生成上邊距 var bg=Math.floor((Math.random()*arr.length));//生成數(shù)組隨機數(shù)獲得隨機數(shù)組下標 box.style.background=arr[bg];//設置顏色 box.style.top=top+"px";//設置上邊距 box.style.left=left+"px";//設置左邊距 } var c=pbox.children; for(var i=0;i< c.length;i++){ c[i].onmousedown=function (e) {// alert(this.offsetLeft); var spacex=e.pageX-this.offsetLeft; var spacey=e.pageY-this.offsetTop; this.onmousemove=function (e) { this.style.left=e.pageX-spacex+"px"; this.style.top=e.pageY-spacey+"px"; } }; c[i].onmouseup=function () { this.onmousemove=null; } } }</script></html>以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持錯新站長站。
新聞熱點
疑難解答
圖片精選