js中如何拖动DIV中的图片?

JavaScript022

js中如何拖动DIV中的图片?,第1张

实现思路:

①鼠标按下+鼠标移动 → 拖拽

②鼠标松开 → 无拖拽

③鼠标偏移 → 拖拽距离

用JavaScript事件方法表示就是:

① onmousedown + onmousemove → startDrag()

② onmouseup → stopDrag()

drag.js代码:

var params = {

left: 0,

top: 0,

currentX: 0,

currentY: 0,

flag: false

}

//获取相关CSS属性

var getCss = function(o,key){

return o.currentStyle? o.currentStyle[key] : document.defaultView.getComputedStyle(o,false)[key] 

}

//拖拽的实现

var startDrag = function(bar, target, callback){

if(getCss(target, "left") !== "auto"){

params.left = getCss(target, "left")

}

if(getCss(target, "top") !== "auto"){

params.top = getCss(target, "top")

}

//o是移动对象

bar.onmousedown = function(event){

params.flag = true

if(!event){

event = window.event

//防止IE文字选中

bar.onselectstart = function(){

return false

}  

}

var e = event

params.currentX = e.clientX

params.currentY = e.clientY

}

document.onmouseup = function(){

params.flag = false

if(getCss(target, "left") !== "auto"){

params.left = getCss(target, "left")

}

if(getCss(target, "top") !== "auto"){

params.top = getCss(target, "top")

}

}

document.onmousemove = function(event){

var e = event ? event: window.event

if(params.flag){

var nowX = e.clientX, nowY = e.clientY

var disX = nowX - params.currentX, disY = nowY - params.currentY

target.style.left = parseInt(params.left) + disX + "px"

target.style.top = parseInt(params.top) + disY + "px"

}

if (typeof callback == "function") {

callback(parseInt(params.left) + disX, parseInt(params.top) + disY)

}

}

}

HTML/CSS

<style type="text/css">

#box{position:absolute left:100px top:100px padding:5px background:#f0f3f9 font-size:12px -moz-box-shadow:2px 2px 4px #666666 -webkit-box-shadow:2px 2px 4px #666666}

#main{border:1px solid #a0b3d6 background:white}

#bar{line-height:24px background:#beceeb border-bottom:1px solid #a0b3d6 padding-left:5px cursor:move}

#content{width:420px height:250px padding:10px 5px}

</style>

<div id="box">

    <div id="main">

        <div id="bar">拖拽</div>

        <div id="content">

            内容……

        </div>

    </div>

</div>

JS部分

<script src="drag.js" type="text/javascript"></script>

<script type="text/javascript">

    var oBox = document.getElementById("box")

    var oBar = document.getElementById("bar")

    startDrag(oBar, oBox)

</script>

我运行了你的代码,其实是可以复制粘贴的,因为你设置的复制出的图像覆盖了原来的位置了,这个可以查看元素可以看得到的。你可以改变

if(Obj!=null)

{

Obj.style.left = event.x-Obj.l

Obj.style.top = event.y-Obj.t

}

改成

if(Obj!=null)

{

Obj.style.left = event.x-Obj.l+100+'px'

Obj.style.top = event.y-Obj.t+100+'px'

}

就可以看得出来了

见证奇迹的时刻到了:

<!DOCTYPE html>

<html>

<head>

    <meta charset="UTF-8">

    <style>

        * {

            margin: 0

            padding: 0

        }

        .box {

            position: absolute

            top: 100px

            left: 100px

        }

        /*四边*/

        .box .t,

        .box .b,

        .box .l,

        .box .r {

            position: absolute

            z-index: 1

            background:#666

        }

        .box .l,

        .box .r {

            width: 10px

            height: 100%

            cursor: col-resize

        }

        .box .t,

        .box .b {

            width: 100%

            height: 10px

            cursor: row-resize

        }

        .box .t {

            top: 0

        }

        .box .b {

            bottom: 0

        }

        .box .l {

            left: 0

        }

        .box .r {

            right: 0

        }

        /*四角*/

        .box .tl,

        .box .bl,

        .box .br,

        .box .tr {

            width: 20px

            height: 20px

            position: absolute

            background: #CCC

            z-index: 2

            cursor: nwse-resize

        }

        .box .tl,

        .box .bl {

            left: -5px

        }

        .box .tr,

        .box .br {

            right: -5px

        }

        .box .br,

        .box .bl {

            bottom: -5px

        }

        .box .tl,

        .box .tr {

            top: -5px

        }

        .box .tr,

        .box .bl {

            cursor: nesw-resize

        }

        /*图片*/

        img {

            width: 100%

            height: 100%

        }

    </style>

    <script>

        window.onload = function () {

            var oDiv = document.getElementsByTagName('div')[0]

            oDiv.style.width = '500px'

            var aSpan = oDiv.getElementsByTagName('span')

            for (var i = 0 i < aSpan.length i++) {

                dragFn(aSpan[i])

            }

            function dragFn(obj) {

                obj.onmousedown = function (ev) {

                    var oEv = ev || event

                    var oldWidth = oDiv.offsetWidth

                    var oldHeight = oDiv.offsetHeight

                    var oldX = oEv.clientX

                    var oldY = oEv.clientY

                    var oldLeft = oDiv.offsetLeft

                    var oldTop = oDiv.offsetTop

                    document.onmousemove = function (ev) {

                        var oEv = ev || event

                        if (obj.className == 'tl') {

                            oDiv.style.width = oldWidth - (oEv.clientX - oldX) + 'px'

                            oDiv.style.height=oldHeight-(oEv.clientY-oldY)+'px'

                            oDiv.style.left = oldLeft + (oEv.clientX - oldX) + 'px'

                            oDiv.style.top = oldTop + (oEv.clientY - oldY) + 'px'

                        }

                        else if (obj.className == 'bl') {

                            oDiv.style.width = oldWidth - (oEv.clientX - oldX) + 'px'

                            oDiv.style.height=oldHeight+(oEv.clientY-oldY)+'px'

                            oDiv.style.left = oldLeft + (oEv.clientX - oldX) + 'px'

                            oDiv.style.bottom = oldTop + (oEv.clientY + oldY) + 'px'

                        }

                        else if (obj.className == 'tr') {

                            oDiv.style.width = oldWidth + (oEv.clientX - oldX) + 'px'

                            oDiv.style.height = oldHeight - (oEv.clientY - oldY)+'px'

                            oDiv.style.right = oldLeft - (oEv.clientX - oldX) + 'px'

                            oDiv.style.top = oldTop + (oEv.clientY - oldY) + 'px'

                        }

                        else if (obj.className == 'br') {

                            oDiv.style.width = oldWidth + (oEv.clientX - oldX) + 'px'

                            oDiv.style.height = oldHeight + (oEv.clientY - oldY)+'px'

                            oDiv.style.right = oldLeft - (oEv.clientX - oldX) + 'px'

                            oDiv.style.bottom = oldTop + (oEv.clientY + oldY) + 'px'

                        }

                        else if (obj.className == 't') {

                            oDiv.style.height=oldHeight-(oEv.clientY-oldY)+'px'

                            oDiv.style.top = oldTop + (oEv.clientY - oldY) + 'px'

                        }

                        else if (obj.className == 'b') {

                            oDiv.style.height = oldHeight + (oEv.clientY - oldY)+'px'

                            oDiv.style.bottom = oldTop - (oEv.clientY + oldY) + 'px'

                        }

                        else if (obj.className == 'l') {

                            oDiv.style.height = oldHeight + 'px'

                            oDiv.style.width = oldWidth - (oEv.clientX - oldX) + 'px'

                            oDiv.style.left = oldLeft + (oEv.clientX - oldX) + 'px'

                        }

                        else if (obj.className == 'r') {

                            oDiv.style.height = oldHeight + 'px'

                            oDiv.style.width = oldWidth + (oEv.clientX - oldX) + 'px'

                            oDiv.style.right = oldLeft - (oEv.clientX - oldX) + 'px'

                        }

                    }

                    document.onmouseup = function () {

                        document.onmousemove = null

                    }

                    return false

                }

            }

        }

    </script>

</head>

<body>

<div class="box">

    <span class="r"></span>

    <span class="l"></span>

    <span class="t"></span>

    <span class="b"></span>

    <span class="br"></span>

    <span class="bl"></span>

    <span class="tr"></span>

    <span class="tl"></span>

    <img src="https://gss0.bdstatic.com/70cFsjip0QIZ8tyhnq/img/iknow/730-350-0.jpg">

</div>

</body>

</html>