如何在jQuery和SASS中制作一个轻轻闪烁的按钮

How to make a gently flashing button in jQuery and SASS?

本文关键字:一个 轻轻 按钮 闪烁 jQuery SASS      更新时间:2023-09-26

在我的Rails应用程序中,我有时不得不使某些按钮闪烁/脉动。

这些按钮的颜色各不相同。有些是红色的,有些是绿色的,有些又是蓝色的。

这是我的jQuery:

setInterval(function(){
  $('a.flashing').toggleClass('blink');
}, 1000);

这是我的CSS(实际上是SASS):

.blink {
  background-color: red;
}

这行得通。

但是,我不希望所有按钮都闪烁为红色。

相反,为了使这种效果对眼睛更温和一些,每个按钮都应该以其原始颜色的略深阴影闪烁(如您所知可能会有所不同)。

如何用尽可能少的代码来实现这一点,理想情况下根本没有jQuery插件?

感谢您的任何帮助。

这是一个

创建脉动效果的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设置为"无限"以获得无尽的脉动。

JSFiddle with the compile CSS: 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);
}

尝试添加 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;
    }

尝试使用 jQuery 对背景颜色进行动画处理

.HTML:

<div id="div1" class="normal" >some text</div>

JavaScript

setTimeout(function(){
    $("#div1").animate({backgroundColor: '#dd6666'}, 500);
}, 1000);

演示