如何在jQuery和SASS中制作一个轻轻闪烁的按钮
How to make a gently flashing button in jQuery and SASS?
在我的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);
演示
相关文章:
- 创建一个类似链接的按钮,并通过Javascript函数打开一个新的弹出窗口
- jQuery:循环一个具有不同超时值的循环
- 当包含另一个asp文件时,是否也包含所有引用的样式和脚本页面
- 从javascript创建一个列表
- 节点导出返回一个空对象
- 使用clickToggle并在单击另一个元素时关闭元素
- 我可以在json对象中添加一个函数吗
- 使用javascript将动态表从一个html页面打印到另一个html页
- 将jsp文件下拉列表中的选定项分配给一个java变量(比如String selection)
- 表追加而不附加最后一个元素
- 我如何找到一个句子中的所有空格并替换忽略它们
- D3在一个调用中绘制不同的SVG形状,没有可见性
- 如何在android中使用phonegap将文件从一个文件夹移动/复制到另一个文件夹
- 使用类从一个标记中双击事件
- Javascript,访问一个主要对象模块模式中的每个对象
- 如果使用 lodash 将属性存在于另一个对象中,则向对象添加属性
- 如何在elfinder插件(一个文件管理器插件)上获得上传前事件
- 我应该如何从xml文件构建一个javascript页面
- Regex代码只允许一个空格
- 如何在jQuery和SASS中制作一个轻轻闪烁的按钮