473,408 Members | 2,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Drag & Drop with pictures

Hi,

I build my first Drag & Drop with JavaScript and I would like to drag
the pictures when I click on it.

When I create a normal div tag with some text the script works, but
when I put a image in the div tag it works not correctly.

Here is my Code... I integrated three pictures and one text, then you
can see the differences.

a part of my html site:
-----------------------------------
<body onload="autostart()">
<div id="part1">Hier eine Auswahl von Pics:</div>

<div id="bild">

<div onmousedown="bewege_bild(this)"
style="position:absolute;top:200px;left:0px;cursor :move;">
<img src="images/elefant.jpg">
</div>

<div onmousedown="bewege_bild(this)"
style="position:absolute;top:200px;left:150px;curs or:move;">
<img src="images/kaffee.jpg">
</div>

<div onmousedown="bewege_bild(this)"
style="position:absolute;top:200px;left:300px;curs or:move;">
<img src="images/ajax.jpg">
</div>

<div onmousedown="bewege_bild(this)"
style="position:absolute;top:200px;left:450px;curs or:move;">
HELLO
</div>

</div>
-----------------------------------

my JS Script:
-----------------------------------
// JavaScript Document

var posx;
var posy;

var dragx;
var dragy;

var dragobjekt = null;

function autostart() {

var bildDiv = document.getElementById("bild");
var bilder = bildDiv.getElementsByTagName("img");

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

bilder[i].onmousedown = bewege_bild;

}

document.onmouseup = dragStop;
document.onmousemove = drag;
}
function bewege_bild(to) {

dragobjekt = to;
dragx = posx - dragobjekt.offsetLeft;
dragy = posy - dragobjekt.offsetTop;
}

function drag(Ereignis) {

posx = document.all ? window.event.clientX : Ereignis.pageX;
posy = document.all ? window.event.clientY : Ereignis.pageY;
if(dragobjekt != null) {
dragobjekt.style.left = (posx - dragx) + "px";
dragobjekt.style.top = (posy - dragy) + "px";
}
}
function dragStop() {

dragobjekt=null;

}
-----------------------------------

Dec 23 '06 #1
2 2928
The script works correctly in IE... but the problems come with the
Firefox (Mozilla)

On 23 Dez., 21:19, "sebastian.janosc...@googlemail.com"
<sebastian.janosc...@googlemail.comwrote:
Hi,

I build my first Drag & Drop with JavaScript and I would like to drag
the pictures when I click on it.

When I create a normal div tag with some text the script works, but
when I put a image in the div tag it works not correctly.

Here is my Code... I integrated three pictures and one text, then you
can see the differences.

a part of my html site:
-----------------------------------
<body onload="autostart()">
<div id="part1">Hier eine Auswahl von Pics:</div>

<div id="bild">

<div onmousedown="bewege_bild(this)"
style="position:absolute;top:200px;left:0px;cursor :move;">
<img src="images/elefant.jpg">
</div>

<div onmousedown="bewege_bild(this)"
style="position:absolute;top:200px;left:150px;curs or:move;">
<img src="images/kaffee.jpg">
</div>

<div onmousedown="bewege_bild(this)"
style="position:absolute;top:200px;left:300px;curs or:move;">
<img src="images/ajax.jpg">
</div>

<div onmousedown="bewege_bild(this)"
style="position:absolute;top:200px;left:450px;curs or:move;">
HELLO
</div>

</div>
-----------------------------------

my JS Script:
-----------------------------------
// JavaScript Document

var posx;
var posy;

var dragx;
var dragy;

var dragobjekt = null;

function autostart() {

var bildDiv = document.getElementById("bild");
var bilder = bildDiv.getElementsByTagName("img");

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

bilder[i].onmousedown = bewege_bild;

}

document.onmouseup = dragStop;
document.onmousemove = drag;

}function bewege_bild(to) {

dragobjekt = to;
dragx = posx - dragobjekt.offsetLeft;
dragy = posy - dragobjekt.offsetTop;

}function drag(Ereignis) {

posx = document.all ? window.event.clientX : Ereignis.pageX;
posy = document.all ? window.event.clientY : Ereignis.pageY;

if(dragobjekt != null) {
dragobjekt.style.left = (posx - dragx) + "px";
dragobjekt.style.top = (posy - dragy) + "px";
}

}function dragStop() {

dragobjekt=null;

}-----------------------------------
Dec 23 '06 #2

It's late so I'm not going to load this into a browser and test it so
this may all be completely wrong, or it might help you solve your problem.

When you put an image into your div (or anything really) the image has a
higher z-index than the division which means when the mouse is over that
image the events are going to be for the image, not the division underneath.

