简体   繁体   English

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

[英]how to drag and resize DIV element

I want to resize and drag DIV element in a HTML page. 我想在HTML页面中调整大小并拖动DIV元素。 The code I used is as follows. 我使用的代码如下。 Javascript: 使用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 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 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>

The issue is that I can't drag it any where I can resize but can't reduce the DIV element size from the original size, I don't know why..?. 问题是我无法拖动到任何我可以调整大小的地方,但无法将DIV元素大小从原始大小减小,我不知道为什么...... ?.

I see you are using the code from TwinHelix http://www.twinhelix.com/javascript/dragresize/ 我看到你正在使用TwinHelix的代码http://www.twinhelix.com/javascript/dragresize/

1) Please give credit to the author by always stating, clearly, where you got the code you are using. 1)请通过始终明确说明您获得所使用代码的位置来表彰作者。 You should have told us that you are using the package available from TwinHelix. 您应该告诉我们您正在使用TwinHelix提供的软件包。 Don't, deliberately or by omission, let people think that code you show is your own. 不要,故意或遗漏,让人们认为你展示的代码是你自己的代码。 If you got it somewhere, tell us. 如果你到了某个地方,请告诉我们。

2) I've used that code to explore some possibilities and I had no problem with it. 2)我已经使用该代码来探索一些可能性,我没有遇到任何问题。 I could resize divs to any size, including to smaller than the original size. 我可以将div调整为任意大小,包括小于原始大小。

I recommend you go to the TwinHelix site and get the current code and then start over and make sure you are using it correctly. 我建议你去TwinHelix网站获取当前代码,然后重新开始并确保你正确使用它。

You can set limits on how large or small a div can be made, limits on how much it can be moved up and down, etc. 您可以设置div的大小限制,限制可以上下移动的数量等。

Make sure you understand at least the basics of what it is doing and how to code the divs to do what you want done. 确保你至少了解它正在做什么的基础知识以及如何编写div来做你想做的事情。

Take TwinHelix's demo page, copy it to your system, with all the Javascript, etc. and get it to work on your system. 获取TwinHelix的演示页面,将其复制到您的系统,使用所有Javascript等,并使其在您的系统上运行。 Then make that page work the way you want - divisions sizes, content, etc. - and once you have divisions working the way you want, integrate the code into your own pages. 然后使该页面按照您想要的方式工作 - 分区大小,内容等 - 一旦您按照自己的方式工作,就可以将代码集成到您自己的页面中。 Never take code from somewhere and jump right in and put it in your pages. 切勿从某处获取代码并直接跳入并将其放入页面中。 Isolate things, make a separate test page with only the new thing, drag and resize in this example, and then integrate it into your pages. 隔离事物,在新的事物中创建一个单独的测试页面,在此示例中拖动并调整大小,然后将其集成到您的页面中。

Trying to change it to work the way you want at the same time you are integrating it into your pages will cause you more problems than you can handle. 尝试将其更改为按照您希望的方式工作,同时将其集成到您的页面中将导致您遇到的问题多于您可以处理的问题。

When doing any programming, break it down into easily handled chunks, get them working the way you want, and then put them together to make the whole (I've been at "this" for 39+ years and have just a bit of experience). 在进行任何编程时,将其分解为易于处理的块,让它们以您想要的方式工作,然后将它们组合在一起以构成整体(我已经在“此”工作了39年以上并且只有一点经验)。

Read the comments in the sample code and the other code and understand at least who to define the divisions. 阅读示例代码和其他代码中的注释,并至少了解定义分部的人员。

Unless you are sure of what you are doing, don't modify any of the code or CSS or you may introduce problems with no idea of where they came from. 除非你确定自己在做什么,否则不要修改任何代码或CSS,否则你可能会引入问题而不知道它们来自何处。

3) Except for a couple of issues, the TwinHelix code works well. 3)除了几个问题,TwinHelix代码运行良好。 My problem was in trying to stretch the capabilities and adding new ones. 我的问题是尝试扩展功能并添加新功能。 I got things working the way I want but there are a few quirks I simply don't like. 我按照我想要的方式工作,但有一些我根本不喜欢的怪癖。 So, I'm looking at some other methods of doing it. 所以,我正在研究其他一些方法。 I don't know when I'll have it done, but when I do I'll post a page on my web site at http://www.bobnovell.com --- be aware that there is not much there right now (as of December 28, 2012) but I have a lot to add when I have time. 我不知道什么时候能完成它,但是当我这样做时,我会在我的网站http://www.bobnovell.com上发布一个页面 - 请注意现在没有多少(截至2012年12月28日)但是,当我有时间时,我还有很多要补充的内容。

4) For a reasonably good drag function, take a look at http://tool-man.org/ToolManDHTML/ - the "Basic Dragable Layers" It does not have resizing but it works well. 4)对于一个相当好的拖动功能,看看http://tool-man.org/ToolManDHTML/ - “基本可拖动图层”它没有调整大小但它运作良好。 The only problem has to do with setting the zIndex. 唯一的问题与设置zIndex有关。 I've added code to handle mouseDowns to bring divisions 我添加了代码来处理mouseDowns带来的分歧

5) I would not expect anyone to look at all of the code - JavaScript, CSS, HTML - that you provided in this question and give you advice -- it is simply too large. 5)我不希望任何人看到你在这个问题中提供的所有代码 - JavaScript,CSS,HTML - 并给你建议 - 它太大了。

Also, go to the authors if you have questions. 另外,如果您有疑问,请联系作者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM