点击改变方块位置

<!DOCTYPE html>
<html>

 

<head lang="en">
<meta charset="UTF-8">
<!-- 知识点:dom创建、dom添加、数组、取模% -->
<title>方块改变</title>
<style>
* {
margin: 0;
padding: 0;
}

 

body {
background: linear-gradient(#a1e6e9 0%, #fff 100%)no-repeat;
min-height: 400px;
}

 

.wrap_box {
width: 500px;
margin: 30px auto;
background: #fff;
border-radius: 5px;
padding: 30px;
box-shadow: 0px 0px 10px #5ab1fd;
}

 

button {
cursor: pointer;
}

 

button {
background: #fff;
line-height: 32px;
border: 2px solid #5ab1fd;
width: 80px;
font-size: 14px;
color: #5ab1fd;
outline: none 0;
}

 

button:hover {
border: 2px solid #de4965;
color: #de4965;
}

 

.contents {
border: 2px solid #5ab1fd;
margin-top: 10px;
padding: 10px;
position: relative;
width: 250px;
height: 250px;
}

 

.contents>div {
height: 50px;
width: 50px;
line-height: 50px;
text-align: center;
background: #5ab1fd;
color: #fff;
position: absolute;
border-radius: 2px;
}
</style>
</head>

 

<body>
<div class=" wrap">
  <div class="btn">
    <button class="mybtn">显示</button>
  </div>
  <div class=" contents ">
    <div style="left:10px ;top:10px">1</div>
    <div style="left:60px ;top:60px">1</div>
    <div style="left:110px ;top:110px">1</div>
    <div style="left:160px ;top:160px">1</div>
    <div style="left:210px ;top:210px">1</div>
  </div>
</div>

 

<!-- 知识点:dom创建、dom添加、数组、取模% -->
<script>

  // 获取dom

  var btn = document.getElementByClassName("mybtn");

  var box = document.getElementByClassName("contents")[0];

  var adiv = box.getElementByTagName('div');

  var nowIndex = -1;

  //位置数据

  var moveArr[

    [[0, 0], [50, 50], [100, 100], [50, 150], [0, 200]],
    [[0, 0], [50, 50], [100, 100], [150, 50], [200, 0]],
    [[200, 0], [150, 50], [100, 100], [150, 150], [200, 200]],
    [[0, 200], [50, 150], [100, 100], [150, 150], [200, 200]]

];

  //写点击事件

  btn.οnclick=function(){

  if(nowIndex == -1){

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

    adiv[i] .style.display = 'block';

} 

  btn.innerHtml = " 改变 ";

  nowIndex++; 

}  else{

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

  var arrIndex = nowIndex%4; //取模

  adiv[i].style.left = (moveArr[arrIndex][i][0]+10)+'px';

  adiv[i].style.top = (moveArr[arrIndex][i][1]+10)+'px';

}

}

}

</script>
</body>

 

</html>

 

转载于:https://www.cnblogs.com/yang656/p/9789799.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值