jQueryUI : Sliding two divs at the same time

标签 jquery css jquery-ui

我有一个网页,其中有两个 div 占据了整个空间。我想一次向它们显示一个,并在它们之间使用 JQueryUI 幻灯片过渡,以便一个 div 滑向左侧,并被从右侧滑入的另一个 div 取代。 不幸的是,现在,由于两个动画同时发生,第二个 div 出现在第一个 div 的下方,然后向上滑动以在动画结束时替换第一个 div。如何定位第二个 div 以使滑动 Action 无缝发生?

代码:

<script type="text/javascript">
    $('#schedule').hide();
    $( '#changer' ).click(function() {
            $("#roster").toggle( "slide", {direction: "left"}, "fast" );
            $("#schedule").toggle("slide", { direction: "right"}, "fast");

            if ($("#changer").html() == 'Schedule View')
                $("#changer").html('Roster View');
            else
                $("#changer").html("Schedule View");
        });
</script>

最佳答案

考虑这个 fiddle :http://jsfiddle.net/FkNa2/2/

相关代码是css定位,

// html
<p>some content here </p>
<div class="container">
    <div id="div1"></div>
    <div id="div2"></div>
</div>
<button>Toggle</button>

// css
#div1{
    width: 500px;
    height: 100px;
    background-color: blue;
    position: absolute;
    top: 0px;
    left: 0px;
}

#div2{
    width: 500px;
    height: 100px;
    background-color: red;
    position: absolute;
    top: 0px;
    left: 0px;
}

.container {
    width: 800px;
    position: relative;
    left: 100px;
    height: 100px;
}

诀窍是确保两个 div 在视口(viewport)中占据相同的位置。这是通过将它们绝对定位到相同的坐标来完成的。请注意,这些坐标是相对于外部 div 的。引用这个问题的答案https://stackoverflow.com/a/10426717/30007

Both “relative” and “absolute” positioning are really relative, just with different framework. “Absolute” positioning is relative to the position of another, enclosing element. “Relative” positioning is relative to the position that the element itself would have without positioning.

It depends on your needs and goals which one you use. “Relative” position is suitable when you wish to displace an element from the position it would otherwise have in the flow of elements, e.g. to make some characters appear in a superscript position. “Absolute” positioning is suitable for placing an element in some system of coordinates set by another element, e.g. to “overprint” an image with some text.

As a special, use “relative” positioning with no displacement (just setting position: relative) to make an element a frame of reference, so that you can use “absolute” positioning for elements that are inside it (in markup).

关于jQueryUI : Sliding two divs at the same time,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16137520/

相关文章:

javascript - 如何在 jQuery 中随机删除或删除数组值?

javascript - 修复了向下滚动时标题摇晃的问题

asp.net - 本地主机与开发服务器上 IE8 的奇怪显示行为

jquery - 模态下方的下拉内容

javascript - jQuery Unresolved 冲突

javascript - 查找DIV id内容并通过AJAX加载

jquery - drupal 6 - 最有效的 jquery 版本?

html - 将页脚保持在底部

javascript - 为什么 JQuery 不显示我的自动完成字段?

javascript - 为什么不计算这个div高度?