我如何用多个类使用Jquery动画按钮的背景颜色
How do I animate the background color of button with multiple classes using Jquery?
我正在使用Foundation创建一个新网页。在这个页面上,我有这个按钮:
<a href="#bottomOfPage" class="small round button" id="viewOurWorkButton">View Our Work</a>
我想通过简单地改变它的背景色和字体颜色来动画这个按钮。问题是,当我尝试使用id #viewOurWorkButton访问按钮的属性时,什么都没有发生。下面是我想用jquery做的:
$( document ).ready(function() {
buttonAnimation();
});
function buttonAnimation() {
$('#viewOurWorkButton').animate({
$('#viewOurWorkButton').css({backgroundColor:'#fff', color:'#000'})
}, 1000, function(){
$('#viewOurWorkButton').animate({
$('#viewOurWorkButton').css({backgroundColor:'#000', color:'#FFF'})
}, 1000, buttonAnimation);
});
}
这也在控制台中显示了这个错误:
Uncaught SyntaxError: Unexpected string
我也试着做。button的动画,但仍然没有效果。下面是.button类的样子:
button, .button {
-webkit-appearance: none;
-moz-appearance: none;
border-radius: 0;
border-style: solid;
border-width: 0;
cursor: pointer;
font-family: "Helvetica Neue", Helvetica, Roboto, Arial, sans-serif;
font-weight: normal;
line-height: normal;
margin: 0 0 1.25rem;
position: relative;
text-align: center;
text-decoration: none;
display: inline-block;
padding: 1rem 2rem 1.0625rem 2rem;
font-size: 1rem;
background-color: #008CBA;
border-color: #007095;
color: #FFFFFF;
transition: background-color 300ms ease-out;
}
如何动画。button属性?
您不需要重复selector
到animate
,您只需要列出properties
。animate
也支持链化。
请注意,您可以使用jQuery
来动画数字属性,但要动画颜色,您需要jQuery-ui
。
这似乎可以工作:
$(document).ready(function () {
buttonAnimation();
});
function buttonAnimation() {
$('#viewOurWorkButton').animate({
backgroundColor: '#fff',
color: '#000'
}, 1000).animate({
backgroundColor: '#000',
color: '#FFF'
}, 1000, buttonAnimation);
}
<script type='text/javascript' src='//code.jquery.com/jquery-2.0.2.js'></script>
<script type="text/javascript" src="//code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<a href="#bottomOfPage" class="small round button" id="viewOurWorkButton">View Our Work</a>
你需要首先在你的页面html中包含jquery UI库,然后我像这样修改你的jquery代码:
$( document ).ready(function() {
buttonAnimation();
});
function buttonAnimation() {
$('#viewOurWorkButton')
.animate({ backgroundColor:'#fff', color:'#000' }, 1000 )
.animate({ backgroundColor:'#000', color:'#FFF' }, 1000 );
}
button, .button {
-webkit-appearance: none;
-moz-appearance: none;
border-radius: 0;
border-style: solid;
border-width: 0;
cursor: pointer;
font-family: "Helvetica Neue", Helvetica, Roboto, Arial, sans-serif;
font-weight: normal;
line-height: normal;
margin: 0 0 1.25rem;
position: relative;
text-align: center;
text-decoration: none;
display: inline-block;
padding: 1rem 2rem 1.0625rem 2rem;
font-size: 1rem;
background-color: #008CBA;
border-color: #007095;
color: #FFFFFF;
transition: background-color 300ms ease-out;
}
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<a href="#bottomOfPage" class="small round button" id="viewOurWorkButton">View Our Work</a>
相关文章:
- 使用JavaScript使按钮做出反应(添加动画)
- 在jquerymobile中设置按钮css属性的动画
- bootstrap在类更改时动画按钮样式
- 如何使用Javascript创建带有精灵表的动画按钮
- 如何实现 ladda-bootstrap 按钮加载动画
- 从导航按钮 JavaScript 中删除动画
- 使用动画时Jquery后退按钮不起作用
- scrollTop按钮没有't设置动画
- 用JavaScript制作一个按钮的动画,从按钮到屏幕中心弹出一个模式对话框
- 如果不使用HTML按钮,就无法通过编程方式触发SVG动画
- Jquery将三个按钮动画化以打开关闭开关
- 如何重新创建移动Apple.com菜单按钮动画
- 如何获得内置进度条的工作按钮动画
- 停止最后一张幻灯片上的jQuery按钮动画
- 视频播放暂停按钮动画
- 谷歌I'm感觉幸运按钮动画javascript
- 我如何用javascript创建png按钮动画
- 多个同时按钮动画
- HTML Web中没有材质按钮动画
- 使用AngularJS制作按钮动画ng重复