Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Sibling selectors and reordering elements

Reply
Thread Tools

Sibling selectors and reordering elements

 
 
eefacm@gmail.com
Guest
Posts: n/a
 
      03-25-2008
I'm composing a simple page that consists largely of a series of
images. Many of the images appear in groups showing the successive
stages of a particular operation. I thought it might look nice to
stack up the images, so that clicking on the topmost image sends it to
the bottom of the stack, showing the image that had been just below
it. Here's my first crack at it, for the simplest case of just two
images:

<style type="text/css">
.cycle { position: relative }
.cycle img { position: absolute; z-index: 1 }
.cycle img + img { top: 2em; left: 2em; z-index: 0 }
</style>
<script type="text/javascript">
function mouseclick(event) {
var target = event.target;
var parent = target.parentNode;
if (target.tagName == "IMG" && parent.className == "cycle") {
parent.appendChild(target);
}
}
document.addEventListener("click", mouseclick, false);
</script>
....
<div class="cycle">
<img src="image1.png">
<img src="image2.png">
</div>

This gives the overlapping visual effect I was after. When I click on
the top image, it jumps down into the lower position, completely
obscuring the lower image, but the lower image does NOT move into the
upper position. I had been expecting that after the appendChild
operation, with the second child of the div now becoming the first
child, the third style rule would no longer be applied to it, but
something about my expectation is clearly incorrect. Anyone have any
idea what might that be?
 
Reply With Quote
 
 
 
 
Tom Cole
Guest
Posts: n/a
 
      03-25-2008
On Mar 25, 2:30*pm, "eef...@gmail.com" <eef...@gmail.com> wrote:
> I'm composing a simple page that consists largely of a series of
> images. *Many of the images appear in groups showing the successive
> stages of a particular operation. *I thought it might look nice to
> stack up the images, so that clicking on the topmost image sends it to
> the bottom of the stack, showing the image that had been just below
> it. *Here's my first crack at it, for the simplest case of just two
> images:
>
> <style type="text/css">
> * .cycle { position: relative }
> * .cycle img { position: absolute; z-index: 1 }
> * .cycle img + img { top: 2em; left: 2em; z-index: 0 }
> </style>
> <script type="text/javascript">
> * function mouseclick(event) {
> * * var target = event.target;
> * * var parent = target.parentNode;
> * * if (target.tagName == "IMG" && parent.className == "cycle") {
> * * * parent.appendChild(target);
> * * }
> * }
> * document.addEventListener("click", mouseclick, false);
> </script>
> ...
> <div class="cycle">
> * <img src="image1.png">
> * <img src="image2.png">
> </div>
>
> This gives the overlapping visual effect I was after. *When I click on
> the top image, it jumps down into the lower position, completely
> obscuring the lower image, but the lower image does NOT move into the
> upper position. *I had been expecting that after the appendChild
> operation, with the second child of the div now becoming the first
> child, the third style rule would no longer be applied to it, but
> something about my expectation is clearly incorrect. *Anyone have any
> idea what might that be?


You may find it easier to either keep an array of image paths and
change the src attribute of the image tag when clicked or (in your
example above) simply play with the style.display attribute of all the
img tags within your div, rather than choosing to keep adding nodes to
the dom. That could get a little excessive as you never seem to remove
them
 
Reply With Quote
 
 
 
 
SAM
Guest
Posts: n/a
 
      03-25-2008
Tom Cole a écrit :
>
> rather than choosing to keep adding nodes to
> the dom. That could get a little excessive as you never seem to remove
> them


used this way appendChild adds nothing
it only moves the element in DOM hierarchy

And, Firefox's Dom Inspector shows their order has changed.

Quite mysterious,

a little as if CSS definitively attached rules on 2nd image in the html
flux.

--
sm
 
Reply With Quote
 
pr
Guest
Posts: n/a
 
      03-26-2008
wrote:
[...]
> <style type="text/css">
> .cycle { position: relative }
> .cycle img { position: absolute; z-index: 1 }
> .cycle img + img { top: 2em; left: 2em; z-index: 0 }
> </style>
> <script type="text/javascript">
> function mouseclick(event) {
> var target = event.target;
> var parent = target.parentNode;
> if (target.tagName == "IMG" && parent.className == "cycle") {
> parent.appendChild(target);
> }
> }
> document.addEventListener("click", mouseclick, false);
> </script>
> ...
> <div class="cycle">
> <img src="image1.png">
> <img src="image2.png">
> </div>
>
> This gives the overlapping visual effect I was after. When I click on
> the top image, it jumps down into the lower position, completely
> obscuring the lower image, but the lower image does NOT move into the
> upper position. I had been expecting that after the appendChild
> operation, with the second child of the div now becoming the first
> child, the third style rule would no longer be applied to it, but
> something about my expectation is clearly incorrect. Anyone have any
> idea what might that be?


You haven't said which browser you're using to look at it in. It works
as you describe in Opera 9.5b and Firefox 3.0b4. If you're using Firefox
< 3.0, then its failure may be explained by <URL:
https://bugzilla.mozilla.org/show_bug.cgi?id=73586>.

You can still do what you want, but it's safest to steer clear of
adjacent sibling selectors, which are relatively recently implemented.
You can either swap the image src to achieve the effect or use class
selectors and swap the className property of the elements, which I
suspect is the more efficient option.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Adjacent Sibling Selectors in CSS Nathan Sokalski ASP .Net 5 06-30-2008 10:20 AM
XSLT: iterating all child elements and accessing homonymous childrenin sibling elements Gerald Aichholzer XML 2 06-27-2006 03:46 PM
reordering elements of a list greenflame Python 7 06-05-2006 04:53 PM
reordering elements of a list greenflame Javascript 2 06-04-2006 12:45 AM
how to find not the next sibling but the 2nd sibling or find sibling "a" OR sinbling "b" localpricemaps@gmail.com Python 11 01-23-2006 07:04 PM



Advertisments