3

I would like to blink my menu text. I have this code, but it doesn't work with IE.

(function($)
{
    $.fn.blink = function(options) {
        var defaults = { delay:500 };
        var options = $.extend(defaults, options);

        return this.each(function() {
            var obj = $(this);
            setInterval(function() {
                if($(obj).css("color") == "rgb(255, 0, 0)")
                {
                    $(obj).css('color','#000000');
                }
                else
                {
                    $(obj).css('color','rgb(255, 0, 0)');
                }
            }, options.delay);
        });
    }
}(jQuery))

$(document).ready(function(){$('.blink').blink()})

Can someone help me? Thank you!

Tomas Aschan
  • 58,548
  • 56
  • 243
  • 402
Nathaniel
  • 43
  • 1
  • 3

4 Answers4

5

The Mini-Effects plug-ins should be simpler here--very small and clearly efficient if this is all you need from the UI Effects Library (aside from those other essentials, "throb", "shake", and "bob").

Simple to use--just load the mini-effects plugin you need, then just call blink() on the element you want to blink.

<script type="text/javascript" charset="utf-8" src="javascripts/jquery.blink.min.js"></script>

Then, just call blink() on some large brightly-colored resource:

$(".selector").blink();
doug
  • 69,080
  • 24
  • 165
  • 199
  • are you referring to the blinking rate (pretty sure it's the same as blink method in main UI Effects Library) or cross-browser fidelity or neither? – doug Jul 29 '10 at 11:27
2

You set obj as $(this), so you must call obj every time instead of $(obj).

Just replace

obj = $(this);

With just

obj = this;

But still think about people with epileption, bad sight, etc.

  • 1
    Actually, that's not relevant. Since he does `.each()` on the wrapped set, `this` in this context is just a single DOM element. By wrapping it in `$()`, he makes it a jQuery set again, but it's still just one element. – Tomas Aschan Jul 29 '10 at 10:41
  • On second thought, I think you might be on to something. But it would be more effective to replace `$(obj)` with `obj` instead, and just perform the re-wrapping once. – Tomas Aschan Jul 29 '10 at 10:45
1

In explorer:

if($(obj).css("color") == "rgb(255, 0, 0)")

is not true, because IE sees this:

 $(obj).css("color") == "rgb(255,0,0)";

Without spaces between numbers.

You can fix it by changing:

$(obj).css('color','rgb(255, 0, 0)');

$(obj).css('color','rgb(255,0,0)');

and

if($(obj).css("color") == "rgb(255, 0, 0)")

to

if($(obj).css("color") == "rgb(255,0,0)")

so:

(function($)
{
    $.fn.blink = function(options) {
        var defaults = { delay:500 };
        var options = $.extend(defaults, options);

        return this.each(function() {
            var obj = $(this);
            setInterval(function() {
                if($(obj).css("color") == "rgb(255,0,0)")
                {
                    $(obj).css('color','#000000');
                }
                else
                {
                    $(obj).css('color','rgb(255,0,0)');
                }
            }, options.delay);
        });
    }
}(jQuery))
$(document).ready(function(){$('.blink').blink()})

EDITED:

            (function($)
{
    $.fn.blink = function(options) {
        var defaults = { delay:500 };
        var options = $.extend(defaults, options);

        return this.each(function() {
            var obj = $(this);
            var state = false;
            setInterval(function() {
                if(state)
                {
                    $(obj).css('color','#000000');
                    state = false;
                }
                else
                {
                    $(obj).css('color','rgb(255,0,0)');
                    state = true;
                }
            }, options.delay);
        });
    }
}(jQuery))
Peter Porfy
  • 8,921
  • 3
  • 31
  • 41
0

Have you checked the code with Firebug, or the built-in developer tools in Chrome? I would expect you need to change

}(jQuery))

into

})(jQuery)

(move the parenthesis around...)

Tomas Aschan
  • 58,548
  • 56
  • 243
  • 402