473,396 Members | 1,738 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,396 software developers and data experts.

Eventhandling and positioning of objects

Hallo,

for hours i'm searching for an solution for the following problem:
Inside a container i have some cathegories and in each of them some
questions (look at HTML),
The questions should be shown or hidden when clicking a cathegory (look
JS and CSS).
With a click on a question, it should be cloned and the clone should be
placed over the question so you can drag and drop it from there to
somewhere else.
The problem:
The clone is positioned in the upper left corner of "container" instead
of "question".
It looks like i get wrong values for offsetX (and -Y and layerX (and
-Y) in the function klonen()
(those for "container" instead of i.g. "cat1ques1".
Can someone tell me what's going wrong?

Many thanks and best regards

Michael
----------- HTML -----------------

<div id="container" class="kontainer">
<div id="vorlagen" class="vorlagen">
<div class="cathegory">
<div id="cat1" onclick="klappKat(this.id)">Kategorie1</div>
<div id="kat1frag1" class="frage">
cat1ques1
</div>
<div id="cat1ques2" class="frage">
cat1ques2
</div>
</div>
<div class="cathegory">
<div id="cat2" onclick="klappKat(this.id)">Kategorie2</div>
<div id="cat2ques1" class="frage">
kat2frag1
</div>
<div id="cat2ques2" class="frage">
kat2frag2
</div>
</div>
</div>
</div>

----------- JAVASCRIPT -----------------

// cathegory navigation (show and hide questions)
function klappKat (where) {
if(!opend) { // var opend is globally, in the beginning it's false
klappe(where, "open");
}
else {
if (opend == where) {
klappe(where, "close");
opend = false;
return true;
}
else if (opend != where) {
klappe(opend, "close");
klappe(where, "open");
}
}
opend = where;
}

function klappe(where, how) {
var anzeige = "block";
if (how == "close") anzeige = "none";
var el = document.getElementById(where);
el = el.nextSibling;
while (el != null) {
if (el.nodeType == 1) {
el.style.display = anzeige;
if (el.attachEvent) el.attachEvent("onmousedown", klonen);
if (el.addEventListener) el.addEventListener("mousedown", klonen,
false);
}
el = el.nextSibling;
}
}

function klonen(e) {
// event target
var was;
if(ie) was = event.srcElement.id;
if(!ie) was = e.target.id;

// if there's already a clone, prevent multiple cloning
if (was.indexOf("klon") != -1) {
bewegflag = true;
}

// if there's no clone
else {
// cloning
var el = document.getElementById(was);
var klon = el.cloneNode(true);
var klasse = document.createAttribute("class");
klasse.nodeValue = "frageklon";
klon.setAttributeNode(klasse);
var klonID = document.createAttribute("id");
klonID.nodeValue = was + "klon";
klon.setAttributeNode(klonID);
IDklon = was + "klon"; // IDklon global

// positioning - get position
if(!e) var e = window.event;
var ev = e;
if (ie) ev.cancelBubble = true;
if (ev.stopPropagation) ev.stopPropagation();

posarr[0] = el.offsetWidth; // posarr ist global
posarr[1] = el.offsetHeight;
posarr[2] = ev.clientX;
posarr[3] = ev.clientY;
if (ie) {
posarr[4] = ev.offsetX; // maybe here is the problem?
posarr[5] = ev.offsetY; // and here
}
else if (!ie) {
posarr[4] = ev.layerX; // or here
posarr[5] = ev.layerY; // or here
}
var lin = (posarr[2] - posarr[4]);
var obn = (posarr[3] - posarr[5]);

var pos = posarr[0]+" | "+posarr[1]+" | "+ posarr[2]+" | "+
posarr[3]+" | "+ posarr[4]+" | "+ posarr[5] + " |lin: " + lin + " |obn:
" + obn;
window.status = pos;

// set position
klon.setAttribute("style", "");
var ks = klon.style;
ks.left = parseInt(lin) + "px";
ks.top = parseInt(obn) + "px";
ks.width = parseInt(posarr[0]-15) + "px";

// append clone to document
el.appendChild(klon);
}
}

------------- CSS --------------
..kontainer {
width: 600px;
}

..kontainer .vorlagen {
font: 1em/1.3 Verdana, Arial, Helvetica, sans-serif;
background: #d9d9d9;
width: 500px;
border: 2px outset #708090;
padding: 15px;
overflow: auto;
}

..kontainer .vorlagen .kategorie {
letter-spacing: 0.8em;
font-weight: bold;
color: #FFFAFA;
background: #ccc;
border: 3px inset #708090;
padding: 10px 10px 10px 10px;
display: block;
}

..kontainer .vorlagen .kategorie .frage {
font: 0.8em/1.2 Verdana, Arial, Helvetica, sans-serif;
background: #FFFFF0;
color: #000;
border: 2px outset #708090;
padding: 5px 5px 5px 5px;
display: none;
}

..kontainer .vorlagen .kategorie .frageklon {
font: 0.6em/1.2 Verdana, Arial, Helvetica, sans-serif;
background: #9FA0FF;
color: #000;
border: 2px outset #708090;
padding: 5px 5px 5px 5px;
display: inline;
position: absolute;
top: 0;
left: 0;
z-index:100;
cursor: move;
}

Jun 12 '06 #1
2 1457
Michi wrote:
The clone is positioned in the upper left corner of "container"
instead of "question".
It looks like i get wrong values for offsetX (and -Y and layerX (and
-Y) in the function klonen()


You should be using offsetTop and offsetLeft, and walking up the element
chain to add each up. Read up about these properties online and how they are
values relative to the offsetParent element.

The logic to find the position of an arbitrary object on a page can be found
here:
http://www.javascripttoolbox.com/lib/objectposition/

This lib is much more accurate than others which simply sum up the offset*
values to find position and could surely be plugged into your code to easily
get accurate positioning results.

Good luck!

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Jun 12 '06 #2
For drag and drop, try using the YAHOO UI library:

http://developer.yahoo.com/yui/

Well documented, reasonably easy to use, good browser compatibility.

ExG
Jun 13 '06 #3

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

Similar topics

4
by: Jane Withnolastname | last post by:
I am trying to re-work an old site by replacing the html with css. On the main page, I have a logo image which I needed centred on the initial screen. I found the solution here:...
11
by: NS | last post by:
I am relativly new to css positioning and have a question regarding the display of a DHTML pop-up Here is the basic HTML I am using: <html> <head> <script language="JavaScript"> <!--
4
by: Marc Tanner | last post by:
Hello, I am currently working on a eventhandling system or something similar, and have the problem of loosing scope. I have read many interesting posts on this group and the faq article about...
3
by: Bill Zack | last post by:
I have an interesting little ASP.NET problem. I have to position and show multiple "frames" in an IE6 browser at run time in a singe ASP.NET page. Each "frame" will contain a couple of...
3
by: Claes Rådström | last post by:
Hi ! We have a base class that derives from System.Web.UI.Page Alla our pages derives from it. In our base class we want to have an access check, (own) , to verfy user access to the derived...
2
by: Jawiko | last post by:
I am trying to place objects like buttons, calendar, listboxes etc on a webform. I am strugling with the positioning. I placed all the objects in a cell and the cell centers itself on the page....
6
by: Mark | last post by:
hi, i'm trying to position something in the top right corner of a container, but i can't seem to figure out how to get it working. here's the html <div class='thumb'><a href='image.jpg'><img...
1
by: final farewell | last post by:
Hello, I am using Ekhau Location Tracking System for an assignment. It is a system that is able to track configured tags attached to people or objects. I am new to the Ekahau Location Tracking...
1
by: mohammadtaha | last post by:
Hi, I am using CSS to position objects (<SPAN> and <DIV> tags) on my webpage, which is working great on regular computer monitors, when I view the same page on a wide screen laptop monitor,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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.