我是全新的jQuery和有一些使用原型的经验。在Prototype中,有一个方法来“闪现”一个元素。简单地用另一种颜色突出它,然后让它变回正常,这样用户的眼睛就会被吸引到它上面。jQuery中有这样的方法吗?我看到淡出,淡出,和动画,但我没有看到任何像“闪光”。也许这三个中的一个可以与适当的输入一起使用?
当前回答
我正在寻找这个问题的解决方案,但不依赖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.
其他回答
如果包含一个库是多余的,这里有一个解决方案,是保证工作。
$('div').click(function() {
$(this).css('background-color','#FFFFCC');
setTimeout(function() { $(this).fadeOut('slow').fadeIn('slow'); } , 1000);
setTimeout(function() { $(this).css('background-color','#FFFFFF'); } , 1000);
});
设置事件触发器 设置块元素的背景色 在setTimeout中使用淡出和淡出来创建一个小动画效果。 内秒setTimeout重置默认背景色 在一些浏览器中进行了测试,效果很好。
你可以使用jQuery Color插件。
例如,为了吸引人们对你页面上所有div的注意,你可以使用以下代码:
$("div").stop().css("background-color", "#FFFF9C")
.animate({ backgroundColor: "#FFFFFF"}, 1500);
编辑-新的和改进
下面使用与上面相同的技术,但它有额外的好处:
参数化的高亮颜色和持续时间 保留原来的背景色,而不是假设它是白色 是jQuery的扩展,所以你可以在任何对象上使用它
扩展jQuery对象:
var notLocked = true;
$.fn.animateHighlight = function(highlightColor, duration) {
var highlightBg = highlightColor || "#FFFF9C";
var animateMs = duration || 1500;
var originalBg = this.css("backgroundColor");
if (notLocked) {
notLocked = false;
this.stop().css("background-color", highlightBg)
.animate({backgroundColor: originalBg}, animateMs);
setTimeout( function() { notLocked = true; }, animateMs);
}
};
使用的例子:
$("div").animateHighlight("#dd0000", 1000);
直接jquery,没有插件。它会闪烁指定的次数,在闪烁时改变背景颜色,然后再将其改变回来。
function blink(target, count, blinkspeed, bc) {
let promises=[];
const b=target.css(`background-color`);
target.css(`background-color`, bc||b);
for (i=1; i<count; i++) {
const blink = target.fadeTo(blinkspeed||100, .3).fadeTo(blinkspeed||100, 1.0);
promises.push(blink);
}
// wait for all the blinking to finish before changing the background color back
$.when.apply(null, promises).done(function() {
target.css(`background-color`, b);
});
promises=undefined;
}
例子:
blink($(`.alert-danger`), 5, 200, `yellow`);
这将使一个元素的背景颜色跳动,直到鼠标悬停事件被触发
$.fn.pulseNotify = function(color, duration) {
var This = $(this);
console.log(This);
var pulseColor = color || "#337";
var pulseTime = duration || 3000;
var origBg = This.css("background-color");
var stop = false;
This.bind('mouseover.flashPulse', function() {
stop = true;
This.stop();
This.unbind('mouseover.flashPulse');
This.css('background-color', origBg);
})
function loop() {
console.log(This);
if( !stop ) {
This.animate({backgroundColor: pulseColor}, pulseTime/3, function(){
This.animate({backgroundColor: origBg}, (pulseTime/3)*2, 'easeInCirc', loop);
});
}
}
loop();
return This;
}
这是足够通用的,你可以编写任何你想要动画的代码。您甚至可以将延迟从300ms减少到33ms,并退色等。
// Flash linked to hash.
var hash = location.hash.substr(1);
if (hash) {
hash = $("#" + hash);
var color = hash.css("color"), count = 1;
function hashFade () {
if (++count < 7) setTimeout(hashFade, 300);
hash.css("color", count % 2 ? color : "red");
}
hashFade();
}
推荐文章
- JSHint和jQuery: '$'没有定义
- 用jQuery检查Internet连接是否存在?
- 如何使用滑动(或显示)函数在一个表行?
- 如何用JavaScript截屏一个div ?
- 如何检测“搜索”HTML5输入的清除?
- 当元素从DOM中移除时触发事件
- Ajax成功事件不工作
- 引导下拉与Hover
- jQuery的.bind() vs. on()
- jQuery .live() vs .on()方法,用于在加载动态html后添加单击事件
- 事件。returnValue已弃用。请使用标准的event.preventDefault()
- 如何处理模式关闭事件在Twitter引导?
- 防止浏览器加载拖放文件
- 清除输入文本内的图标
- jQuery为ajax请求返回“parsererror”