views:

191

answers:

6

Hi

I'm trying to make a pattern in a layout (see attachment for visualisation) The problem is that using :odd :even doesnt work.

I've tried to make it work by using "for loops", and if statements, but obviously jquery doesn't do this stuff in that way. Or maybe i'm supposed to do it outside the document ready statement? I tired that to but it just doesnt work.

How does one go about that?

EDIT: Important note... These squares are max 8 on a page and generated in Wordpress, each square being a post. So I'm not able to divide things into rows as suggested unfortunately.

css:

.thumb_container {
width:274px;
height: 274px;
float: left;
position: relative;
background-color: white;
}

 .thumb_container:nth-child(4n+1) { clear:both; background-color: green } /* green just to see if its working */

Jquery tweek file (http://baked-beans.tv/bb/wp-content/themes/bakedbeans/js/jquery.site.tweeks.js)

    $(".thumb_container:nth-child(8n+2), .thumb_container:nth-child(8n+4), .thumb_container:nth-child(8n+5), .thumb_container:nth-child(8n+7)")
.css({"background-color":"#333333"});

HTML Click HTML for link

switch

+1  A: 
var i = 1;
$('#wrapper > div').each(function()
{
    var isEvenRow = Math.ceil(i / 4) % 2 == 0; // 4 is number of columns
    var isCellAlternate = i % 2 == isEvenRow ? 0 : 1;

    if ( isCellAlternate ) {
        $(this).css("background-color", "#000");
    } else {
        $(this).css("background-color", "#ccc");
    }
    i++;
});​

or the less readable but shorter version:

var i = 1;
$('#wrapper > div').each(function() {
    if (i % 2 == (Math.ceil(i / 4) % 2 == 0) ? 0 : 1) $(this).css("background-color", "#000");
    else $(this).css("background-color", "#ccc");
    i++;
});​

essentially you change the test for the alternate cell every row.

dave thieben
This is what comes out of it:http://jsfiddle.net/xSNsk/92/
RGBK
updated the code. it works in jsfiddle now.
dave thieben
THANK YOU THANK YOU THANK YOU!That works, regardless of lack of divs nested in rowing divs.:-)
RGBK
+1  A: 

Not sure how your markup goes, but you can use the :nth-child(n) selector to achieve a checkerboard effect. I've set up an example for you here. I'm not sure how efficient it will be, although it seems fast enough for a 4x4 grid.

$("div:nth-child(8n+2),div:nth-child(8n+4),div:nth-child(8n+5),div:nth-child(8n+7)")
    .css({"background-color":"white"});​

This repeats a pattern on the 2nd, 4th, 5th and 7th every 8 divs (8n). If you have a different size grid, you'll have to tweak these selectors (and add to them).

It's much simpler if you're using a table - example:

$("tr:odd > td:even, tr:even > td:odd").css({"background-color":"white"});​

If you're willing to use wrapper divs, you can use the rows technique, wrapping every 4 divs in an outer div and using:

<div class="row"><div>1</div><div>2</div><div>3</div><div>4</div></div>
<div class="row"><div>1</div><div>2</div><div>3</div><div>4</div></div>
<div class="row"><div>1</div><div>2</div><div>3</div><div>4</div></div>
<div class="row"><div>1</div><div>2</div><div>3</div><div>4</div></div>
$(".row:odd > div:even, .row:even > div:odd").css({"background-color":"white"});​
Andy E
Whats the latest on use of tables, isn't it "Naughty" to use em and we should all be using divs lest we get executed by the CSS Police? Just curious...
RGBK
Also I dont know why but it doesn't work. I think I undersatnd it, I tweaked it in JS Fiddler and it works the way i think it should but when i put it in my site it doesn't. http://baked-beans.tv/bb/
RGBK
@RGBK: yes, it's ideal not to use tables for layout. You can achieve the same effect using wrapper divs, I added an example for you and you can see it in action at http://jsfiddle.net/rFxgJ/. Of course, then you get judged for having "divitis".
Andy E
@RGBK Though truthfully speaking unless you have content to put into the `div` or table cells or whatever, it should be easier to use background image to achieve this.
Yi Jiang
@RGBK: It looks like it's because your content isn't separated. Visually, you have a 4x4 grid but your markup separates it into a 1x4, a 2x4 and another 1x4. So you have to apply different selectors to the grids. It's probably easiest for you to use the wrapper div example, if you give them a class like "row".
Andy E
@Andy I don't know, my code seems to work fine, Win 7 Stable Channel. Also, surely using `.css('background-color', 'white')` is better than `.css({'background-color', 'white'})` ?
Yi Jiang
@Yi Jiang: I'm also on win7 stable, v5.0. I started with the object literal because I wasn't just setting the background color in my examples and it just stuck that way. I'd say the difference in performance is negligible in any case :)
Andy E
@Andy The thing is I can't separate them by wrappers as it's generated by the loop in Wordpress, essentially each of these are a thumbnail to a post and there could be any amount of results up to 8 max per page. When you say i can apply different selectors to the grid do you mean by the nth-child thing? I would have thought the original one you gave me here (http://jsfiddle.net/xSNsk/) would also work in my code, but for some reason it doesnt.
RGBK
I just relaised why things are spazzing out... http://jsfiddle.net/xSNsk/54/ (This is how it should be working)For some reason if i do this in Wordpress, it clears every single div rather then every fourth div, really weird.So I cant even begin to fix this. Any ideas why that may be?
RGBK
+1  A: 

You can do something like this

$("#wrapper > :nth-child(8n+1), #wrapper > :nth-child(8n+3), #wrapper > :nth-child(8n+6), #wrapper > :nth-child(8n+8)").addClass('dark');

with this HTML:

<div id="wrapper">
    <div></div>
    ... 16 divs
<div>

and this CSS:

#wrapper {
    width: 160px;
    border: 1px solid #999;
    overflow: hidden;
}

