Slide a div offscreen using jQuery

后端 未结 4 518
感动是毒
感动是毒 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条回答
  •  一生所求
    2020-12-07 10:27

    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/

提交回复
热议问题