这是我的第一个问题(我先搜索并浏览了几个小时)
所以我正在尝试建立一个单页网站,该网站基于我发现的这个 jsfiddle 滑动偏移部分:http: //jsfiddle.net/jtbowden/ykbgT/ 归功于 jtbowden!
<div id="container">
<div id="box1" class="box">Div #1</div>
<div id="box2" class="box">Div #2</div>
<div id="box3" class="box">Div #3</div>
</div>
CSS:
body {
padding: 0px;
}
#container {
position: absolute;
margin: 0px;
padding: 0px;
width: 100%;
height: 100%;
overflow: hidden;
}
.box {
position: absolute;
width: 50%;
height: 300px;
line-height: 300px;
font-size: 50px;
text-align: center;
border: 2px solid black;
left: 50%;
top: 100px;
margin-left: -25%;
}
#box1 {
background-color: green;
left: -150%;
}
#box2 {
background-color: yellow;
}
#box3 {
background-color: red;
left: 150%;
}
js:
$('.box').click(function() {
$('.box').each(function() {
if ($(this).offset().left < 0) {
$(this).css("left", "150%");
} else if ($(this).offset().left > $('#container').width()) {
$(this).animate({
left: '50%',
}, 500 );
} else {
$(this).animate({
left: '-150%',
}, 500 );
}
});
});
它完全符合我的要求,只是当您添加更多部分时,他们开始互相吃内容并且不会像只有一个“容器”时那样返回到“div1”,这基本上是发生了什么:http:/ /jsfiddle.net/cody32x/MTvG8/ 我尝试重命名每个部分,甚至为每个部分创建新功能,但我无法弄清楚!
<div id="box1" class="box">Div #1</div>
<div id="box2" class="box">Div #2</div>
<div id="box3" class="box">Div #3</div>
<div id="box4" class="box">Div #4</div>
<div id="box5" class="box">Div #5</div>
<div id="box6" class="box">Div #1</div>
<div id="box7" class="box">Div #2</div>
<div id="box8" class="box">Div #3</div>
<div id="box9" class="box">Div #4</div>
<div id="box10" class="box">Div #5</div>
</div>
<div id="container2">
<div id="box1" class="box">2-Div #1</div>
<div id="box2" class="box">2-Div #2</div>
<div id="box3" class="box">2-Div #3</div>
<div id="box4" class="box">2-Div #4</div>
<div id="box5" class="box">2-Div #5</div>
<div id="box6" class="box">2-Div #1</div>
<div id="box7" class="box">2-Div #2</div>
<div id="box8" class="box">2-Div #3</div>
<div id="box9" class="box">2-Div #4</div>
<div id="box10" class="box">2-Div #5</div>
</div>
CSS:
body {
padding: 0px;
}
#container {
position: absolute;
margin: 0px;
padding: 0px;
width: 100%;
height: 100%;
overflow: hidden;
cursor: pointer;
}
#container2 {
position: absolute;
margin: 0px;
padding: 0px;
width: 100%;
height: 100%;
top: 300px;
overflow: hidden;
cursor: pointer;
}
.box {
position: absolute;
width: 100%;
height: 100px;
line-height: 100px;
font-size: 50px;
text-align: center;
border: 2px solid black;
left: 200%;
top: 100px;
margin-left: -50%;
}
#box1 {
background-color: green;
left: 50%;
}
#box2 {
background-color: yellow;
}
#box3 {
background-color: red;
}
#box4 {
background-color: orange;
}
#box5 {
background-color: blue;
}
#box6 {
background-color: black;
}
#box7 {
background-color: gray;
}
#box8 {
background-color: blue;
}
#box9 {
background-color: red;
}
#box10 {
background-color: aqua;
}
js:
$('.box').click(function() {
$(this).animate({
left: '-50%'
}, 500, function() {
$(this).css('left', '150%');
$(this).appendTo('#container');
});
$(this).next().animate({
left: '50%'
}, 500);
});
非常感谢!抱歉,如果我在这里发帖不正确……我是新来的。