使用导航箭头依次将div动画显示/隐藏

4
我想做的是通过点击左右导航箭头,按照方向依次使div进入和离开屏幕。当你点击右箭头时,div就会从右到左滑动,它能正常工作。我无法找出左箭头的代码,所以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个回答

3

工作中 演示

试试这个

我已经编辑了你的代码

代码

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

希望这能帮到您,谢谢。

我注意到你的代码限制了给定数量的盒子(5)。请查看下面的答案,以便不限制div的数量! - drivebass

1

使用更少的JS和CSS过渡实现的解决方案

来自@BenjaminGruenbaum的帮助。

查看工作的fiddle: http://jsfiddle.net/zPVFd/1/

HTML - 类'.current'已删除:

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

CSS - 给'.box'添加了CSS过渡效果。
.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();
    }
});

1
你应该尽力保持 OP 代码版本,OP 已经尝试过一些东西,他只是想完成它。 - SarathSprakash
我没有给你点踩,但是发现你被点踩了,所以我给你点赞。 - SarathSprakash
@SarathSprakash 实际上这是来自OP版本的相同代码,但使用了CSS3过渡而不是animate,并采用了更紧凑的代码方法。结果工作良好!感谢你的+1!我不明白为什么我的第二个可行答案会被踩🙂 - drivebass

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接