2016-12-05 4 views
1

위쪽, 왼쪽, 오른쪽 및 아래를 터치하기 전에 드래그 가능한 텍스트의 유효성을 검사해야합니다. 전에 박스 안에 1px 여백 경계를 알려야합니다. 1px 여백 다음에 움직여서는 안됩니다. 어떻게 자바 스크립트 또는 jquery를 사용하여 그것을 달성하기 위해? 내가 오해하지 않은 경우상자 주위를 움직이는 드래그 가능한 텍스트의 유효성을 검사하는 방법은 무엇입니까?

https://jsfiddle.net/uf44m36j/1/

$('.dragme').draggable({ containment: ".drag-parent" }); 
+0

당신이 요소에의 오프셋 (offset) 발견'.drag' 이벤트와 함수를 작성해야 그것의 부모. – ntgCleaner

+0

'stop' 이벤트가 발생하면 부모와 교차하는지 확인해야합니다. 'position' 속성이 도움이 될 수 있습니다. https://jsfiddle.net/d345k0/phfyqp4j/ – d345k0

+0

샘플을 보내 주시겠습니까 – Parker

답변

0

, 당신은 사각형 테두리를 명중하기 전에 사용자에게 통지하지 않습니다 싶어? 이 사례에 드래그 이벤트를 사용하고이 이벤트의 위치를 ​​식별 할 수 있습니다.

문서 : http://api.jqueryui.com/draggable/#event-drag

여기

인 JsFiddle : https://jsfiddle.net/yusrilmaulidanraji/uf44m36j/2/

$('.dragme').draggable({ 
 
containment: ".drag-parent", 
 
drag: function(event, ui) { 
 
    \t if (ui.position.top == 1) { 
 
    //tell user the box almost hits the top 
 
    } 
 
    if (ui.position.left == 1) { 
 
    //tell user the box almost hits the left 
 
    } 
 
} 
 
});
.dragme { 
 
     border-radius: 0.5em; 
 
     width: 12em; 
 
     height: 8em; 
 
     padding: 1em; 
 
     font-family: "Montserrat", "Arial"; 
 
     background-color: #00C5CD; 
 
     color: white; 
 
    } 
 
    
 
    
 
    .drag-parent { 
 
    border: 0.1em solid #BA064E; 
 
    width: 45em; 
 
    height: 20em; 
 
}
<script src="//code.jquery.com/jquery-1.9.1.js"></script> 
 
<script src="//code.jquery.com/ui/1.9.2/jquery-ui.js"></script> 
 
<link href="//code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css" rel="stylesheet"/> 
 
<div class="drag-parent"> 
 
    <div class="dragme">Drag me!</div> 
 
</div>

+0

maulidan 누르기 전에 사용자에게 경계 여백 1px를 알려야합니다. – Parker

+0

@Parker 나는 당신이 무슨 뜻인지 이해하지 못한다고 생각합니다. 테두리를'drag-parent' 또는'dragme'에 추가하는 것을 의미합니까? –

+0

@maulidan 텍스트를 옮기는 동안 박스 테두리를 만져서는 안되며, 상자 주위에 1px 테두리의 단색 여백을 표시하고 거기에서 멈춰야합니다. – Parker