Slide a div offscreen using jQuery

后端 未结 4 514
感动是毒
感动是毒 2020-12-07 09:55

This is a bit of a challenge. Here\'s what I\'m looking for:

  1. 3 divs on screen
  2. Div 1 resides in the middle of the page (centered)
  3. Div 2 reside
4条回答
  •  -上瘾入骨i
    2020-12-07 10:24

    Something like this?

    http://jsfiddle.net/jtbowden/ykbgT/embedded/result/

    http://jsfiddle.net/jtbowden/ykbgT/

    This is the basic functionality. It doesn't scale to more divs, etc, but that should get you started.

    The key is to wrap your elements in a container and make the overflow hidden.

    Update:

    Here's a slightly better version that handles any number of divs (greater than 1):

    http://jsfiddle.net/jtbowden/ykbgT/1/

    Simplified further:

    http://jsfiddle.net/jtbowden/ykbgT/2/

    Code snippet:

    $('.box').click(function() {
    
        $(this).animate({
            left: '-50%'
        }, 500, function() {
            $(this).css('left', '150%');
            $(this).appendTo('#container');
        });
    
        $(this).next().animate({
            left: '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;
        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 #1
    Div #2
    Div #3
    Div #4
    Div #5

提交回复
热议问题