我是全新的jQuery和有一些使用原型的经验。在Prototype中,有一个方法来“闪现”一个元素。简单地用另一种颜色突出它,然后让它变回正常,这样用户的眼睛就会被吸引到它上面。jQuery中有这样的方法吗?我看到淡出,淡出,和动画,但我没有看到任何像“闪光”。也许这三个中的一个可以与适当的输入一起使用?
当前回答
CSS支持所有主流浏览器的闪烁
.flash {
animation: flash 0.5s ease-out;
animation-iteration-count: 10;
}
@keyframes flash {
0% { opacity: 0.5; }
50% { opacity: 1.0; }
100% { opacity: 0.5; }
}
将这个类添加到要闪光的元素中
$(elem).addClass("flash");
迭代计数:10是你想让它闪烁的次数,不需要删除类,它会自己停止闪烁。
使用不透明度:这适用于任何颜色的元素,只要它不是透明的。
其他回答
创建两个类,给每个类一个背景色:
.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();
$('#district').css({opacity: 0});
$('#district').animate({opacity: 1}, 700 );
这个函数使它闪烁。 它必须使用cssHooks,因为rgb默认返回background-color函数。
希望能有所帮助!
$.cssHooks.backgroundColor = {
get: function(elem) {
if (elem.currentStyle)
var bg = elem.currentStyle["backgroundColor"];
else if (window.getComputedStyle)
var bg = document.defaultView.getComputedStyle(elem,
null).getPropertyValue("background-color");
if (bg.search("rgb") == -1)
return bg;
else {
bg = bg.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
function hex(x) {
return ("0" + parseInt(x).toString(16)).slice(-2);
}
return "#" + hex(bg[1]) + hex(bg[2]) + hex(bg[3]);
}
}
}
function blink(element,blinkTimes,color,originalColor){
var changeToColor;
if(blinkTimes === null || blinkTimes === undefined)
blinkTimes = 1;
if(!originalColor || originalColor === null || originalColor === undefined)
originalColor = $(element).css("backgroundColor");
if(!color || color === null || color === undefined)
color = "#ffffdf";
if($(element).css("backgroundColor") == color){
changeToColor = originalColor;
}else{
changeToColor = color;
--blinkTimes;
}
if(blinkTimes >= 0){
$(element).animate({
"background-color": changeToColor,
}, {
duration: 500,
complete: function() {
blink(element, blinkTimes, color, originalColor);
return true;
}
});
}else{
$(element).removeAttr("style");
}
return true;
}
你可以使用这个插件(把它放在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 );
CSS支持所有主流浏览器的闪烁
.flash {
animation: flash 0.5s ease-out;
animation-iteration-count: 10;
}
@keyframes flash {
0% { opacity: 0.5; }
50% { opacity: 1.0; }
100% { opacity: 0.5; }
}
将这个类添加到要闪光的元素中
$(elem).addClass("flash");
迭代计数:10是你想让它闪烁的次数,不需要删除类,它会自己停止闪烁。
使用不透明度:这适用于任何颜色的元素,只要它不是透明的。
推荐文章
- 可以在setInterval()内部调用clearInterval()吗?
- 无法读取未定义的属性“msie”- jQuery工具
- jQuery:执行同步AJAX请求
- jQuery表格排序
- 如何禁用文本选择使用jQuery?
- 如何停止事件冒泡复选框点击
- Bootstrap抛出Uncaught错误:Bootstrap的JavaScript需要jQuery
- 如何删除和清除所有的本地存储数据
- 如何修改标签文本?
- 如何获得十六进制颜色值而不是RGB值?
- 如何将一个标题转换为jQuery的URL段塞?
- $(window).width()与媒体查询不一样
- AJAX请求中的内容类型和数据类型是什么?
- 如何打破_。在underscore.js中的每个函数
- 如何在jQuery中获得当前日期?