【问题标题】:jQuery animate divs on/off screen sequentially with navigation arrowsjQuery 使用导航箭头按顺序打开/关闭屏幕上的 div
【发布时间】:2013-09-16 18:21:12
【问题描述】:

我想要做的是通过根据方向单击左右导航箭头顺序地在屏幕上滑动和关闭 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');
});


});

【问题讨论】:

    标签: jquery html screen slide arrows


    【解决方案1】:

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

    希望对你有帮助,谢谢

    【讨论】:

    • 我注意到您的代码仅限于给定数量的框 (5)。请在下面查看我的答案,以了解 div 的数量没有限制!
    【解决方案2】:

    减少 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();
        }
    });
    

    【讨论】:

    • 你应该尽量保持OP代码版本,OP已经尝试了一些东西,他只是想完成它
    • 我没有给你投反对票,但发现你投了反对票,所以我 +1
    • @SarathSprakash 实际上它与 OP 版本的代码相同,但使用 CSS3 过渡而不是动画和更紧凑的代码方法。结果运行良好!感谢您的+1!我不明白为什么我对第二个工作答案投了反对票:)
    猜你喜欢
    • 2012-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多