[英]how to drag and resize DIV element
我想在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等,并使其在您的系统上运行。 然后使该页面按照您想要的方式工作 - 分区大小,内容等 - 一旦您按照自己的方式工作,就可以将代码集成到您自己的页面中。 切勿从某处获取代码并直接跳入并将其放入页面中。 隔离事物,在新的事物中创建一个单独的测试页面,在此示例中拖动并调整大小,然后将其集成到您的页面中。
尝试将其更改为按照您希望的方式工作,同时将其集成到您的页面中将导致您遇到的问题多于您可以处理的问题。
在进行任何编程时,将其分解为易于处理的块,让它们以您想要的方式工作,然后将它们组合在一起以构成整体(我已经在“此”工作了39年以上并且只有一点经验)。
阅读示例代码和其他代码中的注释,并至少了解定义分部的人员。
除非你确定自己在做什么,否则不要修改任何代码或CSS,否则你可能会引入问题而不知道它们来自何处。
3)除了几个问题,TwinHelix代码运行良好。 我的问题是尝试扩展功能并添加新功能。 我按照我想要的方式工作,但有一些我根本不喜欢的怪癖。 所以,我正在研究其他一些方法。 我不知道什么时候能完成它,但是当我这样做时,我会在我的网站http://www.bobnovell.com上发布一个页面 - 请注意现在没有多少(截至2012年12月28日)但是,当我有时间时,我还有很多要补充的内容。
4)对于一个相当好的拖动功能,看看http://tool-man.org/ToolManDHTML/ - “基本可拖动图层”它没有调整大小但它运作良好。 唯一的问题与设置zIndex有关。 我添加了代码来处理mouseDowns带来的分歧
5)我不希望任何人看到你在这个问题中提供的所有代码 - JavaScript,CSS,HTML - 并给你建议 - 它太大了。
另外,如果您有疑问,请联系作者。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.