日期:2014-05-16  浏览次数:20336 次

js实现 鼠标拖动图片
<html>
<head>
<title>拖动演示</title>
</head>
<style type="text/css">
    #plane1 {position:absolute; left:10; top:20;  z-index:5}
    #plane2 {position:absolute; left:10; top:20; width:800; height:600; z-index:999; background:url(mask1.png) no-repeat;}
</style>
<SCRIPT LANGUAGE="JavaScript">

var isNav, isIE
if (parseInt(navigator.appVersion) >= 4) {
    if (navigator.appName == "Netscape") {
        isNav = true
    } else {
        isIE = true
    }
}


function setZIndex(obj, zOrder) {
    obj.zIndex = zOrder
}


function shiftTo(obj, x, y) {
    if (isNav) {
        obj.moveTo(x,y)
    } else {
        obj.pixelLeft = x
        obj.pixelTop = y
    }
}
// ***End API Functions***

// Global holds reference to selected element
var selectedObj
// Globals hold location of click relative to element
var offsetX, offsetY

// Find out which element has been clicked on


function setSelectedElem(evt) {
    if (isNav) {
        // declare local var for use in upcoming loop
        var testObj
        // make copies of event coords for use in upcoming loop
        var clickX = evt.pageX
        var clickY = evt.pageY
        // loop through all layers (starting with frontmost layer)
        // to find if the event coordinates are in the layer
        for (var i = document.layers.length - 1; i >= 0; i--) {
            testObj = document.layers[i]
            if ((clickX > testObj.left) &&
                (clickX < testObj.left + testObj.clip.width) &&
                (clickY > testObj.top) &&
                (clickY < testObj.top + testObj.clip.height)) {
                    // if so, then set the global to the layer, bring it
                    // forward, and get outa here
                    selectedObj = testObj
                    setZIndex(selectedObj, 100)
                    return
            }
        }
    } else {
        // use IE event model to get the targeted element