2012-07-02 6 views
3

HTML 페이지에서 DIV 요소의 크기를 조정하고 끌고 싶습니다. 내가 사용한 코드는 다음과 같습니다. 자바 스크립트 :DIV 요소를 끌고 크기를 조정하는 방법

<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 요소 크기를 줄일 수 없다, 나는 왜 .. ..?

답변

8

나는 당신이 사용하는 코드를 어디서 얻었 항상 명확하게 진술하여 작성자에게 신용을 제공하십시오)

1 TwinHelix http://www.twinhelix.com/javascript/dragresize/의 코드를 사용하십시오. TwinHelix에서 제공하는 패키지를 사용하고 있다고 말했어야합니다. 고의적으로 또는 누락으로 사람들이 자신이 제시 한 코드를 사용한다고 생각하게하지 마십시오. 어딘가에 있다면 알려주세요.

2) 몇 가지 가능성을 탐색하기 위해이 코드를 사용했지만 아무런 문제가 없었습니다. divs의 크기를 원래 크기보다 작은 크기로 조정할 수있었습니다.

나는 TwinHelix 사이트로 가서 현재 코드를 얻은 다음 다시 시작하고 올바르게 사용하는지 확인하는 것이 좋습니다.

당신은 적어도이 무엇을하고 있는지의 기본을 이해하고 있는지 확인 등

사업부가 할 수있는 방법을 규모에 제한이 아래로 이동 할 수 있습니다 얼마나 많은 제한을 설정할 수 있습니다

div를 작성하여 원하는 작업을 수행하는 방법에 대해 설명합니다.

트위터 헬리콥터의 데모 페이지를 가져와 시스템에 복사하고 모든 자바 스크립트 등을 사용하여 시스템에서 작동하도록하십시오. 그런 다음 해당 페이지가 원하는대로 작동하도록합니다 (부서의 크기, 내용 등). 그리고 원하는 방식으로 부서를 운영하고 나면 코드를 자신의 페이지에 통합하십시오. 어딘가에서 코드를 가져 와서 바로 들어가서 페이지에 넣으십시오. 사물을 격리하고, 새로운 것만으로 별도의 테스트 페이지를 만들고,이 예제에서 끌어서 크기를 조정 한 다음 페이지에 통합하십시오.

동시에 원하는 방식으로 변경하려고하면 페이지에 통합하는 것과 동시에 처리 할 수없는 것보다 많은 문제가 발생할 수 있습니다.

프로그래밍을 할 때는 쉽게 처리 할 수있는 덩어리로 나누고, 원하는 방식으로 작업하게 한 다음, 전체를 만들기 위해 함께 놓으십시오 (나는 "이"를 39 년 이상 보냈으며 방금 약간의 경험).

예제 코드와 다른 코드의 주석을 읽고 적어도 누가 부서를 정의하는지 이해하십시오.

당신이하고있는 일을 확신하지 못하는 한, 코드 나 CSS를 수정하지 마십시오. 어디서 왔는지 모를 때 문제가 생길 수 있습니다.

3) 몇 가지 문제점을 제외하고는 TwinHelix 코드가 잘 작동합니다. 내 문제는 기능을 늘리고 새로운 기능을 추가하려고 시도하는 데있었습니다.내가 원하는 방식으로 작동하는 것들을 가지고 있지만 단순히 좋아하지 않는 몇 가지 단점이 있습니다. 그래서, 나는 다른 방법을 찾고 있습니다. 언제 내가 할 수 있을지 모르겠지만, 내가 할 때 http://www.bobnovell.com에 내 웹 사이트에 페이지를 게시 할 것입니다 --- 거기에별로 많지 않다는 것을 알고 있어야합니다 (2012 년 12 월 28 일 현재) 하지만 시간이있을 때 추가해야 할 것이 많습니다.

4) 적당히 좋은 드래그 기능을 보려면 http://tool-man.org/ToolManDHTML/ - "기본 드래그 가능 레이어"를보십시오. 크기 조정 기능은 없지만 잘 작동합니다. 유일한 문제는 zIndex를 설정하는 것과 관련이 있습니다. 나는 mouseDowns를 처리하여 divisions을 처리하는 코드를 추가했습니다.

5)이 질문에서 제공하고 조언을 제공하는 모든 코드 (JavaScript, CSS, HTML)를 아무도 보지 않을 것으로 예상됩니다. 단순히 너무 큽니다.

또한 질문이 있으시면 작성자에게 문의하십시오.

관련 문제