小球随鼠标移动

第一种方式:利用js产生节点实现

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>小球随鼠标移动</title>

</head>

<body>

<div id="wrap"></div>

</body>

<script type="text/javascript">

//创建一个数字的随机函数

function randNum(m, n) {

return Math.floor(Math.random() * (n - m) + m);

};

//创建一个随机颜色的函数

function randColor() {

var r = randNum(0, 255);

var g = randNum(0, 255);

var b = randNum(0, 255);

return "rgb(" + r + "," + g + "," + b + ")";

};

var arrX = [];   //存储鼠标距可视窗口左边的距离

var arrY = []; //存储鼠标距可视窗口顶部的距离

var divSum = []; //存储创建的节点

var index = 0; //存储小球的编号

var wrap = document.getElementById("wrap");


function creatdiv() {    //创建一个新的节点

var event1 = event || e;

arrX.push(event1.clientX); //存位置

arrY.push(event1.clientY);

var divs = document.createElement("div");

divs.style.position = "absolute";

divs.style.width = "40px";

divs.style.height = "40px";

divs.style.borderRadius = "50%";

divs.style.backgroundColor = randColor();

divs.style.left = event1.clientX - 20 + "px";

divs.style.top = event1.clientY - 20 + "px";

divs.style.textAlign="center";

divs.style.lineHeight="40px";

divs.innerHTML = index; //为创建的div节点添加内容

wrap.appendChild(divs); //添加divs节点

divSum.push(divs); //存储创建的div节点

// console.log(divSum[4]);       //调试最后一个节点

}

document.onmousemove = function(e) {

index++;

if(index==81){

index = 0; //节点内容清零

}

creatdiv();  

if (divSum.length >= 81) {

// debugger;    //调试程序用的命令

console.log(divSum[0]);

wrap.removeChild(divSum[0]);

for (var i = 0; i < 80; i++) {

arrX[i] = arrX[i + 1]; //交换位置坐标X

arrY[i] = arrY[i + 1]; //交换位置坐标Y

divSum[i] = divSum[i + 1];  //交换节点位置

}

divSum.pop();     //删除创建的最后一个节点

}

// console.log(arrX);

// console.log(arrY);

}

</script>


</html>

第二种方式:在html中写好结构,js中处理逻辑交互

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title></title>

<style type="text/css">

.content{

width: 30px;

height: 30px;

background-color: red;

border-radius: 50%;

font-size: 17px;

text-align: center;

line-height: 30px;

position: absolute;

}

</style>

</head>

<body>

<div class="content">1</div>

<div class="content">2</div>

<div class="content">3</div>

<div class="content">4</div>

<div class="content">5</div>

<div class="content">6</div>

<div class="content">7</div>

<div class="content">8</div>

<div class="content">9</div>

<div class="content">10</div>

<div class="content">11</div>

<div class="content">12</div>

<div class="content">13</div>

<div class="content">14</div>

<div class="content">15</div>

<div class="content">16</div>

<div class="content">17</div>

<div class="content">18</div>

<div class="content">19</div>

<div class="content">20</div>

<div class="content">21</div>

<div class="content">22</div>

<div class="content">23</div>

<div class="content">24</div>

<div class="content">25</div>

<div class="content">26</div>

<div class="content">27</div>

<div class="content">28</div>

<div class="content">29</div>

<div class="content">30</div>

<div class="content">31</div>

<div class="content">32</div>

<div class="content">33</div>

<div class="content">34</div>

<div class="content">35</div>

<div class="content">36</div>

<div class="content">37</div>

<div class="content">38</div>

<div class="content">39</div>

<div class="content">40</div>

<div class="content">41</div>

<div class="content">42</div>

<div class="content">43</div>

<div class="content">44</div>

<div class="content">45</div>

<div class="content">46</div>

<div class="content">47</div>

<div class="content">48</div>

<div class="content">49</div>

<div class="content">50</div>

<div class="content">51</div>

<div class="content">52</div>

<div class="content">53</div>

<div class="content">54</div>

<div class="content">55</div>

<div class="content">56</div>

<div class="content">57</div>

<div class="content">58</div>

<div class="content">59</div>

<div class="content">60</div>

<div class="content">61</div>

<div class="content">62</div>

<div class="content">63</div>

<div class="content">64</div>

<div class="content">65</div>

<div class="content">66</div>

<div class="content">67</div>

<div class="content">68</div>

<div class="content">69</div>

<div class="content">70</div>

<div class="content">71</div>

<div class="content">72</div>

<div class="content">73</div>

<div class="content">74</div>

<div class="content">75</div>

<div class="content">76</div>

<div class="content">77</div>

<div class="content">78</div>

<div class="content">79</div>

<div class="content">80</div>

</body>

<script type="text/javascript">

function randomNum (min,max) {

  return Math.floor(Math.random() * (max - min + 1) + min);

}

//设置颜色

var divs = document.getElementsByClassName('content');

for (var i = 0;i < divs.length;i++) {

var r = randomNum(0,255);

var g = randomNum(0,255);

var b = randomNum(0,255);

divs[i].style.backgroundColor = 'rgb('

+ r + ',' + g + ',' + b + ')';

}

//设置多个球div跟随着鼠标移动

document.onmousemove = function (e) {

//获取事件对象

var event = window.event || e;

//设置相邻div的位置为上一次鼠标移动的位置

for (var i = divs.length - 1; i > 0;i--) {

divs[i].style.top = divs[i - 1].style.top;

divs[i].style.left  = divs[i - 1].style.left;

}

//设置最上面的div位置,跟随着鼠标位置移动

divs[0].style.top = event.clientY + 'px';

divs[0].style.left = event.clientX + 'px';

}

</script>

</html>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值