【问题标题】:Fade background color of button on hover css在悬停css上淡出按钮的背景颜色
【发布时间】:2015-03-29 01:05:05
【问题描述】:

我正在尝试将按钮的背景颜色从蓝色渐变为白色,并将字体颜色从白色渐变为蓝色。我检查了其他帖子。

这是我的小提琴: http://jsfiddle.net/t533wbuy/1/

我用这个小提琴作为参考: http://jsfiddle.net/visualdecree/SvjHx/

我的问题是:

  • 我希望它是一个 A 按钮,而不是使用 ul > li。
  • 字体颜色在悬停时不会变成白色。

CSS

.menuitem{
width: 200px;
height:30px;
background-color: #005abb;
font-size:16px;font-family: 'gotham_htfbold';-webkit-border-radius: 15px;
-moz-border-radius: 15px;
border-radius: 15px;
}

.menuitem a{
width: 200px;
height: 30px;
height: auto;
display: block;
position: relative;
color: #FFF;
text-align: center;
z-index: 9999;

}

.menu-item-span{
display: none;
width: 200px;
height: 30px;
background-color: #fff;
bottom: 0;
left: 0;
position: absolute;
right: 0;
top: 0;
color:#005abb;
z-index: -999;
-webkit-border-radius: 15px;
-moz-border-radius: 15px;
 border-radius: 15px;
 }

html


    点击这里申请

jquery

$(function() {
$(".menuitem").find("class")
.hide() // Finds span hide.
.end() // After find stop.
.hover(function() { // On hover find and fadein then fadeout
$(this).find("span").stop(true, true).fadeIn();
 }, function() {
$(this).find("span").stop(true, true).fadeOut();
});
});

【问题讨论】:

  • 你可以只用 CSS 来做到这一点

标签: jquery html css


【解决方案1】:

正如@Aaron 和@user1447679 提到的,这种情况下的最佳实践是使用纯css,但如果你说你真的想用css 做,你可以使用mouseentermouseleave 事件来改变@元素的987654324@和background-color用jQuery的.css()方法:

$(function() {
    $("#lnk-1").on('mouseover',function() { 
        $(this).css('background-color','black').css('color','white'); 
    })
    .on('mouseleave',function(){
        $(this).css('background-color','blue').css('color','black'); 
    });
});

这是一个简单的演示:http://jsfiddle.net/t533wbuy/4/

【讨论】:

    【解决方案2】:

    为什么不使用纯 CSS?

    a {
      transition: all .4s ease;
      padding: 12px 28px;
      border-radius: 18px;
    }
    a {
      background-color: blue;
      color: #fff;
    }
    a:hover {
      background-color: grey;
      color: #000;
    }
    <a href="#">BUTTON</a>

    【讨论】:

    • 哈。你打败了我!
    • 你得快点@user1447679 :o)
    • 天哪,这很棒,但有没有办法为此添加自定义类。我不确定如何使用代码中的双 a 标签来做到这一点。我原来有 a.btnsubmit:link
    • 我只是用嵌套代替.blahclass a。标记为答案!
    猜你喜欢
    • 2011-10-23
    • 1970-01-01
    • 2019-06-23
    • 2013-06-26
    • 1970-01-01
    • 1970-01-01
    • 2014-07-24
    • 2014-09-09
    • 2022-12-03
    相关资源
    最近更新 更多