最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery UI draggable with absolute position and bottom property - Stack Overflow

programmeradmin4浏览0评论

I am using jQuery UI to create a draggable <div>. The <div> is absolutely positioned to the bottom-right or top-left using CSS:

.dragbox {
    position: absolute;
    width: 140px;
    min-height: 140px; /* I need to use min-height */
    border: 3px solid black;
    padding: 10px;
}
.bottom {
    bottom: 5px; /* causes box to resize when dragged */
    right: 5px;
}
.top {
    top: 5px; /* does not cause box to resize */
    left: 5px;
}

The HTML looks like this:

<div class="dragbox bottom">
    Bottom Box :(
</div>
<div class="dragbox top">
    Top Box :)
</div>

The JavaScript is $('.dragbox').draggable(); to make the <div> elements draggable. The top-left <div> works as expected, but the bottom-right <div> resizes when dragged. If I change the min-height poperty of .dragbox to height I see the expected behavior for both boxes, but I need to use min-height for this because I don't know the length of the inner content.

How do I position the <div> in the bottom-right and enable dragging without having the <div> resize when dragged? The top-left and bottom-right <div> elements should behave the same way.

DEMO (I use Chrome 27 and Safari 6)

I am using jQuery UI to create a draggable <div>. The <div> is absolutely positioned to the bottom-right or top-left using CSS:

.dragbox {
    position: absolute;
    width: 140px;
    min-height: 140px; /* I need to use min-height */
    border: 3px solid black;
    padding: 10px;
}
.bottom {
    bottom: 5px; /* causes box to resize when dragged */
    right: 5px;
}
.top {
    top: 5px; /* does not cause box to resize */
    left: 5px;
}

The HTML looks like this:

<div class="dragbox bottom">
    Bottom Box :(
</div>
<div class="dragbox top">
    Top Box :)
</div>

The JavaScript is $('.dragbox').draggable(); to make the <div> elements draggable. The top-left <div> works as expected, but the bottom-right <div> resizes when dragged. If I change the min-height poperty of .dragbox to height I see the expected behavior for both boxes, but I need to use min-height for this because I don't know the length of the inner content.

How do I position the <div> in the bottom-right and enable dragging without having the <div> resize when dragged? The top-left and bottom-right <div> elements should behave the same way.

DEMO (I use Chrome 27 and Safari 6)

Share Improve this question edited Jun 25, 2013 at 14:23 ChrisP asked Jun 25, 2013 at 14:09 ChrisPChrisP 5,9421 gold badge35 silver badges36 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 17

jQuery's draggable works by manipulating the css top and left values. When a box has both top and bottom values set (and no static height), the box will stretch to match both properties. This is what's causing the resizing.

A workaround could be to set a static height on the box, or if that's not an option, set the bottom value to "auto" when the draggable is created, and fetch the current "top" position at the same time to make sure the element stays put.

$('.dragbox').draggable({
    create: function( event, ui ) {
        $(this).css({
            top: $(this).position().top,
            bottom: "auto"
        });
    }
});

http://jsfiddle.net/expqj/8/

@xec gave me the main idea and I changed it this way and it worked for me;

 jQuery("#container").draggable({
                    start: function (event, ui) {
                        $(this).css({
                            right: "auto",
                            top: "auto"
                        });
                    }
                });

for me, worked this way (for my bottom, left absolute positioned div):

        $("#topBarUpDownDiv").draggable({ axis: "x",
            stop: function( event, ui ) {
                var elem=$(this)[0];
                $(elem).css({ bottom: $(this).position().bottom, top: "auto" });
           }
        });
发布评论

评论列表(0)

  1. 暂无评论