Slide a div offscreen using jQuery

JavascriptJqueryHtmlCss

Javascript Problem Overview


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 resides just off the screen on the far left
  4. Div 3 resides just off the screen on the far right
  5. OnClick, Div 1 slides to the position Div 2 was (to the left), Div 2 slides off the screen entirely, Div 3 slides to where Div 3 was (middle, centered). A new div arrives on the right.

I've tried using jQuery animation and AddClass. jQuery doesn't like sliding a div offscreen.

Any thoughts?

For an example of what I'm describing, visit Groupon.com. I thought it was a cool idea, and have given myself the challenge of recreating it. So far, no dice.

-D

Javascript Solutions


Solution 1 - Javascript

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);
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<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>

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;
}

Solution 2 - Javascript

Maybe I misinterpreted. I though you wanted three divs in a row, and only the ones on the end sliding and whatnot.

http://jsfiddle.net/acsfy/

(I know you're using jQuery for this, but it pissed me off as I was trying to force it to work. You'd have to adapt this for your purposes.)

Solution 3 - Javascript

Extending Jeff B answer, i've included Hammer.js and made a circular list.

$(function() {
$("#esq").click(function() {
    console.log("Esquerda !");
    var obj = $(".ativo");
    $(obj).animate({
        left: '-50%'
    }, 500, function() {
        $(this).css('left', '+150%');
        $(this).appendTo('#container');
    });
    $(obj).next().animate({
        left: '+50%'
    }, 500, function() {
        $(this).addClass('ativo');
        $(obj).removeClass('ativo');
    });
});

$("#dir").click(function() {
    console.log("Direita !");
    var obj = $(".ativo");
    var prox = $(obj).siblings(":last");
    $(obj).animate({
        left: '+150%'
    }, 500, function() {
        $(prox).prependTo('#container');
    });
    $(prox).css('left', '-50%');
    $(prox).animate({
        left: '+50%'
    }, 500, function() {
        $(this).addClass('ativo');
        $(obj).removeClass('ativo');
    });
});

var hammertime = new Hammer(document.getElementById("container"));
hammertime.get('swipe').set({direction: Hammer.DIRECTION_HORIZONTAL});
hammertime.on('swipeleft', function() {
    $("#esq").trigger("click");
});
hammertime.on('swiperight', function() {
    $("#dir").trigger("click");
});

});

Example in: http://jsfiddle.net/tvLt1r9h/2/

Solution 4 - Javascript

And... not a year too late. If you want it to start on the first div, your css needs to look like this.

#box1 { background-color:#333; } #box2 { background-color:#333; left: -50%; } #box3 { background-color:#333; left: 150%; } #box4 { background-color:#333; left: 150%; } #box5 { background-color:#333; left: 150%; }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondjreedView Question on Stackoverflow
Solution 1 - JavascriptJeff BView Answer on Stackoverflow
Solution 2 - JavascriptsdleihssirhcView Answer on Stackoverflow
Solution 3 - JavascriptAlan CNView Answer on Stackoverflow
Solution 4 - JavascriptDenny SmithView Answer on Stackoverflow