Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 441343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T20:57:47+00:00 2026-05-12T20:57:47+00:00

I have a little tool on a web page that can be clicked and

  • 0

I have a little tool on a web page that can be clicked and dragged along a horizontal line. First I had the drag handle as an img. I had some problems with layout and realised later that it would probably be better to use a div with an img background.

So I changed the drag handle to use a div, and I discovered a pretty obvious error in my javascript code. I had the onMove and onUp events attached to the handle itself. So for example, if I clicked on the drag handle div and then moved my mouse upwards out of the div (as the div only moves left and right), it then doesn’t catch the onUp or onMove events.

But the thing I don’t understand, is why it worked perfectly when I was using an img tag. Is this a bug or is there something funny about images that makes them behave this way? Is it future-safe to leave use an img and leave the code as is?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T20:57:48+00:00Added an answer on May 12, 2026 at 8:57 pm

    When drag starts (onDown or
    onClick), you should actually
    dynamically attach onUp and onMove
    events to document instead of
    handle. Just don’t forget to clear
    them onUp.

    However, there should be no difference
    between <img> and <div> in this
    case.

    — EDIT —

    I stand corrected. Seems, there is slight difference in img and div handling in FF since FF3. It enables you to drag images to your computer, but we can get around it :).

    Anyway, I have made some slight modifications to your script, cleaned it up a bit, and it should work now in all browsers. I have tested it on my computer with Firefox 3.5.5, Safari 4.0.3, Google Chrome, and Opera 10.1.

    However, I don’t have a way to test it on IE, so please report results.

    <!DOCTYPE html>
    <html><head><meta charset="utf-8"><title>Page Title</title>
    <script>
    window.onload = function() { //After page is loaded this script will start automaticly
        var divEl = document.getElementById("myDiv"); // Locate element with id "myDiv"
        var imgEl = document.getElementById("myImg"); // Locate element with id "myImg"
        divEl.onmousedown = onDown; // Assign funnction to event mousedown on "myDiv"
        imgEl.onmousedown = onDown; // Assign funnction to event mousedown on "myImg"
    
        function onDown(e) { // Mouse is down, long live the mouse :)
            $el = this;
            if (e.preventDefault) { e.preventDefault(e); } // This prevents default "drag image" behaivour
            window.onmousemove = function(e) { // Assign function onmousemove to window
                this.onmouseup = function(e) { // When we do a mouseup anywhere on window
                    window.onmousemove = undefined; // We clear onmouse from window
                    window.onmouseup = undefined; // We clear onmouseup from window
                }
                /* Part borowed from http://www.quirksmode.org/js/events_properties.html */
                if (e.pageX) { posx = e.pageX; } // In case it is a normal browser
                else if (e.clientX) { // In case it is IE
                    posx = e.clientX + document.body.scrollLeft+document.documentElement.scrollLeft;
                    }
                /* End of borowed part */   
                $el.style.left = posx-parseInt($el.offsetWidth)/2+"px"; // Move this element to where mouse is
            }
        }
    }   
    </script>
    <style>
        #myImg, #myDiv { display: block; position: relative; left: 0; width: 100px; height: 50px; }
        #myImg { border: 1px solid red; }
        #myDiv { border: 1px solid blue; background-color: lime;}
    </style>
    </head>
    <body>
        <img src="some_image.png" alt="my img" id="myImg">
        <div id="myDiv">My div</div>
    </body>
    </html>
    

    Ah, yes. CSS & JS in this example are in HTML only for testing purposes. In real code it is suggested you separate them to their own files.

    Good luck 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a little command line tool that reads from stdin. On the command
I have a page in my ASP.NET web application that is essentially a tool
I have a little web admin tool I'm working on that allows a user
I have a little foundation tool test (Objective-C) that I am playing with and
brief history. I have a little reporting tool that I am creating that uses
In my company we have a web-based tool that you enter an account, press
I have been thinking of maybe writing a little tool that logs into SO
I have a little tool written in JavaScript and PHP that takes a list
I have my little designer tool (my program). On the left side I have
I have little question about how web browser retrieve webpage? I know this User

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.