#wrapper div {
    width: 40px;
    height: 40px;
    float: left;
    background-color: #fff;
}

#wrapper .dark {
    background-color: #f5f5f5;
}

Using a wrapper here helps so that you do not need to add a class to every div, and you definitely do not need two different classes or colors - simply define a 'default' color, then override it with a added class. Oh, and the :odd, :even and nth-child selectors work on the elements they are attached to - in your case, the .thumb_container element. .thumb_container > :even would work, for future reference.

Have a look at the actual code here: http://jsfiddle.net/HjMrx/

Yi Jiang
Your example doesn't appear to work in Google Chrome. Can't tell why at first glance.
Andy E
@Andy - Eh? It does, I'd just tested it. Which part of it is not working?
Yi Jiang
Same here. On Chrome, aint working. Weird?! Cos it works in FF...
RGBK
Also not in Safari, really weird. I'm on mac.
RGBK
working for me on Chrome.
Anurag
A: 

another way to do it would be [not proofed for function but demonstrates the logic]:

 $('div').click(function(e){
  $tester=$(this).children('div.Other').size();
        for(var $j=0;$j<$tester;$j=$j+1){
                      if($j%2==0){
              $(this).children().eq($j).css(...);
                      }else{
                              $(this).children().eq($j).css(...);
                      }
        }

 });
jason m
A: 

Hi All this is driving me mental.

Also what I dont get about Stack Overflow, as genius as it is, is that you can't just show new alternatives to the same question... Like When You answer you can't just put new code sample in there if you are he one asking the question?! What am i getting wrong here.

So now I'm Answering my question but actually not, I'm just showing the problem in a new way.

So anyway, I'm trying this in every way possible, with no luck, would really appreciate some help here. Check out the page in question here http://baked-beans.tv/bb

It should be making a grid as shown in the first post of this thread, but it's not alternating as it should.

The :nth-child(4n+4) works when i use jsfiddler but not when I do it in wordpress, that's the conundrum. Why would wordpress mess it up?

    function oddRow() {
        $(".thumb_container:odd").css("background-color", "#f00");
        $(".thumb_container:even").css("background-color", "#fff");

    };

    function evenRow() {
        $(".thumb_container:odd").css("background-color", "#fff");
        $(".thumb_container:even").css("background-color", "#f00");
    };


$('.thumb_container').each(function(i) {

     i=(i+1);

     if (i%4==0 ){

        if (switchMe == false){
        switchMe = true;
        }

        else if (switchMe == true){
        switchMe = false;
        }
     }

     if (switchMe == false){
        oddRow();
        $(this).css("background-color", "#000");

     } else if (switchMe == true){
        evenRow();
        $(this).css("background-color", "#000");

     }

});
RGBK
A: 

This can be done in 2 lines of jquery.

Here's the HTML format that I used for this:

<div id="container">
<div class="row">
    <div></div>
    <div></div>
    <div></div>
    <div></div>
</div>
<div class="row">
    <div></div>
    <div></div>
    <div></div>
    <div></div>
</div>
</div>

CSS:

#container {
        width: 816px;
        margin: 0 auto;
    }
    #container .row div {
        width: 100px;
        height: 100px;
        float: left;
        background: #fff;
        border: 1px solid black;
    }
    .dark {
        background: #000 !important;
    }

jquery:

$(document).ready(function() {
        $("#container .row:nth-child(even) div:nth-child(even)").addClass("dark");
        $("#container .row:nth-child(odd) div:nth-child(odd)").addClass("dark");
});

This tells it to set a class of dark for every even div on every even row and set that class on the odd divs in the odd rows.

Brian Ray
Hi Brian thanks for the reply but thats the whole thing... I dont have the luxury of separating the rows by divs like you show in the html! :-( I only have a series of raw divs to work with. Thats the dilemma...
RGBK
Sorry, missed that in your original post.
Brian Ray