最近我使用JQuery Draggable和Resizable插件遇到了一些问题。在寻找解决方案时,我在许多不同的地方发现了一些非常零散的代码,最终得出了一个干净的解决方案,似乎对我来说几乎完美地运作。
我想与其他人分享我的发现,如果他们也遇到了这个问题。
我有一个包含IFrame的div。这个div必须可以调整大小和拖动。很简单-只需将JQuery Draggable和Resizable添加到div中:
$("#Div").draggable();
$("#Div").resizable();
当你拖动另一个包含 iframe 的 div 或尝试调整当前 div 的大小时,一切都很顺利,直到移动到当前 iframe 上方。当在 iframe 上面时,两个功能都会停止。
解决方案:
$("#Div").draggable({
start: function () {
$(".AllContainerDivs").each(function (index, element) {
var d = $('<div class="iframeCover" style="zindex:99;position:absolute;width:100%;top:0px;left:0px;height:' + $(element).height() + 'px"></div>');
$(element).append(d);});
},
stop: function () {
$('.iframeCover').remove();
}
});
$("#Div").resizable({
start: function () {
$(".AllContainerDivs").each(function (index, element) {
var d = $('<div class="iframeCover" style="z-index:99;position:absolute;width:100%;top:0px;left:0px;height:' + $(element).height() + 'px"></div>');
$(element).append(d);
});
},
stop: function () {
$('.iframeCover').remove();
}
});
享受吧!
注:为了创建窗口,这里有一些额外的代码,当选择它们时,它们会被带到其他可拖动元素的前面:
在可拖动的开始函数中 -
var maxZ = 1;
$(".AllContainerDivs").each(function (index, element) {
if ($(element).css("z-index") > maxZ) {
maxZ = $(element).css("z-index");
}
});
$(this).css("z-index", maxZ + 1);