js鼠标拖动div

JavaScript017

js鼠标拖动div,第1张

你的obj.style.left是获取不到的因为该div没有设置style属性所以只要将样式改为行内就行了

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <title>文哥讨厌IE</title>

</head>

<body>

    <div id="over" onmousedown="down(this,event)" style="width:100pxheight:30pxbackground:redposition:absoluteleft:20pxtop:500px" onmousemove="move(this,event)" onmouseup="seup(this,event)">

        

    </div>

<script type="text/javascript">

    var posX,posY

    var downX,downY

    var mark=false

    function down(obj,event)

    {

        obj.style.cursor="move"

        posX=obj.style.left

        posY=obj.style.top

        downX=event.clientX

        downY=event.clientY

        mark=true

        ///alert(posX)

        ///alert(posY)

    }

    function move(obj,event)

    {

        var moveX=event.clientX

        var moveY=event.clientY

        if (mark) {

            obj.style.left=parseInt(moveX) + parseInt(posX) - parseInt(downX) + "px"

            obj.style.top=parseInt(moveY) + parseInt(posY) - parseInt(downY)+ "px"

        }

    }

    function seup(obj,event)

    {

        if (mark) {

            var moveX=event.clientX

            var moveY=event.clientY

            obj.style.left=parseInt(moveX) + parseInt(posX) - parseInt(downX)+ "px"

            obj.style.top=parseInt(moveY) + parseInt(posY) - parseInt(downY)+ "px"

            downX=moveX

            downY=moveY

        }

        obj.style.cursor="default"

            mark = false 

    }

</script>

</body>

</html>

<html>

<head>

<title>jquery或JS拖动DIV左右移动</title>

<script src="jquery-1.7.1.min.js"></script>

<style type="text/css">

    body {background-color: #fff }

    .win {position: absolute top: 0px left: 0pxwidth: 300pxheight: 222px}

    .title {height: 20pxwidth: 300px  position: absolutebackground-color: #0094ff float: inherit top: 0px left: 0px }

    .winCon { height: 200pxwidth: 298px position: absolute border: solid 

              border-width: 1px border-color: #0094ff border-top: none  float: inherit left: 0px top: 20px }

</style>

</head>

<body onload="showDiv(this,'test1')">

</body>

<script type="text/javascript">

    function showDiv(element, str) {

        $(document.body).append("<div class='win' id='win" + str + "'><div class='title' id='" + str + "'></div><div class='winCon'> 清新自在</div></div>")

        $("#" + str).mousedown(function (event) {

            var offset = $(this).offset()

            _x = event.clientX - offset.left

            _y = event.clientY + 20 - offset.top

            $("#win" + str).css({ "top": offset.top - 20 + "px" })

            $("#win" + str).mousemove(function (event) {

                _xx = event.clientX - _x

                _yy = event.clientY - _y

                this.style.left = _xx + "px"

                this.style.top = _yy + "px"

                this.style.zIndex = "100"

                return false

            })

            return false

        })

        $("#win" + str).mouseup(function () {

            $(this).unbind("mousemove")

            $(this).css({ "z-index": "-1" })

            return false

        })

    }

</script>

</html>