Different colours for different articles using a:hover

916 views Asked by At

I'm trying to assign three different colours to three different articles on a single page using a:hover in Wordpress.

This is the site http://paragrams.wpshower.com/

At present all the articles turn pale yellow on a:hover. I'm trying to create different colours for each article (for example, first article to be yellow, the second article red, third blue and so on).

Below is the php & CSS for the a:hover at present. I assume I need to wrap each thumb id in a different div and specify the colour in the CSS?

PHP:

<ul class="mcol">
          <?php if(have_posts()) : ?><?php while(have_posts()) : the_post(); ?>
            <li class="article">

                    <?php
                    if ( has_post_thumbnail() ) { ?>
                    <?php 
                    $imgsrcparam = array(
                    'alt'   => trim(strip_tags( $post-    >post_excerpt )),
                    'title' => trim(strip_tags( $post-    >post_title )),
                    );
                    $thumbID = get_the_post_thumbnail( $post->ID, 'background',     $imgsrcparam ); ?>



                    <div><a href="<?php the_permalink() ?>" class="preview"><?php echo     "$thumbID"; ?></a></div>
                    <?php } ?>


                <h2><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>

and CSS:

.li_container {
background-attachment: scroll;
background-image: url(images/main-bg.gif);
background-repeat: repeat-y;
background-position: left top;
}
.li_container .article:hover {
background-color:#57bfeb;
}

This is the js:

  #  * Splits a <ul>/<ol>-list into equal-sized columns. 
#  *  
#  * Requirements:  
#  * <ul> 
#  * <li>"ul" or "ol" element must be styled with margin</li> 
#  * </ul> 
#  *  
#  * @see http://www.codeasily.com/jquery/multi-column-list-with-jquery 
#  */  
jQuery.fn.makeacolumnlists = function(settings){
settings = jQuery.extend({
    cols: 3,                // set number of columns
    colWidth: 0,            // set width for each column or leave 0 for     auto width
    equalHeight: 'li',  // can be false, 'ul', 'ol', 'li'
    startN: 1               // first number on your ordered list
}, settings);

if(jQuery('> li', this)) {
    this.each(function(y) {
        var y=jQuery('.li_container').size(),
            height = 0, 
            maxHeight = 0,
            t = jQuery(this),
            classN = t.attr('class'),
            listsize = jQuery('> li', this).size(),
            percol = Math.ceil(listsize/settings.cols),
            contW = t.width(),
            bl = ( isNaN(parseInt(t.css('borderLeftWidth'),10)) ? 0 :     parseInt(t.css('borderLeftWidth'),10) ),
            br = ( isNaN(parseInt(t.css('borderRightWidth'),10)) ? 0 :     parseInt(t.css('borderRightWidth'),10) ),
            pl = parseInt(t.css('paddingLeft'),10),
            pr = parseInt(t.css('paddingRight'),10),
            ml = parseInt(t.css('marginLeft'),10),
            mr = parseInt(t.css('marginRight'),10),
            col_Width = Math.floor((contW - (settings.cols-1)*(bl+br+pl+pr+ml+mr))/settings.cols);
        if (settings.colWidth) {
            col_Width = settings.colWidth; 
        }
        var colnum=1,
            percol2=percol;
        jQuery(this).addClass('li_cont1').wrap('<div id="li_container' + (++y) + '" class="li_container"></div>');
        for (var i=0; i<=listsize; i++) {
            if (colnum > settings.cols) colnum = 1;
            var eq = jQuery('> li:eq('+i+')',this);
            eq.addClass('li_col'+colnum);
            colnum++;
            //if(i>=percol2) { percol2+=percol; colnum++; }
            //var eq = jQuery('> li:eq('+i+')',this);
            //eq.addClass('li_col'+ colnum);
            //if(jQuery(this).is('ol')){eq.attr('value', ''+(i+settings.startN))+'';}
        }
        jQuery(this).css({cssFloat:'left', width:''+col_Width+'px'});
        for (colnum=2; colnum<=settings.cols; colnum++) {
            if(jQuery(this).is('ol')) {
                jQuery('li.li_col'+ colnum, this).appendTo('#li_container' + y).wrapAll('<ol class="li_cont'+colnum +' ' + classN + '" style="float:left; width: '+col_Width+'px;"></ol>');
            } else {
                jQuery('li.li_col'+ colnum, this).appendTo('#li_container' + y).wrapAll('<ul class="li_cont'+colnum +' ' + classN + '" style="float:left; width: '+col_Width+'px;"></ul>');
            }
        }
        if (settings.equalHeight=='li') {
            for (colnum=1; colnum<=settings.cols; colnum++) {
                jQuery('#li_container'+ y +' li').each(function() {
                    var e = jQuery(this);
                    var border_top = ( isNaN(parseInt(e.css('borderTopWidth'),10)) ? 0 : parseInt(e.css('borderTopWidth'),10) );
                    var border_bottom = ( isNaN(parseInt(e.css('borderBottomWidth'),10)) ? 0 : parseInt(e.css('borderBottomWidth'),10) );
                    height = e.height() + parseInt(e.css('paddingTop'), 10) + parseInt(e.css('paddingBottom'), 10) + border_top + border_bottom;
                    maxHeight = (height > maxHeight) ? height : maxHeight;
                });
            }
            for (colnum=1; colnum<=settings.cols; colnum++) {
                var eh = jQuery('#li_container'+ y +' li');
                var border_top = ( isNaN(parseInt(eh.css('borderTopWidth'),10)) ? 0 : parseInt(eh.css('borderTopWidth'),10) );
                var border_bottom = ( isNaN(parseInt(eh.css('borderBottomWidth'),10)) ? 0 : parseInt(eh.css('borderBottomWidth'),10) );
                mh = maxHeight - (parseInt(eh.css('paddingTop'), 10) + parseInt(eh.css('paddingBottom'), 10) + border_top + border_bottom );
                eh.height(mh);
            }
        } else 
        if (settings.equalHeight=='ul' || settings.equalHeight=='ol') {
            for (colnum=1; colnum<=settings.cols; colnum++) {
                jQuery('#li_container'+ y +' .li_cont'+colnum).each(function() {
                    var e = jQuery(this);
                    var border_top = (     isNaN(parseInt(e.css('borderTopWidth'),10)) ? 0 : parseInt(e.css('borderTopWidth'),10) );
                    var border_bottom = ( isNaN(parseInt(e.css('borderBottomWidth'),10)) ? 0 : parseInt(e.css('borderBottomWidth'),10)     );
                    height = e.height() + parseInt(e.css('paddingTop'),     10) + parseInt(e.css('paddingBottom'), 10) + border_top + border_bottom;
                    maxHeight = (height > maxHeight) ? height :     maxHeight;
                });
            }
            for (colnum=1; colnum<=settings.cols; colnum++) {
                var eh = jQuery('#li_container'+ y +'     .li_cont'+colnum);
                var border_top = (     isNaN(parseInt(eh.css('borderTopWidth'),10)) ? 0 : parseInt(eh.css('borderTopWidth'),10) );
                var border_bottom = ( isNaN(parseInt(eh.css('borderBottomWidth'),10)) ? 0 : parseInt(eh.css('borderBottomWidth'),10) );
                mh = maxHeight - (parseInt(eh.css('paddingTop'), 10) + parseInt(eh.css('paddingBottom'), 10) + border_top + border_bottom );
                /*eh.height(mh);*/
            }
        }
        jQuery('#li_container' + y).append('<div style="clear:both; overflow:hidden; height:0px;"></div>');
    });
}
}

