首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在jQuery和SASS中制作一个轻柔的闪烁按钮?

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

Stack Overflow用户
提问于 2013-10-23 16:44:21
回答 3查看 5.8K关注 0票数 3

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

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

这是我的jQuery:

代码语言:javascript
复制
setInterval(function(){
  $('a.flashing').toggleClass('blink');
}, 1000);

这是我的CSS (实际上):

代码语言:javascript
复制
.blink {
  background-color: red;
}

这个很管用。

然而,我不希望所有的按钮在红色闪现。

相反,为了使这种效果在眼睛上更柔和一点,每个按钮应该闪烁在一个稍深的颜色,它的原始颜色(正如你所知道的可能有所不同)。

如何在尽可能少的代码和理想情况下根本不使用jQuery插件来实现这一点?

谢谢你的帮助。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-10-23 17:05:44

这是一个SASS (+ Compass)函数,它创建了一个脉动效应。脉动的次数可以通过$count指定。

代码语言:javascript
复制
=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/ ;-):

代码语言:javascript
复制
@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);
}
票数 3
EN

Stack Overflow用户

发布于 2013-10-23 16:56:27

尝试添加一个CSS3转换效果,然后您不需要跳过和JS箍。.normal是默认颜色。

代码语言:javascript
复制
 .normal {
      background-color:rgba(250,20,10,1);
      transition: all .5s ease;
    }

.blink {
      background-color:rgba(250,20,10,.2);
      transition: all .5s ease;
    }
票数 2
EN

Stack Overflow用户

发布于 2013-10-23 17:09:29

尝试使用jQuery动画背景颜色

HTML:

代码语言:javascript
复制
<div id="div1" class="normal" >some text</div>

JavaScript

代码语言:javascript
复制
setTimeout(function(){
    $("#div1").animate({backgroundColor: '#dd6666'}, 500);
}, 1000);

演示

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19547557

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档