JavaScript實(shí)現(xiàn)圖片拖曳效果
更新時(shí)間:2017年09月08日 09:15:37 作者:周森
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)圖片拖曳效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了js實(shí)現(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="隨機(jī)生成">
<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'];//隨機(jī)顏色
//給btn注冊(cè)點(diǎn)擊事件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);//隨機(jī)生成左邊距
var top=parseInt(Math.random()*(500-100+1) + 100);//隨機(jī)生成上邊距
var bg=Math.floor((Math.random()*arr.length));//生成數(shù)組隨機(jī)數(shù)獲得隨機(jī)數(shù)組下標(biāo)
box.style.background=arr[bg];//設(shè)置顏色
box.style.top=top+"px";//設(shè)置上邊距
box.style.left=left+"px";//設(shè)置左邊距
}
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>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
使用watch在微信小程序中實(shí)現(xiàn)全局狀態(tài)共享
這篇文章主要給大家介紹了關(guān)于如何使用watch在小程序中實(shí)現(xiàn)全局狀態(tài)共享的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用微信小程序具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06
擴(kuò)展javascript的Date方法實(shí)現(xiàn)代碼(prototype)
長(zhǎng)期從事C#的開發(fā),被C#影響著我的思維。C#中DateTime的操作就很方便,于是就參考它對(duì)js的Date做了擴(kuò)展。2010-11-11

