javascript - 如何使用 jQuery 动画从上到下滑动不同的 div?

标签 javascript jquery jquery-ui

我想将第一个 div 滑动到最后一个 div 与动画同步。 你能在 jquery 上帮助我吗?

$(function() {
    $('#switch').click(function() {
        $('#one').animate({top:$("#three").offset().top}); 


    });
});

jsfiddle

最佳答案

由于资料少,我制作了一个demo 4,大家试试看,

HTML

<div style="position:relative;">
    <div id="one" class="square">1</div>
    <div id="two" class="square">2</div>
    <div id="three" class="square">3</div>
    <div id="four" class="square">4</div>
</div>
<div class="clear"></div>
<br />
<button id="switch">switch positions</button>

CSS

#one {
    top:0;
    background:blue;
}
#two {  
    top:50px;
    background:red;
}
#three {
    top:100px;
    background:purple;
}
#four {
    top:150px;
    background:orange;
}
.square {
    text-align:center;
    line-height:50px;
    color:white;
    font-weight:bold;
    width:50px;
    height:50px;
    position:absolute;
}
.clear {
    clear:both;
}
#switch{
    position:relative;
    left:150px;
}

Jquery

$(function () {
    $('#switch').click(function () {
        var t = ($('.square').length-1) * $('.square:first').height();
        $('.square:first').animate({
            top: t
        }, function () {
            $('.square:gt(0)').each(function (i, v) {
                var $el = $('.square:eq(' + (i + 1) + ')');
                $el.animate({
                    top: i * $el.height()
                })
            });
            o = $(this).clone();
            $(this).remove();
            o.insertAfter($('.square:last'));
        });
    });
});

$(function () {
    $('#switch').click(function () {
        var t = ($('.square').length-1) * $('.square:first').height();
        $('.square:first').animate({
            top: t
        }, function () {
            $('.square:gt(0)').each(function (i, v) {
                var $el = $('.square:eq(' + (i + 1) + ')');
                $el.animate({
                    top: i * $el.height()
                })
            });
            o = $(this).clone();
            $(this).remove();
            o.insertAfter($('.square:last'));
        });
    });
});
#one {
    top:0;
    background:blue;
}
#two {  
    top:50px;
    background:red;
}
#three {
    top:100px;
    background:purple;
}
#four {
    top:150px;
    background:orange;
}
.square {
    text-align:center;
    line-height:50px;
    color:white;
    font-weight:bold;
    width:50px;
    height:50px;
    position:absolute;
}
.clear {
    clear:both;
}
#switch{
    position:relative;
    left:150px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div style="position:relative;">
    <div id="one" class="square">1</div>
    <div id="two" class="square">2</div>
    <div id="three" class="square">3</div>
    <div id="four" class="square">4</div>
</div>
<div class="clear"></div>
<br />
<button id="switch">switch positions</button>

你也可以试试Swap Plugin .

关于javascript - 如何使用 jQuery 动画从上到下滑动不同的 div?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31933999/

相关文章:

jquery - 计算并显示在主div中被拖动到y轴后的div高度

javascript - 在 VueJS 中使用 POST 请求提交表单

javascript - 按条件过滤,带断路取n

javascript - AJAX 将类添加到表格单元格

jquery - 如何修复 Jquery UI slider 显示负值

jquery-ui - 如何在 YouTube 视频/SlideShare PPT 上显示 jQuery 对话框?

java - Linux + 动态插入的 Java applet = 在 NPObject 上调用方法时出错

javascript - JS重定向不加载静态内容

c# - 在 jquery 完整日历中更改事件的背景颜色

javascript - 脚本加载冲突?包含 Adob​​e Edge Animation 时对象没有方法错误