메뉴 건너뛰기

프로그램언어

2014.02.27 10:32

Record Drag/Drop Position

조회 수 29201 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
<head>
<title>Record Drag/Drop Position</title>

<script type="text/javascript">
// Write session or persistent cookies
function setCookie(cookieName,cookieValue,cookieExpiration) {
    
if (cookieExpiration!=null) {
        // Cookie has a duration / date object is expected
document.cookie=cookieName + "=" + escape(cookieValue) + ";path=/;expires=" + cookieExpiration.toGMTString()
    } else {
        // Cookie has no duration / it will remain only for the browser session
document.cookie=cookieName + "=" + escape(cookieValue) + ";path=/"
    }
}

// Get cookie value
function getCookie(cookieName) { 

cookieString=document.cookie
    cookieName+="="
    
// If at least one cookie exists...
    if (cookieString.length>0) {
        
// Search for the cookie name
i=cookieString.indexOf(cookieName)
        
// If the cookie name exists, return its value
        if (i!=-1) {
            i += cookieName.length
            j = cookieString.indexOf(";",i)
            
            if (j==-1) {j = cookieString.length}
            
            return unescape(cookieString.substring(i,j))
        }
    }

    // Return a null value if the cookie doesn't exist
return null
}

// Delete a cookie
function deleteCookie(cookieName) {

// Get a date object for the 1st january 1970
cookieExpiration = new Date(1970,0,1)
// Set cookie value to an empty string and its expiration date to the 1st january 1970
document.cookie=cookieName + "=;path=/;expires=" + cookieExpiration.toGMTString()
}

// Get object left position, even if nested
function getAbsLeft(o) {
oLeft = o.offsetLeft
while(o.offsetParent!=null) {
oParent = o.offsetParent
oLeft += oParent.offsetLeft
o = oParent
}
return oLeft
}

// Get object top position, even if nested
function getAbsTop(o) {
oTop = o.offsetTop
while(o.offsetParent!=null) {
oParent = o.offsetParent
oTop += oParent.offsetTop
o = oParent
}
return oTop
}

// Set object left position
function setLeft(o,oLeft) {
o.style.left = oLeft + "px"
}

// Set object top position
function setTop(o,oTop) {
o.style.top = oTop + "px"
}

// Set object top and left positions
function setPosition(o,oLeft,oTop) {
setLeft(o,oLeft)
setTop(o,oTop)
}

// Mouse button down handler
function dragMouseDown(e)
{
// Get the event object for IE
if (!e) {e = window.event}

// Dragging begins...
doDrag=true

// Get a reference to the dragged object
o=document.getElementById("draggedObject")

// Get original top and left positions of the dragged object
oLeft=getAbsLeft(o)
oTop=getAbsTop(o)

// Get the mouse cursor position into the dragged object surface
mouseLeft=e.clientX-oLeft
mouseTop=e.clientY-oTop

}

// Mouse button up handler
function dragMouseUp(e)
{
// Dragging stops
doDrag=false

// Get the event object for IE
if (!e) {e = window.event}

// Store the position of the dragged object
// as a cookie / the cookie value is a snippet of JavaScript
oLeft = e.clientX-mouseLeft
oTop = e.clientY-mouseTop
cookieValue = "var oLeft=" + oLeft + ";" + "var oTop=" + oTop
setCookie("recPos",cookieValue,expirationDate)

}

// Mouse move handler
function dragMouseMove(e)
{
// Get the event object for IE
if (!e) {e = window.event}

// If dragging is on going...
if (doDrag)
{
// Get a reference to the dragged object
o=document.getElementById("draggedObject")

// Set the top and left positions of the dragged object relatively to the mouse cursor
oLeft = e.clientX-mouseLeft
oTop = e.clientY-mouseTop
setPosition(o,oLeft,oTop)

// Stop event propagation
return false
} 
}

// Show last recorded position
function getRecPos()
{
alert(getCookie("recPos"))
}

// At page load, look for a recorded position
// If so, move the dragged object to the last recorded position
function setRecPos()
{
cookieValue = getCookie("recPos")
if (cookieValue!=null)
{
// Interpret the snippet of JavaScript stored in the cookie
eval(cookieValue)

// Move the dragged object to the last recorded position
o=document.getElementById("draggedObject")
setPosition(o,oLeft,oTop)
}
}

// Set the expiration date 5 days ahead in the time
expirationDate = new Date()
expirationDate.setDate(expirationDate.getDate() + 5)

doDrag=false
mouseLeft=0
mouseTop=0

document.onmousemove = dragMouseMove
</script>

</head>

<body onload="setRecPos()">

<div id="draggedObject" style="position:absolute;top:100px;left:100px;width:80px;height:80px;cursor:pointer;cursor:hand;background-color:green" onmousedown="dragMouseDown(event)" onmouseup="dragMouseUp(event)"></div>

<button onclick="getRecPos()">Get recorded position</button></br>
Drag object, close the demo page and re-open it to see the end result.

</body>

</html>

List of Articles
번호 제목 날짜 조회 수
180 엑셀(*.xls) 화일을 PHP에서 읽기 2017.03.06 17471
179 시간관련함수 2016.12.23 17328
178 게시판 페이징 기법과 개념 file 2017.03.06 17323
177 PHP에서 Excel 파일을 만들 수 있는 PHPExcel file 2017.03.06 17112
176 지엠 웹에디터 v1.1 (저작권표시없음)| file 2017.03.06 17109
175 www가 붙은 도메인과 안붙은 같은 도메인, 로그인 세션 유지 2017.03.07 17080
174 PHP 만년달력 소스 2017.03.06 17063
173 php 내장함수 2017.03.07 17001
172 PHP 날짜, 시간 관련 함수. date(), mktime() 2017.03.06 16707
171 PHP의 유동변수!? - $a1 ~ $a2 같은 형식의 변수를 반복문 돌릴때... 2017.03.06 16610
170 php로 db 컨트롤 1 2017.03.06 15769
169 php 문자열관련 함수 2017.03.06 15580
168 메일주소의 골뱅이를 그림처리하기 2017.03.06 15508
167 base64 인코딩/디코딩 함수의 특징 file 2018.02.09 13078
166 PHP 네이버블로그 원격 글쓰기 API 소스 file 2018.02.09 12465
165 PHP에서 암호화 encrypt 복호화 decrypt 해서 값을 넘기기 2018.02.09 10626
164 AJAX를 활용하여 JSON 댓글 처리하기 (PHP) 2018.07.04 8454
163 MySQL(MariaDB) 테이블 만들기 2018.03.28 8152
162 [이클립스]PHP 개발환경 만들기 file 2018.07.04 7939
161 헤더이용 다운로드 받을시 바로열기부분 소스 2018.07.24 7320
Board Pagination Prev 1 ... 4 5 6 7 8 9 10 11 12 13 ... 17 Next
/ 17

하단 정보를 입력할 수 있습니다

© k2s0o1d4e0s2i1g5n. All Rights Reserved