The solution is to attach the same onmousedown event to your image as
you do to your division. Well that would work save you're using "this"
to pass the element information. BTW "this" isn't all that reliable in
javascript.

Anyway, as a workaround you might try something like this..

<img src="images/elefant.jpg" onmousedown="bewege_bild(this.offsetParent)">

this.offsetParent basically should kick the object being passed to
bewege_bild up to your division (which is the parent of your image).

Another workaround would be to do one or two <BRbefore the image that
will leave some "raw" white space in your division which the user can grab.

If that doesn't work then hopefully someone else will pass by and help,
or I'll take a look at it all again tomorrow when I'm not
falling-down-tired.

Best of luck and Merry Christmas Eve!

se*****************@googlemail.com wrote:
Hi,

I build my first Drag & Drop with JavaScript and I would like to drag
the pictures when I click on it.

When I create a normal div tag with some text the script works, but
when I put a image in the div tag it works not correctly.

Here is my Code... I integrated three pictures and one text, then you
can see the differences.

a part of my html site:
-----------------------------------
<body onload="autostart()">
<div id="part1">Hier eine Auswahl von Pics:</div>

<div id="bild">

<div onmousedown="bewege_bild(this)"
style="position:absolute;top:200px;left:0px;cursor :move;">
<img src="images/elefant.jpg">
</div>

<div onmousedown="bewege_bild(this)"
style="position:absolute;top:200px;left:150px;curs or:move;">
<img src="images/kaffee.jpg">
</div>

<div onmousedown="bewege_bild(this)"
style="position:absolute;top:200px;left:300px;curs or:move;">
<img src="images/ajax.jpg">
</div>

<div onmousedown="bewege_bild(this)"
style="position:absolute;top:200px;left:450px;curs or:move;">
HELLO
</div>

</div>
-----------------------------------

my JS Script:
-----------------------------------
// JavaScript Document

var posx;
var posy;

var dragx;
var dragy;

var dragobjekt = null;

function autostart() {

var bildDiv = document.getElementById("bild");
var bilder = bildDiv.getElementsByTagName("img");

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

bilder[i].onmousedown = bewege_bild;

}

document.onmouseup = dragStop;
document.onmousemove = drag;
}
function bewege_bild(to) {

dragobjekt = to;
dragx = posx - dragobjekt.offsetLeft;
dragy = posy - dragobjekt.offsetTop;
}

function drag(Ereignis) {

posx = document.all ? window.event.clientX : Ereignis.pageX;
posy = document.all ? window.event.clientY : Ereignis.pageY;
if(dragobjekt != null) {
dragobjekt.style.left = (posx - dragx) + "px";
dragobjekt.style.top = (posy - dragy) + "px";
}
}
function dragStop() {

dragobjekt=null;

}
-----------------------------------

--
http://www.hunlock.com -- Musings in Javascript, CSS.
$FA
Dec 24 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Karsten Schramm | last post by:
Hi, if I drag an Outlook.MailItem to a Windows-Explorer window a <subject>.msg file will be created. Now I try to drag & drop a mail item to my own WinForm app. Unfortunately it doesn't work....
0
by: Plumer | last post by:
Hello everyone, Yesterday I posted a message about implementing drag & drop in a TreeView control. I'm having real difficulty getting this to work -- the process seems to be incredibly...
2
by: Grey | last post by:
I need to design a workflow application with C#. I want to design an UI with some workflow components which they can be drag & drop anywhere in order to design the workflow for the application...
6
by: jojobar | last post by:
Hello, I look at the asp.net 2.0 web parts tutorial on the asp.net web site. I tried to run it under firefox browser but it did not run. If I want to use this feature in a commercial product...
1
by: Terry Olsen | last post by:
My first time using TreeViews. I have TreeView1 set up to display my directory structure just like Windows Explorer. I can drag & drop files and folders over to TreeView2. I can re-arrange the...
3
by: VB Programmer | last post by:
In VB.NET 2005 (winform) any sample code to drag & drop items between 2 listboxes? Thanks!
2
by: Timbo | last post by:
Hi there, I’m not used to working in VB and I think this situation calls for excactly that. I use Access 97 SR-2. My first table is a table containing all the Tickets I got. The field ”Ticket”...
0
by: Truevision .Net | last post by:
Hi, I have a problem with drag and drop functionality when it comes to dropping pictures from sources like for example internet explorer and the webbrowser control. Dragging and dropping from...
5
by: Romulo NF | last post by:
Greetings, I´m back here to show the new version of the drag & drop table columns (original script ). I´ve found some issues with the old script, specially when trying to use 2 tables with...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.