在我的Rails应用程序中,我有时不得不使某些按钮闪烁/脉动。
这些按钮颜色各不相同。有些是红色的,另一些是绿色的,还有一些是蓝色的。
这是我的jQuery:
setInterval(function(){
$('a.flashing').toggleClass('blink');
}, 1000);这是我的CSS (实际上):
.blink {
background-color: red;
}这个很管用。
然而,我不希望所有的按钮在红色闪现。
相反,为了使这种效果在眼睛上更柔和一点,每个按钮应该闪烁在一个稍深的颜色,它的原始颜色(正如你所知道的可能有所不同)。
如何在尽可能少的代码和理想情况下根本不使用jQuery插件来实现这一点?
谢谢你的帮助。
发布于 2013-10-23 17:05:44
这是一个SASS (+ Compass)函数,它创建了一个脉动效应。脉动的次数可以通过$count指定。
=keyframes($name)
@-webkit-keyframes #{$name}
@content
@-moz-keyframes #{$name}
@content
@-ms-keyframes #{$name}
@content
@keyframes #{$name}
@content
=animation-name($name)
-webkit-animation-name: $name
-moz-animation-name: $name
-o-animation-name: $name
animation-name: $name
=animation-duration($duration)
-webkit-animation-duration: $duration
-moz-animation-duration: $duration
-o-animation-duration: $duration
animation-duration: $duration
=animation-timing-function($timing-function)
-webkit-animation-timing-function: $timing-function
-moz-animation-timing-function: $timing-function
-o-animation-timing-function: $timing-function
animation-timing-function: $timing-function
=animation-iteration-count($iteration-count)
-webkit-animation-iteration-count: $iteration-count
-moz-animation-iteration-count: $iteration-count
-o-animation-iteration-count: $iteration-count
animation-iteration-count: $iteration-count
=animation-direction($direction)
-webkit-animation-direction: $direction
-moz-animation-direction: $direction
-o-animation-direction: $direction
animation-direction: $direction
// define keyframes
+keyframes(change_background_color)
to
background-color: $some_color
// define the mixin
=pulsate($time:0.2s, $count:8)
+animation-name(change_background_color)
+animation-duration($time)
+animation-iteration-count($count)
+animation-direction(alternate)
+animation-timing-function(ease-in-out)
// use the mixin in a class
.pulsate-8times
+pulsate(1s, 16)不需要JS (除了切换类)。将$count设置为“无限”,以实现无休止的脉动。
具有编译的CSS: JSFiddle:http://jsfiddle.net/3L2yA/的
更新: SCSS中也一样(感谢http://sasstoscss.com/ ;-):
@mixin keyframes($name) {
@-webkit-keyframes #{$name} {
@content;
}
@-moz-keyframes #{$name} {
@content;
}
@-ms-keyframes #{$name} {
@content;
}
@keyframes #{$name} {
@content;
}
}
@mixin animation-name($name) {
-webkit-animation-name: $name;
-moz-animation-name: $name;
-o-animation-name: $name;
animation-name: $name;
}
@mixin animation-duration($duration) {
-webkit-animation-duration: $duration;
-moz-animation-duration: $duration;
-o-animation-duration: $duration;
animation-duration: $duration;
}
@mixin animation-timing-function($timing-function) {
-webkit-animation-timing-function: $timing-function;
-moz-animation-timing-function: $timing-function;
-o-animation-timing-function: $timing-function;
animation-timing-function: $timing-function;
}
@mixin animation-iteration-count($iteration-count) {
-webkit-animation-iteration-count: $iteration-count;
-moz-animation-iteration-count: $iteration-count;
-o-animation-iteration-count: $iteration-count;
animation-iteration-count: $iteration-count;
}
@mixin animation-direction($direction) {
-webkit-animation-direction: $direction;
-moz-animation-direction: $direction;
-o-animation-direction: $direction;
animation-direction: $direction;
}
@include keyframes(change_background_color) {
to {
background-color: $some_color;
}
}
@mixin pulsate($time: 0.2s, $count: 8) {
@include animation-name(change_background_color);
@include animation-duration($time);
@include animation-iteration-count($count);
@include animation-direction(alternate);
@include animation-timing-function(ease-in-out);
}
.pulsate-8times {
@include pulsate(1s, 16);
}发布于 2013-10-23 16:56:27
尝试添加一个CSS3转换效果,然后您不需要跳过和JS箍。.normal是默认颜色。
.normal {
background-color:rgba(250,20,10,1);
transition: all .5s ease;
}
.blink {
background-color:rgba(250,20,10,.2);
transition: all .5s ease;
}发布于 2013-10-23 17:09:29
尝试使用jQuery动画背景颜色
HTML:
<div id="div1" class="normal" >some text</div>JavaScript
setTimeout(function(){
$("#div1").animate({backgroundColor: '#dd6666'}, 500);
}, 1000);https://stackoverflow.com/questions/19547557
复制相似问题