使用jquery更改div的位置
我有一个div,其中包含一个小div和一个空白作为起点。我想做的是,当我单击任何与空空间相邻的div时,单击的div必须移动到该空空间,并创建另一个空空间,当单击时,该空空间将被其附近的任何其他div占用。想法是小div必须有自动机制来决定移动的位置,移动将/必须由css位置控制。我是jquery的新手,如有任何建议,将不胜感激 这是我的代码,我刚从两行div开始,但是在我转到4行之前,我不能正确地得到它使用jquery更改div的位置,jquery,Jquery,我有一个div,其中包含一个小div和一个空白作为起点。我想做的是,当我单击任何与空空间相邻的div时,单击的div必须移动到该空空间,并创建另一个空空间,当单击时,该空空间将被其附近的任何其他div占用。想法是小div必须有自动机制来决定移动的位置,移动将/必须由css位置控制。我是jquery的新手,如有任何建议,将不胜感激 这是我的代码,我刚从两行div开始,但是在我转到4行之前,我不能正确地得到它 <html> <head> <title&
<html>
<head>
<title>divs</title>
<link rel="stylesheet" media="all" type="text/css" href="css/main.css"/>
<script type="text/javascript" src="js/jquery-1.8.0.js"></script>
</head>
<script type="text/javascript">
$(document).ready(function() {
var goTo= new Array("2,5","1,3,6","2,4,7","3,8","1,6","2,5,7","3,7,8","4,7");
var position= new Array("0,0","0,0,60","0,0,120","0,180","0,60","0,0,120","0,60,180","0,120");
var here=8;
var click=true;
$(".box").click(function(){
if(click==true){
click=false;
var id=$(this).attr("id");
id=id.split("_");
id=parseInt(id[1]);
id--;
var adjacents=goTo[id];
adjacents=adjacents.split(",");
var move=false;
for(i in adjacents){
if(adjacents[i]==here){
move=true;
}
}
if(move==true){
var movePos=position[(here-1)];
alert(movePos);
movePos=movePos.split(",")
$(this).animate({top:movePos[0],left:movePos[1]},1000,function(){
$(this).attr("id","box_"+here);
here=id+1;
click=true;
})
}
else{
click=true;
}
}
});
});
</script>
<body>
<div id="container">
<div id="box_1" class="box" style="background:yellow;top:0px;left:0px;">
1
</div>
<div id="box_2" class="box" style="background:red;top:0px;left:60px;">
2
</div>
<div id="box_3" class="box" style="background:magenta;display:block;top:0px;left:120px;">
3
</div>
<div id="box_4" class="box" style="background:brown;top:0px;left:180px;">
4
</div>
<div id="box_5" class="box" style=" background:orange;top:60px;left:0px;">
5
</div>
<div id="box_6" class="box" style="background:pink;top:60px;left:60px;">
6
</div>
<div id="box_7" class="box" style="background:green;top:60px;left:120px;">
7
</div>
</div>
</body>
</html>
divs
$(文档).ready(函数(){
var goTo=新数组(“2,5”、“1,3,6”、“2,4,7”、“3,8”、“1,6”、“2,5,7”、“3,7,8”、“4,7”);
var位置=新数组(“0,0”、“0,0,60”、“0,0120”、“0180”、“0,60”、“0,0120”、“0,60180”、“0120”);
这里的var=8;
var click=true;
$(“.box”)。单击(函数(){
如果(单击==true){
单击=false;
var id=$(this.attr(“id”);
id=id.split(“”);
id=parseInt(id[1]);
id--;
变量邻接=转到[id];
邻接=邻接。拆分(“,”);
var-move=false;
for(i在邻接中){
if(邻接[i]==此处){
move=true;
}
}
if(move==true){
var movePos=位置[(此处-1)];
警报(移动POS);
移动位置=移动位置拆分(“,”)
$(this).animate({top:movePos[0],left:movePos[1]},1000,function(){
$(this.attr(“id”,“box”+此处);
这里=id+1;
单击=真;
})
}
否则{
单击=真;
}
}
});
});
1.
2.
3.
4.
5.
6.
7.
您好,您可以继续在大div中创建相同数量的小div。
并使其中一个小div透明。接下来,单击透明分区附近的某个分区
将单击的div的css更改为透明的,反之亦然。使用jQuery交换css请发布一些代码,以便我们可以看到您的尝试。欢迎使用堆栈溢出!这不是一个讲故事的网站,它是关于提问的。请尝试一下,遇到特定问题时再回来。是否要实现类似的功能>>