javascript - 如何拖动和调整 DIV 元素的大小

标签 javascript html css

我想在 HTML 页面中调整和拖动 DIV 元素。我使用的代码如下。 Javascript:

 <script type="text/javascript" src="dragresize.js"></script>
<script language="javascript" type="text/javascript"> 
var dragresize = new DragResize('dragresize',
 { minWidth: 50, minHeight: 50, minLeft: 20, minTop: 20, maxLeft: 600, maxTop: 600 });

// Optional settings/properties of the DragResize object are:
//  enabled: Toggle whether the object is active.
//  handles[]: An array of drag handles to use (see the .JS file).
//  minWidth, minHeight: Minimum size to which elements are resized (in pixels).
//  minLeft, maxLeft, minTop, maxTop: Bounding box (in pixels).

// Next, you must define two functions, isElement and isHandle. These are passed
// a given DOM element, and must "return true" if the element in question is a
// draggable element or draggable handle. Here, I'm checking for the CSS classname
// of the elements, but you have have any combination of conditions you like:

dragresize.isElement = function(elm)
{
 if (elm.className && elm.className.indexOf('drsElement') > -1) return true;
};
dragresize.isHandle = function(elm)
{
 if (elm.className && elm.className.indexOf('drsMoveHandle') > -1) return true;
};

// You can define optional functions that are called as elements are dragged/resized.
// Some are passed true if the source event was a resize, or false if it's a drag.
// The focus/blur events are called as handles are added/removed from an object,
// and the others are called as users drag, move and release the object's handles.
// You might use these to examine the properties of the DragResize object to sync
// other page elements, etc.

dragresize.ondragfocus = function() { };
dragresize.ondragstart = function(isResize) { };
dragresize.ondragmove = function(isResize) { };
dragresize.ondragend = function(isResize) { };
dragresize.ondragblur = function() { };

// Finally, you must apply() your DragResize object to a DOM node; all children of this
// node will then be made draggable. Here, I'm applying to the entire document.
dragresize.apply(document);
</script>

CSS

<style type="text/css">
.drsElement { 
position: relative;
 border: 1px solid #333;
}

.drsMoveHandle {
 height: 10px;
 border-bottom: 1px solid #666;
 cursor: move;
}

.dragresize {
 position: absolute;
 width: 5px;
 height: 5px;
 font-size: 1px;
 background: #EEE;
 border: 1px solid #333;
}


.dragresize-tl {
 top: -8px;
 left: -8px;
 cursor: nw-resize;
}
.dragresize-tm {
 top: -8px;
 left: 50%;
 margin-left: -4px;
 cursor: n-resize;
}
.dragresize-tr {
 top: -8px;
 right: -8px;
 cursor: ne-resize;
}

.dragresize-ml {
 top: 50%;
 margin-top: -4px;
 left: -8px;
 cursor: w-resize;
}
.dragresize-mr {
 top: 50%;
 margin-top: -4px;
 right: -8px;
 cursor: e-resize;
}

.dragresize-bl {
 bottom: -8px;
 left: -8px;
 cursor: sw-resize;
}
.dragresize-bm {
 bottom: -8px;
 left: 50%;
 margin-left: -4px;
 cursor: s-resize;
}
.dragresize-br {
 bottom: -8px;
 right: -8px;
 cursor: se-resize;
}
</style>

HTML

<table cellpadding="5" cellspacing="0" width="100%" style="margin:auto;">
            <tr>
                <td><div class="drsElement drsMoveHandle" id="output1" style="font-weight:bold;height:20px;margin-top:40px"></div></td>
            </tr>
            <tr>
                <td><div class="drsElement drsMoveHandle" id="output2" style="height:40px;margin-top:30px"></div></td>
            </tr>   
            <tr>
                <td><div class="drsElement drsMoveHandle" id="output3" style="height:50px;margin-top:40px;"></div></td>
            </tr>   
        </table>

问题是我不能拖动它到任何我可以调整大小但不能从原始大小减小 DIV 元素大小的地方,我不知道为什么......?

最佳答案

我看到您正在使用来自 TwinHelix http://www.twinhelix.com/javascript/dragresize/ 的代码

1) 请始终清楚地说明您从何处获得所使用的代码,以表彰作者。您应该告诉我们您正在使用 TwinHelix 提供的软件包。不要有意或无意地让人们认为您展示的代码是您自己的。如果您在某处找到它,请告诉我们。

2) 我已经使用该代码探索了一些可能性,我没有遇到任何问题。我可以将 div 调整为任何大小,包括小于原始大小。

我建议您访问 TwinHelix 站点并获取当前代码,然后重新开始并确保您正确使用它。

您可以设置 div 的大小限制、上下移动的限制等。

确保您至少了解它正在做什么以及如何编写 div 代码来完成您想要完成的事情的基础知识。

获取 TwinHelix 的演示页面,将其与所有 Javascript 等一起复制到您的系统,并让它在您的系统上运行。然后让该页面按照您想要的方式工作 - 分区大小、内容等 - 一旦您的分区按照您想要的方式工作,将代码集成到您自己的页面中。永远不要从某个地方获取代码并直接跳入并将其放入您的页面中。隔离事物,制作一个仅包含新事物的单独测试页面,在此示例中拖动并调整大小,然后将其集成到您的页面中。

在将其集成到您的页面中的同时,试图将其更改为您希望的方式工作会给您带来更多无法处理的问题。

在进行任何编程时,将其分解成易于处理的 block ,让它们按照您想要的方式工作,然后将它们组合在一起形成整体(我在“这个”领域工作了 39 多年,只有一个一点经验)。

阅读示例代码和其他代码中的注释,至少了解由谁来定义部门。

除非您确定自己在做什么,否则不要修改任何代码或 CSS,否则您可能会引入不知道问题出处的问题。

3) 除了几个问题,TwinHelix 代码运行良好。我的问题在于尝试扩展功能并添加新功能。我让事情按照我想要的方式工作,但有一些我根本不喜欢的怪癖。所以,我正在研究其他一些方法。我不知道什么时候能完成,但当我完成时,我会在我的网站上发布一个页面 http://www.bobnovell.com --- 请注意,现在(截至 2012 年 12 月 28 日)内容不多,但我有很多时间要补充。

4) 要获得相当不错的拖动功能,请查看 http://tool-man.org/ToolManDHTML/ - “基本可拖动图层”它没有调整大小但效果很好。唯一的问题与设置 zIndex 有关。我已经添加了代码来处理 mouseDowns 以带来 split

5) 我不希望任何人查看您在这个问题中提供的所有代码(JavaScript、CSS、HTML)并给您建议——它们实在是太大了。

此外,如果您有任何问题,请联系作者。

关于javascript - 如何拖动和调整 DIV 元素的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11288739/

相关文章:

html - 如何使用CSS创建动态 Angular 框?

javascript - 有谁知道即使页面已刷新,我如何保留 Javascript 变量?

javascript - jQuery 显示/隐藏点击错误

html - 在 HTML 页面中显示 XML 数据

php - 需要复选框选择php

CSS alpha 不透明度 IE8 溢出被隐藏

javascript - 下拉菜单被 chop

javascript - for循环内的jQuery自定义事件仅在最后一次迭代时触发

javascript - 如何在 Nodejs 调试控制台 View 中更改对象的字符串表示

html - 两侧的空白区域 (html/css)