代码之家  ›  专栏  ›  技术社区  ›  drivebass

jQuery使用导航箭头按顺序在屏幕上/屏幕下动画显示div

  •  4
  • drivebass  · 技术社区  · 11 年前

    我想做的是根据方向点击左右导航箭头,依次在屏幕上和屏幕上滑动div。当你点击向右箭头时,divs在屏幕上从右向左滑动,效果很好。我搞不清左箭头的代码,所以div将在屏幕上从左到右的相反方向滑动。这是一把小提琴: http://jsfiddle.net/ykbgT/6696/

    还有一个问题 Slide divs off screen using jQuery + added navigation arrows 标记为已回答,但回答没有给出导航箭头功能的任何细节。

    这是代码:

    HTML格式

    <div class="move left">&larr;</div>
    <div class="move right">&rarr;</div>
    
    <div id="container">
    
    <div id="box1" class="box current">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>
    

    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;
    }
    .move{
        position:fixed;
        z-index:2;
        top:50%;
        margin-top:-20px;
        text-align:center;
        padding:20px;
        background:#fff;
        color: #000;
    }
    .left{
        left:0;
    }
    .right{
        right:0;
    }
    

    JAVASCRIPT语言

    $('.right').click(function(event) {
    $('.current').animate({
        left: '-50%'
    }, 500, function() {
        $(this).css('left', '150%');
        $(this).appendTo('#container');
        $(this).removeClass('current');
    });
    
    $('.current').next().animate({
        left: '50%'
    }, 500, function (){
     $(this).addClass('current');
    });
    
    
    });
    
    $('.left').click(function(event) {
    $('.current').animate({
        left: '50%'
    }, 500, function() {
        $(this).css('left', '-150%');
        $(this).prependTo('#container');
        $(this).removeClass('current');
    });
    
    $('.current').prev().animate({
        left: '-50%'
    }, 500, function (){
     $(this).addClass('current');
    });
    
    
    });
    
    2 回复  |  直到 11 年前
        1
  •  3
  •   SarathSprakash    11 年前

    工作 DEMO

    试试这个

    我已经编辑了你的代码

    密码

    var i = 1;
    $('.right').click(function () {
        if (i < $('#container div').length) {
            $("#box" + i).animate({
                left: '-50%'
            }, 400, function () {
                var $this = $("#box" + i);
                $this.css('left', '150%')
                    .appendTo($('.container'));
            });
            $("#box" + i).next().animate({
                left: '50%'
            }, 400);
            i++;
        }
    });
    $('.left').click(function () {
    
        if (i > 1) {
            $("#box" + i).animate({
                left: '150%'
            }, 400, function () {
                var $this = $("#box" + i);
                $this.css('right', '-150%')
                    .appendTo($('.container'));
            });
            $("#box" + i).prev().animate({
                left: '50%'
            }, 400);
            i--;
        }
    });
    

    希望这能有所帮助,谢谢

        2
  •  1
  •   Community CDub    7 年前

    具有较少JS和CSS转换的解决方案

    来自的帮助 @BenjaminGruenbaum

    检查工作小提琴: http://jsfiddle.net/zPVFd/1/

    HTML-类“.current”已删除:

    <div id="box1" class="box">Div #1</div>
    

    CSS-将CSS转换添加到“.box”

    .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%;
      -webkit-transition: all 0.3s ease-out;
      -moz-transition: all 0.3s ease-out;
      -o-transition: all 0.3s ease-out;
      transition: all 0.3s ease-out;
    
    }
    

    JS代码

    var boxes = $(".box").get(),
        current = 0;
    $('.right').click(function () {
        if (current == (-boxes.length + 1)){
            } else {
            current--;
        updateBoxes();
        }
    });
    
    function updateBoxes() {
        for (var i = current; i < (boxes.length + current); i++) {
            boxes[i - current].style.left = (i * 100 + 50) + "%";
        }
    }
    
    $('.left').click(function () {
        if (current == 0){
        }else{
        current++;
        updateBoxes();
        }
    });