jQuery 手动调整大小的 DIV
我正在尝试创建一个可调整大小的 div,而不使用 jQuery 的界面库。
var myY = 0;
var mouseDown = false;
var originalHeight = 0;
function resize(e){
if(mouseDown == true){
$("#cooldiv").height(originalHeight+e.pageY-myY);
}
}
$(document).ready(function(){
$().mouseup(function(e){
myY = 0;
mouseDown = false;
originalHeight = 0;
$().unbind("mousemove", resize);
});
$("#resizeBar").mousedown(function(e){
myY = e.pageY;
originalHeight = $("#cooldiv").height();
mouseDown = true;
$().bind("mousemove", resize);
});
});
...
<div id="cooldiv" style="width: 500px; height: 300px; background-color: #cccccc; position: relative;">
<div id="resizeBar" style="height: 10px; width: 500px; background-color: #aaaaaa; position: absolute; bottom: 0;"></div>
</div>
第一次调整大小工作正常(即 mousedown、mousemove 然后 mouseup),但在后续(mousedown+mousemove)中,浏览器尝试拖动整个 resizeBar div,而不是正确调整其父容器的大小。 在 mouseup 上,div 然后开始在 mousemove 上调整“cooldiv”的大小,而不需要任何 mousedown,直到进一步单击鼠标。
这些问题不会出现在 Internet Explorer 中。
I'm trying to create a resizable div without using jQuery's interface library.
var myY = 0;
var mouseDown = false;
var originalHeight = 0;
function resize(e){
if(mouseDown == true){
$("#cooldiv").height(originalHeight+e.pageY-myY);
}
}
$(document).ready(function(){
$().mouseup(function(e){
myY = 0;
mouseDown = false;
originalHeight = 0;
$().unbind("mousemove", resize);
});
$("#resizeBar").mousedown(function(e){
myY = e.pageY;
originalHeight = $("#cooldiv").height();
mouseDown = true;
$().bind("mousemove", resize);
});
});
...
<div id="cooldiv" style="width: 500px; height: 300px; background-color: #cccccc; position: relative;">
<div id="resizeBar" style="height: 10px; width: 500px; background-color: #aaaaaa; position: absolute; bottom: 0;"></div>
</div>
The first resize works fine(i.e. mousedown, mousemove then mouseup), but on subsequent (mousedown+mousemove)s, the browser attempts to drag the whole resizeBar div instead of properly resizing its parent container. On mouseup, the div then starts resizing "cooldiv" on mousemove without any mousedown required, until a further click of the mouse.
These problems don't show up in Internet Explorer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我偶尔能够破坏调整大小功能,在该功能中我会得到“不允许”光标,并且该框不会动态调整大小,尽管当我放开鼠标时它会适当调整大小并保持这种状态。
在
resize
函数末尾添加return false;
似乎可以阻止浏览器尝试选择/拖动调整大小栏。 我在有限的环境中工作,所以我只能使用 ie8(以及 ie7 模式下的 ie8)进行测试。我必须将您对
$()
的调用替换为$(document)
才能正常工作。 我还建议将 mousemove 绑定移出 mousedown 处理程序,并删除取消绑定调用。i was occasionally able to break the resize functionality, where i would get the "not allowed" cursor and the box would not dynamically resize, although when i let go of the mouse it would resize appropriately and stay that way.
adding
return false;
to the end of theresize
function seems to stop the browser from trying to select/drag the resize bar. i'm working in a limited environment so i can only test with ie8 (and ie8 in ie7 mode).i had to replace your calls to
$()
with$(document)
to get this working. i would also recommend moving themousemove
binding out of themousedown
handler, and removing the unbinding call.这是一个使用“hr”标签作为分隔符(在 2 个 div 之间)的示例:
Javascript:(使用 JQuery):
CSS(可选):
This is an example with a "hr" tag as separator (between 2 divs):
Javascript: (using JQuery):
CSS (optional):
尝试添加
到 mouseup 函数的末尾。
Try adding
to the end of your mouseup function.