tags:

views:

4876

answers:

6

I would like to implement something similar to 37Signals's Yellow Fade effect.

I am using Jquery 1.3.2

The code

(function($) {
   $.fn.yellowFade = function() {
    return (this.css({backgroundColor: "#ffffcc"}).animate(
            {
                backgroundColor: "#ffffff"
            }, 1500));
   }
 })(jQuery);

and the next call show yellow fade the DOM elment with box id.

$("#box").yellowFade();

But it only makes it yellow. No white background after 15000 milliseconds.

Any idea why it is not working?

Solution

You can use:

$("#box").effect("highlight", {}, 1500);

But you would need to include:

effects.core.js
effects.highlight.js

+17  A: 

This function is part of jQuery effects.core.js :

$("#box").effect("highlight", {}, 1500);

As Steerpike pointed out in the comments, effects.core.js and effects.highlight.js need to be included in order to use this.

Baldu
I saw the demo in the jquery site http://docs.jquery.com/UI/Effects/Highlight#overviewI have tried in my code but does not do anything. Do I need to download any extra. It says dependencies: Effects Core. It is this another plugin.
Sergio del Amo
Correct answer, but I'll just mention it's a built in function to the jQuery effects.core.js, not to the core jQuery file as animate(). Just thought it was worth clarifying.
Steerpike
Heh, as Sergio has so obviously just discovered...yes Sergio, you need to include the effects.core.js file and effects.highlight.js (check the source here: http://docs.jquery.com/UI/Effects/Highlight)
Steerpike
@R.Bemrose. Effect function it is not listed in the link's list you posted
Sergio del Amo
@Steerpike. Do I have to download effects.core.js? and where from?
Sergio del Amo
http://dev.jquery.com/view/tags/ui/latest/ui/effects.core.js
Steerpike
http://dev.jquery.com/view/tags/ui/latest/ui/effects.highlight.js
Steerpike
It's not built into jQuery...
J-P
This is available in jQuery UI: http://docs.jquery.com/UI/Effects/Highlight#overview
Matt Scilipoti
A: 
(function($) {
  $.fn.yellowFade = function() {
    this.animate( { backgroundColor: "#ffffcc" }, 1 ).animate( { backgroundColor: "#ffffff" }, 1500 );
  }
})(jQuery);

Should do the trick. Set it to the yellow, then fade it to white

Phil Carter
Thanks for the answer. It is not working though. It seems animate does not do anything.
Sergio del Amo
what version of jQuery are you using?
Phil Carter
1.3.2. I added this to my question
Sergio del Amo
+1  A: 
function YellowFade(selector){
   $(selector)
      .css('opacity', 0)
      .animate({ backgroundColor: 'Yellow', opacity: 1.0 }, 1000)
      .animate({ backgroundColor: '#ffffff', opacity: 0.0}, 350, function() {
             this.style.removeAttribute('filter');
              });
}

The line this.style.removeAttribute('filter') is for an anti-aliasing bug in IE.

Now, call YellowFade from wherever, and pass your selector

YellowFade('#myDivId');

Credit: Phil Haack had a demo showing exactly how to do this. He was doing a demo on JQuery and ASPNet MVC.

Take a look at this video

Update: Did you take a look at the video? Forgot to mention this requires the JQuery.Color plugin

Vin
The next error is being thrown by the filter line. I am using yellow fade in a table row element (tr) "this.style.removeAttribute is not a function [Break on this error] this.style.removeAttribute('filter');"
Sergio del Amo
+3  A: 

I just solved a problem similar to this on a project I was working on. By default, the animate function cannot animate colors. Try including jQuery Color Animations.

All the answers here use this plugin but no one mentions it.

T Pops
A: 

I hated adding 23kb just to add effects.core.js and effects.highlight.js so I wrote the following. It emulates the behavior by using fadeIn (which is part of jQuery itself) to 'flash' the element:

$.fn.faderEffect = function(options){
    options = jQuery.extend({
     count: 3, // how many times to fadein
     speed: 500, // spped of fadein
     callback: false // call when done
    }, options);

    return this.each(function(){

     // if we're done, do the callback
     if (0 == options.count) 
     {
      if ( $.isFunction(options.callback) ) options.callback.call(this);
      return;
     }

     // hide so we can fade in
     if ( $(this).is(':visible') ) $(this).hide();

     // fade in, and call again
     $(this).fadeIn(options.speed, function(){
      options.count = options.count - 1; // countdown
      $(this).faderEffect(options); 
     });
    });
}

then call it with $('.item').faderEffect();

Corey Maass
This is not the same as the yellow fade which the user required.
Jon Winstanley
Right. I'm suggesting an alternative or "something similar".
Corey Maass
+1  A: 

Actually, I have a solution that only needs jQuery 1.3x, and no aditionnal plugin.

First, add the following functions to your script

function easeInOut(minValue,maxValue,totalSteps,actualStep,powr) {
    var delta = maxValue - minValue;
    var stepp = minValue+(Math.pow(((1 / totalSteps)*actualStep),powr)*delta);
    return Math.ceil(stepp)
}

function doBGFade(elem,startRGB,endRGB,finalColor,steps,intervals,powr) {
    if (elem.bgFadeInt) window.clearInterval(elem.bgFadeInt);
    var actStep = 0;
    elem.bgFadeInt = window.setInterval(
     function() {
      elem.css("backgroundColor", "rgb("+
       easeInOut(startRGB[0],endRGB[0],steps,actStep,powr)+","+
       easeInOut(startRGB[1],endRGB[1],steps,actStep,powr)+","+
       easeInOut(startRGB[2],endRGB[2],steps,actStep,powr)+")"
      );
      actStep++;
      if (actStep > steps) {
      elem.css("backgroundColor", finalColor);
      window.clearInterval(elem.bgFadeInt);
      }
     }
     ,intervals)
}

Next, call the function using this:

doBGFade( $(selector),[245,255,159],[255,255,255],'transparent',75,20,4 );

I'll let you guess the parameters, they are pretty self explanatory. To be honest the script is not from me, I took it on a page then changed it so it works with the latest jQuery.

NB: tested on firefox 3 and ie 6 (yes it works on that old thing too)