今天小編給大家分享的是js實(shí)現(xiàn)隨機(jī)生成div效果的方法,很多人都不太了解,今天小編為了讓大家更加了解,所以給大家總結(jié)了以下內(nèi)容,一起往下看吧。一定會(huì)有所收獲的哦。
創(chuàng)新互聯(lián)是專業(yè)的云浮網(wǎng)站建設(shè)公司,云浮接單;提供成都做網(wǎng)站、網(wǎng)站建設(shè)、外貿(mào)營銷網(wǎng)站建設(shè),網(wǎng)頁設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行云浮網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來合作!

描述:
鼠標(biāo)點(diǎn)擊頁面,在哪里點(diǎn)擊就在那個(gè)位置創(chuàng)建一個(gè)div,寬高50,顏色隨機(jī),div在鼠標(biāo)點(diǎn)擊的正中間。
效果如下圖所示:

js代碼:
var Method=(function () {
return {
EVENT_ID:"event_id",
loadImage:function (arr) {
var img=new Image();
img.arr=arr;
img.list=[];
img.num=0;
// 如果DOM對(duì)象下的事件偵聽沒有被刪除掉,將會(huì)常駐堆中
// 一旦觸發(fā)了這個(gè)事件需要的條件,就會(huì)繼續(xù)執(zhí)行事件函數(shù)
img.addEventListener("load",this.loadHandler);
img.self=this;
img.src=arr[img.num];
},
loadHandler:function (e) {
this.list.push(this.cloneNode(false));
this.num++;
if(this.num>this.arr.length-1){
this.removeEventListener("load",this.self.loadHandler);
var evt=new Event(Method.EVENT_ID);
evt.list=this.list;
document.dispatchEvent(evt);
return;
}
this.src=this.arr[this.num];
},
$c:function (type,parent,style) {
var elem=document.createElement(type);
if(parent) parent.appendChild(elem);
for(var key in style){
elem.style[key]=style[key];
}
return elem;
},
divColor: function () {
var col="#";//這個(gè)字符串第一位為# 顏色的格式
for(var i=0;i<6;i++){
col+=parseInt(Math.random()*16).toString(16);//rondom*16后的隨機(jī)值即為0-1*16==0-16; toString(16)為轉(zhuǎn)化為16進(jìn)制
}
return col;//最后返回一個(gè)七位的值 格式即為#nnnnnn 顏色的格式
},
random:function (min,max) {
max=Math.max(min,max);
min=Math.min(min,max);
return Math.floor(Math.random()*(max-min)+min);
},
dragElem:function (elem) {
elem.addEventListener("mousedown",this.mouseDragHandler);
elem.self=this;
},
removeDrag:function (elem) {
elem.removeEventListener("mousedown",this.mouseDragHandler);
},
mouseDragHandler:function (e) {
if(e.type==="mousedown"){
e.stopPropagation();
e.preventDefault();
document.point={x:e.offsetX,y:e.offsetY};
document.elem=this;
this.addEventListener("mouseup",this.self.mouseDragHandler);
document.addEventListener("mousemove",this.self.mouseDragHandler);
}else if(e.type==="mousemove"){
this.elem.style.left=e.x-this.point.x+"px";
this.elem.style.top=e.y-this.point.y+"px";
}else if(e.type==="mouseup"){
this.removeEventListener("mouseup",this.self.mouseDragHandler);
document.removeEventListener("mousemove",this.self.mouseDragHandler);
}
}
}
})();html代碼:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<script src="js/Method.js"></script>
<body>
<script>
init();
function init() {
document.addEventListener("mousedown",mouseHandler);
}
function mouseHandler(e) {
var randomDiv=Method.$c("div",document.body,{
width: "50px",
height: "50px",
position: "absolute",
backgroundColor:divColor()
})
randomDiv.style.left=e.clientX-randomDiv.offsetWidth/2+"px";
randomDiv.style.top=e.clientY-randomDiv.offsetHeight/2+"px";
/* top:e.clientY-this.offsetHeight/2+"px",//原因 設(shè)置為了X...xbl
// removeEventListener(randomDiv);*/
}
function divColor() {
var col="#";//這個(gè)字符串第一位為# 顏色的格式
for(var i=0;i<6;i++){
col+=parseInt(Math.random()*16).toString(16);//rondom*16后的隨機(jī)值即為0-1*16==0-16; toString(16)為轉(zhuǎn)化為16進(jìn)制
}
return col;//最后返回一個(gè)七位的值 格式即為#nnnnnn 顏色的格式
}
</script>
</body>
</html>以上就是js實(shí)現(xiàn)隨機(jī)生成div效果的方法的簡略介紹,當(dāng)然詳細(xì)使用上面的不同還得要大家自己使用過才領(lǐng)會(huì)。如果想了解更多,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道哦!
本文標(biāo)題:js實(shí)現(xiàn)隨機(jī)生成div效果的方法
網(wǎng)站URL:http://www.chinadenli.net/article6/gjopog.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營銷推廣、網(wǎng)站制作、網(wǎng)頁設(shè)計(jì)公司、定制網(wǎng)站、響應(yīng)式網(wǎng)站、App開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)