我是全新的jQuery和有一些使用原型的经验。在Prototype中,有一个方法来“闪现”一个元素。简单地用另一种颜色突出它,然后让它变回正常,这样用户的眼睛就会被吸引到它上面。jQuery中有这样的方法吗?我看到淡出,淡出,和动画,但我没有看到任何像“闪光”。也许这三个中的一个可以与适当的输入一起使用?


当前回答

5年后……(并且不需要额外的插件)

这一个“脉冲”它到你想要的颜色(例如白色),在它后面放一个div背景色,然后淡入对象。

HTML对象(例如按钮):

<div style="background: #fff;">
  <input type="submit" class="element" value="Whatever" />
</div>

jQuery(香草,没有其他插件):

$('.element').fadeTo(100, 0.3, function() { $(this).fadeTo(500, 1.0); });

元素——类名

fadeTo()中的第一个数字-转换的毫秒数

fadeTo()中的第二个数字-对象在fade/unfade之后的不透明度

你可以在这个网页的右下角查看:https://single.majlovesreg.one/v1/

编辑(willsteel)无重复选择器使用$(this)和调整值实际执行一个闪光(作为OP请求)。

其他回答

我正在寻找这个问题的解决方案,但不依赖jQuery UI。

这就是我想出的,它为我工作(没有插件,只是Javascript和jQuery); -这是工作小提琴- http://jsfiddle.net/CriddleCraddle/yYcaY/2/

在你的CSS文件中设置当前的CSS参数为普通的CSS,并创建一个新类,只处理参数的变化,即background-color,并将其设置为'!“重要”来覆盖默认行为。像这样…

.button_flash {
background-color: #8DABFF !important;
}//This is the color to change to.  

然后只需使用下面的函数并将DOM元素作为一个字符串传递,一个表示您希望flash发生次数的整数,一个表示您希望更改到的类的整数,以及一个表示延迟的整数。

注意:如果你为'times'变量传递一个偶数,你将得到你开始时的类,如果你传递一个奇数,你将得到被切换的类。两者都有不同的用途。我用“I”来改变延迟时间,否则它们会同时发射,效果就会丢失。

function flashIt(element, times, klass, delay){
  for (var i=0; i < times; i++){
    setTimeout(function(){
      $(element).toggleClass(klass);
    }, delay + (300 * i));
  };
};

//Then run the following code with either another delay to delay the original start, or
// without another delay.  I have provided both options below.

//without a start delay just call
flashIt('.info_status button', 10, 'button_flash', 500)

//with a start delay just call
setTimeout(function(){
  flashIt('.info_status button', 10, 'button_flash', 500)
}, 4700);
// Just change the 4700 above to your liking for the start delay.  In this case, 
//I need about five seconds before the flash started.  

你可以进一步扩展Desheng Li的方法,允许迭代计数进行多次闪光,如下所示:

// Extend jquery with flashing for elements
$.fn.flash = function(duration, iterations) {
    duration = duration || 1000; // Default to 1 second
    iterations = iterations || 1; // Default to 1 iteration
    var iterationDuration = Math.floor(duration / iterations);

    for (var i = 0; i < iterations; i++) {
        this.fadeOut(iterationDuration).fadeIn(iterationDuration);
    }
    return this;
}

然后你可以调用方法的时间和闪光的数量:

$("#someElementId").flash(1000, 4); // Flash 4 times over a period of 1 second

创建两个类,给每个类一个背景色:

.flash{
 background: yellow;
}

.noflash{
 background: white;
}

用下面的类创建一个div:

<div class="noflash"></div>

下面的函数将切换类并使其显示为闪烁:

var i = 0, howManyTimes = 7;
function flashingDiv() {
    $('.flash').toggleClass("noFlash")
    i++;
    if( i <= howManyTimes ){
        setTimeout( f, 200 );
    }
}
f();

一个脉冲效果(离线)JQuery插件适合你正在寻找什么?

您可以添加一个持续时间,以便及时限制脉冲效果。


正如J-P在评论中提到的,现在有他更新的脉冲插件。 见他的GitHub回购。这是一个演示。

你可以使用这个插件(把它放在js文件中,并通过script-tag使用它)

http://plugins.jquery.com/project/color

然后用这样的方法:

jQuery.fn.flash = function( color, duration )
{

    var current = this.css( 'color' );

    this.animate( { color: 'rgb(' + color + ')' }, duration / 2 );
    this.animate( { color: current }, duration / 2 );

}

这将为所有jQuery对象添加一个'flash'方法:

$( '#importantElement' ).flash( '255,0,0', 1000 );