JAKARTAPROJECT
JAKARTA TIPJSP TIPJSP 질문&답변DATABASE TIPJAVASCRIPT TIPWEBHACKING TIP기타 TIP
자바스크립트 팁
자바스크립트 팁
자바스크립트 및 CSS 팁 게시판입니다
Javascript drag and drop
GoodBug http://www.jakartaproject.com
이미지 슬라이더 보기

Overview

Ever wanted to drag an element or image on your webpage? You can try using this very lightweight javascript drag handler. You can attach this drag handler to any relative or absolute positioned element.

First include the script at the top of your code (you can find the code in the source code section of this page). Do it in <head> tag. After loading the script you can write your code to attach drag handler.

You can attach this drag handler to any number of elements on page and you will be able to drag them all. The most importaint thing is that these elements must be posisioned relatively or absolutely.

 

Source code for webtoolkit.drag.js

 

/**
*
* Crossbrowser Drag Handler
* http://www.webtoolkit.info/
*
**/

var DragHandler = {


    // private property.
    _oElem : null,


    // public method. Attach drag handler to an element.
    attach : function(oElem) {
        oElem.onmousedown = DragHandler._dragBegin;

        // callbacks
        oElem.dragBegin = new Function();
        oElem.drag = new Function();
        oElem.dragEnd = new Function();

        return oElem;
    },


    // private method. Begin drag process.
    _dragBegin : function(e) {
        var oElem = DragHandler._oElem = this;

        if (isNaN(parseInt(oElem.style.left))) { oElem.style.left = '0px'; }
        if (isNaN(parseInt(oElem.style.top))) { oElem.style.top = '0px'; }

        var x = parseInt(oElem.style.left);
        var y = parseInt(oElem.style.top);

        e = e ? e : window.event;
        oElem.mouseX = e.clientX;
        oElem.mouseY = e.clientY;

        oElem.dragBegin(oElem, x, y);

        document.onmousemove = DragHandler._drag;
        document.onmouseup = DragHandler._dragEnd;
        return false;
    },


    // private method. Drag (move) element.
    _drag : function(e) {
        var oElem = DragHandler._oElem;

        var x = parseInt(oElem.style.left);
        var y = parseInt(oElem.style.top);

        e = e ? e : window.event;
        oElem.style.left = x + (e.clientX - oElem.mouseX) + 'px';
        oElem.style.top = y + (e.clientY - oElem.mouseY) + 'px';

        oElem.mouseX = e.clientX;
        oElem.mouseY = e.clientY;

        oElem.drag(oElem, x, y);

        return false;
    },


    // private method. Stop drag process.
    _dragEnd : function() {
        var oElem = DragHandler._oElem;

        var x = parseInt(oElem.style.left);
        var y = parseInt(oElem.style.top);

        oElem.dragEnd(oElem, x, y);

        document.onmousemove = null;
        document.onmouseup = null;
        DragHandler._oElem = null;
    }

}

 

 

2009-02-04 18:18:13
220.70.88.***

 

좋은생각 ^^

첨부파일 (총 1개)
  1. webtoolkit_drag.js 1.65 KB (171 다운로드)
0점 (0명)
덧글 1개 | 태그 1개 | 관련글보기
태그입력
쉽표(,)구분으로 한번에 여러 태그를 입력할수 있습니다
drag (1)
행복한광대
(0) (0)
감사합니다. ^^
118.218.148.*** 2009-02-04 18:18:13
이름 비밀번호
자바스크립트 팁
자바스크립트 및 CSS 팁 게시판입니다
! 번호 제목 글쓴이 일자 조회
220 img 이미지 부드럽게 줄이기 1 GoodBug 2009-12-18 8,159
219 rss xml 파싱하는 모듈 입니다. 1 고고싱 2009-11-27 7,610
218 트리 입니다 . 1 고고싱 2009-11-27 5,846
217 에러처리 1 kaiser 2009-05-11 6,060
216 웹페이지 복사 방지 해지 하는 법 kaiser 2009-05-03 11,645
215 javascript 한글 짜르기 1 1 kaiser 2008-08-14 7,953
214 rolling 롤링 스크립트 1 2 GoodBug 2008-07-11 10,103
213 alert 메시지를 print 처럼 사용하기 2 고고싱 2008-06-10 8,642
212 onload onload 펑션이 정의되어 있는 상황에서 onload에 추가하기 1 GoodBug 2008-04-11 8,417
211 javascript 자바스크립트 개발가이드 4 1 kaiser 2008-04-09 8,866
210 escape HTML escpae code 1 GoodBug 2008-04-02 6,471
209 optgroup SELECT 목록 그룹별로 묶기 1 1 GoodBug 2008-03-06 8,191
208 동적 테이블 테이블에 동적 TR 삽입 및 삭제 IE, FF 공용 1 GoodBug 2008-01-18 11,717
drag Javascript drag and drop 1 1 GoodBug 2008-01-16 8,136
206 sort Sortable HTML table 2 GoodBug 2008-01-16 7,375
205 scrollable table Scrollable HTML table 1 GoodBug 2008-01-16 7,057
204 CSS CSS centered layout 1 1 GoodBug 2008-01-16 6,656
203 utf8 Javascript UTF-8 1 GoodBug 2008-01-16 7,885
202 encode Javascript url decode, encode 2 GoodBug 2008-01-16 8,099
201 암호화 Javascript MD5 2 GoodBug 2008-01-15 7,335
copyright 2005-2018 by Unicorn