jQuery.fn.uncolumnlists = function(){
jQuery('.li_cont1').each(function(i) {
    var onecolSize = jQuery('#li_container' + (++i) + ' .li_cont1 > li').size();
    if(jQuery('#li_container' + i + ' .li_cont1').is('ul')) {
        jQuery('#li_container' + i + ' > ul > li').appendTo('#li_container'     + i + ' ul:first');
        for (var j=1; j<=onecolSize; j++) {
            jQuery('#li_container' + i + ' ul:first li').removeAttr('class').removeAttr('style');
        }
        jQuery('#li_container' + i + ' ul:first').removeAttr('style').removeClass('li_cont1').insertBefore('#li_container' + i);
    } else {
        jQuery('#li_container' + i + ' > ol > li').appendTo('#li_container' + i + ' ol:first');
        for (var j=1; j<=onecolSize; j++) {
            jQuery('#li_container' + i + ' ol:first li').removeAttr('class').removeAttr('style');
        }
        jQuery('#li_container' + i + ' ol:first').removeAttr('style').removeClass('li_cont1').insertBefore('#li_container' + i);
    }
    jQuery('#li_container' + i).remove();
});
}
3

There are 3 answers

0
t31os On

What i'd personally do is add an incrementing class using PHP, like so..

<ul class="mcol">
<?php if(have_posts()) : $i = 0; while(have_posts()) : the_post(); $i++; ?>
    <li class="article <?php echo "item-$i";?>">

You then get something like..

<li class="article item-1">content</li>
<li class="article item-2">content</li>
<li class="article item-3">content</li>

Then toggle a class on hover to..

$(document).ready(function(){
    $('li.article').hover(function(){
        $(this).toggleClass('active-item');
    });
});

That then gives you one class that covers all list items, one unique to each item shown, and one that is only attached to the element on hover..

li.article { /* CSS for all list items */ }
li.article.active-item { /* CSS for all any item active */ }
li.item-1 { /* CSS for the first item */ }
li.item-2 { /* CSS for the second item */ }
li.active-item.item-1 { /* CSS for the first item when hovered */ }
li.active-item.item-2 { /* CSS for the secpnd item when hovered */ }

NOTE: It doesn't hurt to make your CSS selectors more specific, adding an ID infront of those(say, the parent list ID) will help ensure you get less conflict with other competing styles.

I hope that's helpful.. ;)

6
eykanal On

You wouldn't need to do anything fancy with divs; just give each li tag a unique class, and specify those class colors in css.

From your code, you could change the line

<li class="article">

to

<?php
switch($post->ID) {
    case 1:
        $class = 'yellow';    break;
    case 2:
        $class = 'blue';      break;
    case 3:
        $class = 'green';     break;
}
?>
<li class="article <?php echo $class; ?>">

This will effectively output class='article yellow' for the first article, class='article blue' for the second, etc. You can then make this all work by changing the css as follows:

.li_container .article.yellow:hover { background-color:#57bfeb; }
.li_container .article.green:hover  { background-color:green; }
.li_container .article.blue:hover   { background-color:blue; }

If you want to get fancy, you could also change switch($post->ID) to switch(mod($post->ID,3)) to do lots of color changing.

6
toddles2000 On

eykanal has a good solution. Just to throw out another option, you could use jquery and just dynamically add the classes based on the article positions in the dom. Something like:

Add this inside the head element of your page:

<script type="text/javascript"src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script>
<script type="text/javascript">
  $(document).ready(function () {
    $('.mcol li.article:nth-child(1)').addClass('yellow')
    $('.mcol li.article:nth-child(2)').addClass('red')
    $('.mcol li.article:nth-child(3)').addClass('blue') 
  });
</script>

You would then add something like this to your css file:

.yellow:hover { background-color: yellow; }
.red:hover { background-color: red; }
.blue:hover { background-color: blue; }

Change the colors to the hex codes of your choice of course. Have Fun!