jquery - 将另一个 div 插入时将其推出

标签 jquery html css slider

现在我有一个基本模板,允许我将 4 个不同的 div 滑动到一个框架中

here是网站 - 尝试点击眼睛、 Nose 或前额

正如您所看到的,div 滑入了框架,但是,

现在我想添加功能,当其中一个 div 滑入时将中心 div 滑出(因此,如果屏幕上的 div 中的顶部 div 滑动,则屏幕上的 div 将向下滑动,如果屏幕中的左侧 div 滑动,则屏幕上的 div 将滑动对等)

有什么办法可以做到这一点?我认为它会对过渡背景产生很大的影响

谢谢大家

凯蒂

最佳答案

您需要做的是将您的内容 div 绝对定位在一个固定的中心 div 中。这将允许您相对于页面中心在内容 div 周围移动。我正在使用 css-transitions 来应用幻灯片效果。所以滑动只适用于现代浏览器,但它会很好地降级到过时的 IE 浏览器。

这是带有工作演示的 fiddle :http://jsfiddle.net/WVPDH/263/

您显然需要对这段代码进行一些修改才能与您的页面一起使用,但这应该不会太难。

我已经发布了下面的代码以防 fiddle 链接变坏:

HTML:

<div id="fullContainer">
    <div id="right">

    </div>
    <div id="left">

    </div>
    <div id="top">

    </div>
    <div id="bottom">

    </div>
</div>
<div id="centerContainer">
    <div id="relativeContainer">
        <div id="content">
            This is where your face should go.  Notice that I placed it within a centering div.  
            This will enable the face to be absolutely positioned, and allow for you to modify 
            it's position when the side-bars slide in.
            <div data-move="left">Open Left</div>
            <div data-move="right">Open Right</div>
            <div data-move="top">Open Top</div>
            <div data-move="bottom">Open Bottom</div>
        </div>
    </div>
</div>

CSS:

#centerContainer {
    position:fixed;
    top:50%;
    left:50%;
    width:0;
    height:0;
}
#relativeContainer {
    position:relative;
}
#fullContainer {
    position:fixed;
    width:100%;
    height:100%;
    top:0;
    left:0;
}
#content {
    position:absolute;
    width:300px;
    height:400px;
    top:-200px;
    left:-150px;
    background:#BADA55;
    border:1px solid #444;
    padding:10px;
    -webkit-transition: all 0.5s ease;
    -moz-transition: all 0.5s ease;
    -o-transition: all 0.5s ease;
    transition: all 0.5s ease;
}
#content.right {
    left:-250px;
}
#content.left {
    left:-50px;
}
#content.bottom {
    top:-300px;
}
#content.top {
    top:-100px;
}

#content div {
    cursor:pointer;
    color:blue;
    text-decoration:underline;
    margin-top:15px;
    text-align:center;
}
#left {
    position:absolute;
    top:0;
    left:-125px;
    height:100%;
    width:100px;
    background:blue;
    border:1px solid #444;
    padding:10px;
    -webkit-transition: all 0.5s ease;
    -moz-transition: all 0.5s ease;
    -o-transition: all 0.5s ease;
    transition: all 0.5s ease;
}

#left.opened {
    left:0;
}

#right {
    position:absolute;
    top:0;
    right:-125px;
    height:100%;
    width:100px;
    background:green;
    border:1px solid #444;
    padding:10px;
    -webkit-transition: all 0.5s ease;
    -moz-transition: all 0.5s ease;
    -o-transition: all 0.5s ease;
    transition: all 0.5s ease;
}

#right.opened {
    right:0;
}

#top {
    position:absolute;
    left:0;
    top:-125px;
    width:100%;
    height:100px;
    background:yellow;
    border:1px solid #444;
    padding:10px;
    -webkit-transition: all 0.5s ease;
    -moz-transition: all 0.5s ease;
    -o-transition: all 0.5s ease;
    transition: all 0.5s ease;
}

#top.opened {
    top:0;
}

#bottom {
    position:absolute;
    left:0;
    bottom:-125px;
    width:100%;
    height:100px;
    background:red;
    border:1px solid #444;
    padding:10px;
    -webkit-transition: all 0.5s ease;
    -moz-transition: all 0.5s ease;
    -o-transition: all 0.5s ease;
    transition: all 0.5s ease;
}

#bottom.opened {
    bottom:0;
}

JS:

function SlideOut(element){

    $(".opened").removeClass("opened");
    $("#"+element).addClass("opened");
    $("#content").removeClass().addClass(element);

}
$("#content div").click(function(){

    var move = $(this).data('move');

    SlideOut(move);

});
​

关于jquery - 将另一个 div 插入时将其推出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13061939/

相关文章:

css - 向右浮动 Bootstrap 模态

html - 表格上的 CSS 圆 Angular 边框

javascript - 为什么ajax调用url以#结尾?

jquery - 向 jquery 函数发送参数

javascript - onchange 仅适用于表中的第一行

html - 如何制作具有特定首选项的 3 列表

javascript - 向上滑动服务页面和 slider 不工作/显示

javascript - 表中的删除按钮,jQuery

javascript - Div 高度始终为零 - 如何使其自动假定高度

html - 带有响应图像的背景叠加