简体   繁体   English

拖动内部元素时,调整父元素的大小。 (jqueryUI可拖动)

[英]Resize parent element when inner one is dragged. (jqueryUI draggable)

jsfiddle example jsfiddle示例

When #right element is dragged, I want to resize its parent. 拖动#right元素后,我想调整其父元素的大小。 My example works incorrectly and I'm failing to comprehend why. 我的示例无法正常工作,我无法理解原因。 Any hints how to fix it? 有任何提示如何解决?

Just in case, what I'm trying to do here is to create a resizable scroll similar to this one . 为了以防万一,我在这里要做的是创建一个可调整大小的滚动条,类似于滚动条。 But for now, I'm only interested in the right element. 但是目前,我只对正确的元素感兴趣。

The simplest solution is as follows (note the #left and #right changes in SCSS): 最简单的解决方案如下(请注意SCSS中的#left和#right更改):

SCSS: SCSS:

#container {
    width: 500px;
    height: 100px;
    background: #f9f9f9;
}

#nav {
    width: 100px;
    height: 100px;
    background: #e9e9e9;
    cursor: move;
}
#left {
    width: 10px;
    height: 100px;
    position: absolute;
    top:0;
    left:0px;
    background: #a9a9a9;
    cursor: w-resize;
    &:hover {
        background: #999;
    }
}
#right {
    width: 10px;
    height: 100px;
    position:absolute;
    top:0;
    right:0px;
    background: #a9a9a9;
    cursor: e-resize;
    &:hover {
        background: #999;
    }
}

JavaScript: JavaScript:

var cont = $("#container")
var nav = $("#nav")
var left = $("#left")
var right = $("#right")

nav.draggable({
    containment: cont
});

right.draggable({
    containment: cont,
    drag: function(e, ui) {
        nav.width(ui.position.left);
    }
});

The only problem was you were getting too fancy with your JavaScript. 唯一的问题是您太喜欢JavaScript。 The ui.position.left had all the information you needed. ui.position.left具有您需要的所有信息。 All I did was change the content from floating to position:absolute; 我所做的就是将内容从浮动更改为位置:绝对;

You can use the good ol' pure JavaScript to do that 您可以使用优质的纯JavaScript来做到这一点
http://jsfiddle.net/DerekL/cCvGD/ http://jsfiddle.net/DerekL/cCvGD/

var oriX = 0,
    oriW = 0,
    mousedown = false;
right.mousedown(function (e) {
    oriX = $(this).offset().left;             //store the initial x and width
    oriW = nav.width();
    mousedown = true;                         //mousedown sets to true
    e.stopPropagation();                      //prevent nav from being dragged
})
$(window).mousemove(function (e) {
    if(mousedown == true) {                   //only when mouse is down (dragging)
        nav.width(oriW + (e.clientX - oriX)); //calculate the width
    }
}).mouseup(function () {
    mousedown = false;                        //mousedown sets to false
});

Both left and right: http://jsfiddle.net/DerekL/cCvGD/1/ 左右两边: http : //jsfiddle.net/DerekL/cCvGD/1/

The problem is that the you are changing the width of your nav container, and this is moving out the right element. 问题是您正在更改导航容器的宽度,而这正移出正确的元素。 So while you are dragging it, it moves twice as far. 因此,当您拖动它时,它会移动两倍。

You can get get around this by resetting the location of your right element in the dragable method 您可以通过在dragable方法中重置右侧元素的位置来解决此问题

right.draggable({
    containment: cont,
    drag: function(e, ui){
        dragged = ui.position.left;
        nav.width(originNavWidth + dragged);
        ui.position.left = nav.position.left;
    }
})

see: http://jsfiddle.net/vf4eS 看到: http : //jsfiddle.net/vf4eS

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

相关问题 当父项可拖动时,防止拖动子元素 - Prevent child element from being dragged when parent is draggable 拖动一个元素时如何使元素可拖动 - How to make to element draggable when one element is dragged JqueryUI Draggable - 自动调整父容器大小 - JqueryUI Draggable - Auto resize parent container jQuery UI draggable - 当内部元素大于父元素时,约束内部元素 - jQuery UI draggable - constrain inner element within parent when inner element is larger than parent jQueryUI - 检测元素是否被拖动 - jQueryUI - Detect if element is beeing dragged jQuery UI可拖动:使用辅助程序时访问被拖动的元素:克隆 - jQuery UI draggable: Access dragged element when using helper: clone 使用html5 draggable属性时,保留拖动A元素的外观 - Preserve appearance of dragged A element when using html5 draggable attribute 在拖动一个jqueryUI可拖动元素时,触发对其他多个元素的拖动 - On dragging one jqueryUI draggable element trigger drag on other multiple elements 将可拖动元素连接到jQueryUI中的可排序元素时,“ appendTo”方法无法正常运行 - “appendTo”-method not functioning correctly when connecting a draggable to a sortable element in jQueryUI jqueryui可调整大小和可拖动元素 - jqueryui resizeable and draggable element is covered
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM