jQuery 动画 divs 使用导航箭头按顺序打开/关闭屏幕

标签 jquery html screen slide arrows

我想做的是根据方向单击左右导航箭头,依次将 div 滑出和滑入屏幕。当您单击向右箭头时,div 在屏幕上从右向左滑动并且工作正常。我无法弄清楚左箭头的代码,因此 div 将在屏幕上从左到右的相反方向滑动。这是一个 fiddle :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');
});


});

最佳答案

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

希望对你有帮助,谢谢

关于jQuery 动画 divs 使用导航箭头按顺序打开/关闭屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18822477/

相关文章:

javascript - jQuery 1.8.1 data() 检索 HTML5 数据属性时出错

jquery - 表格单元格内具有动态内容的 Div 和表格行高对齐

html - 如何避免在检查元素时显示 html 和 body 元素?

javascript - 所选 event.target 的父 div 的边框轮廓

html - 如何在 Webbrowser VB.Net 中只保留一段 HTML 代码?

java - Android 设置壁纸 - 一般尺寸?

javascript - 如何识别哪些事件已通过 JavaScript 绑定(bind)?

jquery - 清爽的美容秘诀

VMWare 上的 Android 屏幕旋转

screen - 有没有办法检测屏幕截图或录制?