0

I have a load of images overlapping, I just done this by giving a css style of margin:0 -20px 0 -20px;. They overlap the one on the left and the one above, its to unioform looking. Is there any way to use js/jquery to give a class a random z-index?

Cheers................

2
Contributors
12
Replies
27
Views
4 Years
Discussion Span
Last Post by GlenRogers
0

Yes I would imagine implementing this by simply creating a function that reads in the length (count) of images then during a loop pick a random value, the. Use the .css() method to apply it.

If you attmo to provide code, we can probably help tweak this concept.

0

Hey thanks for the reply.

This is the code I have tried

<script type="text/javascript">
        function nailthumb-container{
                document.getElementByClass('nailthumb-container').style.zIndex = Math.floor(Math.random() * 256);
            }
    </script>

But this doesnt work!

0

So I also tried this

<script type="text/javascript">
        $(document).ready(function(){
            $(".nailthumb-container").css('zIndex','Math.floor(Math.random() * 256'));
      });
    </script>

This is the css for .nailthumb-container

.nailthumb-container {
  width: 120px;
  height: 120px;
  margin:0 -30px -45px 0;
  padding: 0px;
  overflow: hidden;
  text-align: left;
  float:left
}

Not working either...................

0

I dont have a moment right now to do some testing with this concept... in the meantime, with the samples you tried, did you look at the source code to see if in fact, the styles are being applied? did you check with any browser dev tool?

0

I checked the source and it seems that the styles are not being applied! Ive never used any browser dev tools! I'll look into it now.......................

0

So, try this when applying CSS via jQuery...

var randomNum = do your random math here...
$(".nailthumb-container").css('z-index', randomNum);

0

This is what I have now

<script type="text/javascript">
        var randomNum = Math.floor(Math.random() * 256)
        $(".nailthumb-container").css('z-index', randomNum);
            )};
    </script>
0

Ok, so as I mentioned in my first response, you need to loop through the elements and apply a random z-index value to each. I took a look at your example and put something together which you should be able to work from.

here is the HTML code:

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
</head>
<body>

<div class="nailthumb-container"></div>
<div class="nailthumb-container"></div>
<div class="nailthumb-container"></div>
<div class="nailthumb-container"></div>

<script>
  $( ".nailthumb-container").each(function() {
    $(this).css('z-index', Math.floor(Math.random() * 256));
});
</script>

</body>
</html>

Here is a screenshot from Chrome's Dev tool showing that random z-index's were applied...
eae9feea43ac7ce2d78cbddfd45879a5

If you look at the script block, all that it is doing is looping through each of the elements that are assigned the class of "nailthumb-container". Then we assign the random z-index to the current item within the loop. The loop continues until all items are evaluated.

Edited by JorgeM

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.