메뉴 건너뛰기

프로그램언어

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>

  1. while, for, foreach 속도 비교

    Date2021.03.26 Views623
    Read More
  2. utf-8 문자열을 주어진 바이트로 자르기

    Date2019.04.29 Views1356
    Read More
  3. TIME_TO_SEC 시간 포맷

    Date2019.01.16 Views1381
    Read More
  4. TIFF, GIF 여러장 변환

    Date2021.03.26 Views323
    Read More
  5. text파일에 한줄씩 내용추가하기

    Date2017.03.06 Views17537
    Read More
  6. Text를 GD 이미지로 뿌리기

    Date2014.02.27 Views29813
    Read More
  7. substr(), mb_substr(), iconv_substr()

    Date2021.03.26 Views564
    Read More
  8. stripslashes — 따옴표 처리한 문자열을 풉니다

    Date2016.12.23 Views20486
    Read More
  9. stripcslashes — addcslashes()로 인용한 문자열을 되돌림

    Date2016.12.23 Views20442
    Read More
  10. RSSReader Class 제작 및 Reader 만들기

    Date2016.08.22 Views21042
    Read More
  11. RSS json_decone 사용방법

    Date2019.01.16 Views1430
    Read More
  12. Record Drag/Drop Position

    Date2014.02.27 Views29201
    Read More
  13. quotemeta 모든 메타 문자앞에 역슬래쉬를 붙인 문자열을 반환

    Date2016.12.23 Views20461
    Read More
  14. printf() sprintf()

    Date2021.03.26 Views272
    Read More
  15. preg_match (정규표현식 매치를 수행합니다)

    Date2016.12.23 Views20843
    Read More
  16. Predefined Variables (미리 정의된 변수들)

    Date2021.03.26 Views274
    Read More
  17. POST값 통째로 인코딩하기

    Date2015.04.06 Views21175
    Read More
  18. POST, GET으로 배열값 받기(직렬화)

    Date2017.03.06 Views23286
    Read More
  19. php한글체크를 위한 정규표현식

    Date2014.04.12 Views22458
    Read More
  20. PHP폼 사용시 폼 양식에서 값이 사라질때

    Date2019.01.08 Views1316
    Read More
Board Pagination Prev 1 ... 4 5 6 7 8 9 10 11 12 13 ... 17 Next
/ 17

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

© k2s0o1d4e0s2i1g5n. All Rights Reserved