简体   繁体   中英

javascript (jQuery) not working in IE6

I have developed a dragable div with an image inside using jquery. The script is working perfectly in Firefox, chrome but not it IE6. could you please help me to fix this issue

check the web page here : my web page

Thank you very much for your consideration.

IE uses clientX and clientY instead of pageX and pageY. Some people fix this by doing the following:

//if IE, then:
if (e.srcElement) {
    e.pageX = oEvent.clientX + document.body.scrollLeft;
    e.pageY = oEvent.clientY + document.body.scrollTop;
}

//rest of event handler goes here

I would probably not write the code myself. jQuery UI provides a $(...).draggable() method that should work, and is cross-browser tested. You can even custom build a jQuery UI download that will only include the components you want.

http://jqueryui.com/demos/draggable/

http://jqueryui.com/download

Unless you expect a lot of your visitors to use it - just drop IE6 support. Keeping sites IE6 compatible either increases code redundancy or degrades quality.

Since you're already using jQuery, why not use jQuery UI's draggable component? This way, you don't have to deal with all the mouse down calculations. I switched your site's code to use jQuery UI's draggable functionality and it was pretty quick and required a lot less code.

Here's the code I used:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jQuery Drag and drop</title>
<style type="text/css"> 
#dv {
             position: absolute;
             cursor: move;
           }  
</style>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.2/jquery-ui.min.js"></script>
<script language="javascript"> 
    $(document).ready(function() {
        $("#dv").draggable({
            cursor: 'crosshair'
        });
    });
</script>
</head>
<body>

<div id="dv" style="position:absolute;left:300px;top:200px;">
<img src="http://www.mejoyal.com/jquery/drupal.png" />
</div>


</div>
</body>

</html>

Hope this helps!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM