Go Back   SitePoint Forums > Forum Index > Program Your Site > JavaScript
Newsletter FAQ Members List Calendar Mark Forums Read

New to SitePoint Forums? Register here for free!

SitePoint Sponsor
 
Reply
 
Thread Tools Display Modes
Old Nov 7, 2009, 11:49   #1
digitalbart
SitePoint Enthusiast
 
digitalbart's Avatar
 
Join Date: Apr 2002
Location: chicago
Posts: 75
fadein background image

Can a background css image be faded in using jQuery ... I tried putting

$(this).fadeIn("slow").css('background-image', 'url(EggA_' + eid + '.gif)'); and it did not work see code below, this flickers when you click on it
html Code:
<a href="javascript:;" id="egg1" class="eggswap"></a>

JavaScript Code:
$(function() {
    var arr = [ "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12" ];
    jQuery.each(arr, function() {
        var eid = this;       
        if (eid != 7) {
            $("#egg"+eid).one("click", function(evt) {
                $(this).css('background-image', 'url(EggA_' + eid + '.gif)');
                $(this).css('cursor','auto');
            });   
        } else {
             $("#egg"+eid).one("click", function(evt) {
                $(this).css('background-image', 'url(EggA_' + eid + '.gif)');
                $(this).attr("href", "http://urlgoeshere.com/");
                 return false;
            });   
          }
      });
});
digitalbart is offline   Reply With Quote
Old Nov 7, 2009, 12:31   #2
Raffles
Tool
silver trophybronze trophy
SitePoint Award Recipient
 
Raffles's Avatar
 
Join Date: Sep 2005
Location: Cambridge, UK
Posts: 3,804
Wow, a classic WTF example of when an abstraction layer interferes and stops people from using much simpler code! I'm referring to this bit:
javascript Code:
var arr = [ "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12" ];
    jQuery.each(arr, function() {
        var eid = this;       
        if (eid != 7) {
which should be using a for loop!
javascript Code:
for (var i = 1; i < 13; i++) {
  if (i !== 7) {

Regarding the problem you've asked about, with the code you posted at the top, the problem is likely that the background image is being applied with every iteration of the fading process. Try it the other way round:
javascript Code:
$(this).css('background-image', 'url(EggA_' + eid + '.gif)').fadeIn("slow");
Since it's being faded in, it shouldn't matter if it's applied before the fading begins.
Raffles is offline   Reply With Quote
Old Nov 9, 2009, 08:11   #3
digitalbart
SitePoint Enthusiast
 
digitalbart's Avatar
 
Join Date: Apr 2002
Location: chicago
Posts: 75
Thanks for the ideas.

The fade needs to be applied when you click I am not sure where to put it then?
JavaScript Code:
$("#egg"+eid).one("click", function(evt) {
                // $(this).css('background-image', 'url(EggA_' + eid + '.gif)');
                $(this).css('background-image', 'url(EggA_' + eid + '.gif)').fadeIn("slow");
digitalbart is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread | Next Thread »

Thread Tools
Display Modes

 
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
Sponsored Links
 
Forum Jump


All times are GMT -7. The time now is 22:31.


Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Copyright 1998-2009, SitePoint Pty Ltd. All Rights Reserved