简体   繁体   English

让div坚持到其他div的一边

[英]Make div stick to side of other div

I'm trying to make a div appear to slide out to the right of another div, using jQuery. 我正在尝试使用jQuery将div显示为滑出另一个div的右侧。

I've based my solution on this answer , by using position:absolute , and getting the position of the trigger element in jQuery, and positioning the popover accordingly. 我通过使用position:absolute ,并在jQuery中获取触发器元素的位置,并相应地定位弹出窗口,将我的解决方案基于此答案

You can see the result of what I have so far on this JSFiddle I have made: Result / Source . 你可以看到我到目前为止所做的JSFiddle的结果结果 / 来源

The problem is, because the popover div uses position:absolute , if I resize the page, or a scrollbar appears / disappears - the popover becomes mis-aligned to the trigger div. 问题是,因为popover div使用position:absolute ,如果我调整页面大小,或者滚动条出现/消失 - 弹出窗口会与触发器div不一致。

How can I make the popover div stick to the right-side of the trigger div? 如何让popover div粘贴到触发器div的右侧?

Rather than relying on jQuery for this, you can make two elements stick together in CSS by making them inline elements, forcing them to touch, and setting the CSS of the container to white-space: nowrap . 您可以通过将两个元素组合在一起,使它们成为内联元素,强制它们触摸,并将容器的CSS设置为white-space: nowrap ,而不是依赖于jQuery。

HTML: HTML:

<div id="main">
    <p>If the draw is popped out, and you resize the page, it breaks, because it has position:absolute</p>
    <div id="clickable">
        <div id="trigger">
            <span>Click here - click away to hide</span>
        </div><div id="pop">
            <span>Now Resize Page</span>
        </div>
    </div>
</div>

Note that </div><div id="pop"> is not a typo. 请注意, </div><div id="pop">不是拼写错误。 To force inline elements to touch, you need to remove all whitespace in your markup. 要强制内联元素触摸,您需要删除标记中的所有空格。

CSS: CSS:

#main {
    width:300px;
    margin:50% auto;
    margin-top:0;
    margin-bottom:0;
    background-color:#EEE;
    padding:10px;
}
#clickable { white-space: nowrap; }

#trigger {
    background-color: #FFF;
    width: 100%; 
    border: 4px solid #ddd;
    display: inline-block; }
#trigger span { margin: 5px; display: block; }

#pop {
    background-color: #333;
    color: #EEE;
    height: 38px;
    opacity: 0;
    display: none; }
#pop span { 
    margin: 10px; 
    display: block; }

JS: JS:

$('#trigger').click(function(event) {
    event.stopPropagation();
    var width = $(this).outerWidth();
    var offset = $(this).offset();
    var top = (offset.top+4)+'px';
    var left = (offset.left+width)+'px';
    $('#pop').css({
        'top' : top,
        'left' : left,
        'display' : 'inline-block',
        'opacity' : 0,
        'width' : 0
    }).animate({
        'width' : '140px',
        'opacity' : 1
    });
});

$('html').click(function(event) {
    if (!($(event.target).closest('#pop').length)) {
        $('#pop').animate({'width':0,'opacity':0},500,
                           function() {
            $(this).hide();
        });
    }
});

Preview: http://jsfiddle.net/Wexcode/4kg2W/3/ 预览: http//jsfiddle.net/Wexcode/4kg2W/3/

将触发器div与position:relative和其中的popover一起放置为position:absolute,这样,popover将具有绝对位置,但总是相对于触发器div。

You could adjust the positioning when the browser resizes: 可以在浏览器调整大小时调整定位:

$(window).resize(function(){
    $("#trigger").trigger('click');
});

你可以使用位置属性

<div style="position: absolute;right:-10px ;top:0"></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM