I want two separate objects on a page to be moveable by the user – mouse click and drag. I’m having all sorts of problems achieving this. the following test code baffles me. Both objects get moved and I cannot work out why.
Run code snippet
I don’t know if that’s causing your problem, but you have two opening
<div>s instead of closing
You might also be better off using HTML5 rather than HTML4.01.
Your test code appears to be very dated.
I’m just having a look through the following tutorial, it’s nicely explained and I think it maybe of use to you.
I think I love you! That’s it. Because of the missing there was only one object. Now only one moves. Now to make the second draggable too!
As @rpg_digital said above your code is very old and unlikely to work cross browser as you have a very old doctype and are utilising quirks mode. That means that certain things will only work if the browser steps in and helps you along with them.
The percentage height on your divs won’t work in standards mode in the way that you have defined them and the js won’t update because you have not used units on your measurements (px).
You don’t really want to leave anything to chance so choose a current doctype (html5) and make sure you use units in your dimensions for css measurements.
i.e. turn the number into a string with units by appending + “px”.
document.getElementById("container").style.top = nowY+event.clientY-offsety + "px"
For percentage height to work in standards mode you need an unbroken chain of heights all the way back to root so in your code you would need this:
You also have typos in your css for the left:15% and left:75% (although they don’t make much sense as the 75% will move most of that element off screen to the right).
Even though you may be just learning there is no reason to learn the wrong way to do something as that just makes it harder to unlearn Try following the link that @rpg_digital pointed to
Works fine now Gandalf spotted the spelling mistakes so I didn’t close my divs
px is really not required – never has been and I’ve been writing html since before it was invented! Yes, it’s in fact IBM-s dsoss we used to use for documentation – html has one extra tag
Thanks but what you wrote is furthermore irrelevant to the problem!
No typos by the way – they may not make sense to you but they do to me – and not part of the problem – what are you a nit-picker?
as I’ve probably been doing this since before you were born I doubt I need your tutorial. And you didn’t address the problem – Gandalf spotted the error – you didn’t – too high on your horse perhaps?
OK, let’s not get personal here.
Members are giving their up their free time to help, and are offering that help based on their understanding of the issue. If they don’t have the full picture, that is not their fault, nor is it a reason to respond with insults.
By all means disagree, but please do it courteously.
Then I must have imagined this.
Sorry if my comments upset you but just trying to point you in the correct way of doing things these days. You may indeed have a unique set up that allows you to use old fashioned code but of course we were not aware of that situation.
It would indeed be remiss of us not to mention code that is broken or malformed as the Internet is forever and anyone else looking at this thread may copy your code and try to use it and fail miserably as it certainly will not work in a standards doctype. Now that you have explained you have a special situation and are able to use old fashioned code then visitors here can be aware that the code is a special use case only.