[스크립트 이벤트] div 영역 마우스로 움직이기 :: 깨알지식 , 공부공부
/* div 움직이기 */
var img_L = 0;
var img_T = 0;
var targetObj;
function getLeft(o){
return parseInt(o.style.left.replace('px', ''));
}
function getTop(o){
return parseInt(o.style.top.replace('px', ''));
}
//움직이기
function moveDrag(e){
var e_obj = window.event? window.event : e;
var dmvx = parseInt(e_obj.clientX + img_L);
var dmvy = parseInt(e_obj.clientY + img_T);
targetObj.style.left = dmvx +"px";
targetObj.style.top = dmvy +"px";
return false;
}
//드래그 시작
function startDrag(e, obj) {
targetObj = obj;
var e_obj = window.event? window.event : e;
img_L = getLeft(obj) - e_obj.clientX;
img_T = getTop(obj) - e_obj.clientY;
document.onmousemove = moveDrag;
document.onmouseup = stopDrag;
if(e_obj.preventDefault)e_obj.preventDefault();
}
//드래그 멈추기
function stopDrag(){
document.onmousemove = null;
document.onmouseup = null;
}
그대로 사용해주면 된다.
*참고 : http://horangi.tistory.com/249
'JavaScript' 카테고리의 다른 글
[스크립트 이벤트] Data 검색 할 때, 바로바로 출력하기 :: 깨알지식 , 공부공부 (0) | 2017.01.02 |
---|---|
[스크립트 이벤트] 체크박스 선택하기 :: 깨알지식 , 공부공부 (0) | 2016.12.30 |
[스크립트 이벤트] 이미지 업로드 할 때, 미리보기 :: 깨알지식 , 공부공부 (0) | 2016.12.28 |
[스크립트 이벤트] 마우스 오버 시, 서브메뉴얼 표시하기. :: 깨알지식 , 공부공부 (0) | 2016.12.14 |