当拖动#right
元素时,我想调整其父级的大小。我的示例不起作用,我无法理解为什么。有什么提示如何修复它吗?
以防万一,我在尝试创建一个可调整大小的滚动条,类似于这个。但目前,我只对右侧的元素感兴趣。
当拖动#right
元素时,我想调整其父级的大小。我的示例不起作用,我无法理解为什么。有什么提示如何修复它吗?
以防万一,我在尝试创建一个可调整大小的滚动条,类似于这个。但目前,我只对右侧的元素感兴趣。
#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:
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);
}
});
您可以使用传统的纯JavaScript来实现此操作。
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
});
right.draggable({
containment: cont,
drag: function(e, ui){
dragged = ui.position.left;
nav.width(originNavWidth + dragged);
ui.position.left = nav.position.left;
}
})
nav
的宽度大于以前时,它就无法工作。 - Derek 朕會功夫