How can I blink with jQuery?

你离开我真会死。 提交于 2019-11-27 07:16:12

问题


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!


回答1:


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();



回答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.




回答3:


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))



回答4:


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...)



来源:https://stackoverflow.com/questions/3361564/how-can-i-blink-with-jquery

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!