在jquery中反转滑动div的循环

时间:2012-11-19 05:36:54

标签: javascript jquery html

HTML部分:

<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 id="box4" class="box">Div #4</div>
    <div id="box5" class="box">Div #5</div>

</div>

Javascript部分:

$('.box').click(function() {

    $(this).animate({
        left: '-50%'
    }, 500, function() {
        $(this).css('left', '150%');
        $(this).appendTo('#container');
    });

    $(this).next().animate({
        left: '50%'
    }, 500);
});​

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: 150%;
    top: 100px;
    margin-left: -25%;
}

#box1 {
    background-color: green;
    left: 50%;
}

#box2 {
    background-color: yellow;
}

#box3 {
    background-color: red;
}

#box4 {
    background-color: orange;
}

#box5 {
    background-color: blue;
}
​

当上面的代码被编译并且页面中的div被点击时,它们向左移动。我希望div在相反的方向向右滑动。我知道这对于了解jQuery的人来说可能非常容易,但我对这个脚本几乎没有任何了解,但我喜欢它的功能。链接到此示例:http://jsfiddle.net/jtbowden/ykbgT/2/感谢所有帮助。

2 个答案:

答案 0 :(得分:1)

$('.box').click(function() {

    $(this).animate({
        left: '150%'
    }, 500, function() {
        $(this).css('left', '-150%');
        $(this).appendTo('#container');
    });

    $(this).next().css({
        left: '-150%'
    }, 500);
    $(this).next().animate({
        left: '50%'
    }, 500);
});​

http://jsfiddle.net/ykbgT/4148/

答案 1 :(得分:1)

<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 id="box4" class="box">Div #4</div>
<div id="box5" class="box">Div #5</div>

</div>


$('.box').click(function() {

$(this).animate({
    right: '-50%'
}, 500, function() {
    $(this).css('right', '150%');
    $(this).appendTo('#container');
});

$(this).next().animate({
    right: '50%'
}, 500);
});​


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;
right: 150%;
top: 100px;
margin-right: -25%;
}

#box1 {
background-color: green;
right: 50%;
}

#box2 {
background-color: yellow;
}

#box3 {
background-color: red;
}

#box4 {
background-color: orange;
}

#box5 {
background-color: blue;
}