【问题标题】:Possible to manipulate background on hover when background is using CSS gradient?当背景使用 CSS 渐变时,可以在悬停时操纵背景吗?
【发布时间】:2013-02-10 03:40:19
【问题描述】:

我有这个具有内联样式的按钮。它是内联的,因为它是用户选择的,所以不幸的是没有办法将一个类链接到这个。

所以我有以下 CSS 内联:

background: #1e5799; /* Old browsers */
background: -moz-linear-gradient(top,  #1e5799 0%, #7db9e8 100%); /* FF3.6+ */
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#1e5799), color-stop(100%,#7db9e8)); /* Chrome,Safari4+ */
background: -webkit-linear-gradient(top,  #1e5799 0%,#7db9e8 100%); /* Chrome10+,Safari5.1+ */
background: -o-linear-gradient(top,  #1e5799 0%,#7db9e8 100%); /* Opera 11.10+ */
background: -ms-linear-gradient(top,  #1e5799 0%,#7db9e8 100%); /* IE10+ */
background: linear-gradient(to bottom,  #1e5799 0%,#7db9e8 100%); /* W3C */
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#1e5799', endColorstr='#7db9e8',GradientType=0 ); /* IE6-9 */

所以我的问题是,如何在我想以某种方式简单地反转背景的地方应用悬停颜色。但请记住,我事先不知道颜色,所以我不能真正在悬停时应用任何颜色。但如果我只是简单地反转背景,这将模拟悬停效果。

此外,按钮可以是可变大小的。

这个可以吗?

这是一个小提琴http://jsfiddle.net/hLrvA/

我已经尝试过了,它有点工作,但不是真的,因为它没有填充按钮。

a:hover { background-position:0 -15px !important; }

我们将不胜感激任何有关如何实现这一目标的见解。

【问题讨论】:

    标签: hover gradient css


    【解决方案1】:

    在悬停状态下反转背景颜色的唯一方法是交换每个背景属性中的十六进制值。

    但是,您可以应用悬停状态,该状态使用基于默认状态的渐变中定义的颜色的渐变。

    Check it out on jsFiddle.

    HTML:

    <a href="#">Button</a>
    

    CSS:

    a {
        background: #1e5799; /* Old browsers */
        background: -webkit-linear-gradient(#1e5799, #7db9e8);
        background: -moz-linear-gradient(#1e5799, #7db9e8);
        background: -o-linear-gradient(#1e5799, #7db9e8);
        background: linear-gradient(#1e5799, #7db9e8);
        filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#1e5799', endColorstr='#7db9e8',GradientType=0 );
        background-size:1px 200px;
        border-radius: 8px;
        color: #fff;
        cursor:pointer; 
        display: block;
        font-size: 1.4em;
        height: 100px;
        line-height: 100px;
        text-decoration: none;
        text-align: center;
        width: 200px;
    
        -webkit-transition: background 1s ease-out;
           -moz-transition: background 1s ease-out;
             -o-transition: background 1s ease-out;
                transition: background 1s ease-out;
    }
    
    a:hover {
        background-position:100px;
    }
    

    【讨论】:

    • 不,即使它不是图像,它也可以工作...即使我将其更改为背景图像,它仍然是相同的效果...但是 Twitter 引导程序有这个,他们可以制作它出于某种原因工作-> twitter.github.com/bootstrap/base-css.html#buttons
    • 如果我必须反转十六进制,我知道该怎么做,但没有办法,因为我事先不知道十六进制,因此我提到了内联样式......如果你看我发布的引导链接,你可以看到他们可以在不触及十六进制的情况下实现它......现在我尝试遵循他们的风格,它“有点”工作......它确实改变了背景位置,但只是部分而不是整个按钮。 ..所以它一定是我想念的东西......
    • 让我贴一个小提琴,让你明白我的意思
    • 看起来他们在悬停时将垂直渐变的位置调整了 15 个像素。
    • 好吧,如果你不设置宽度和高度,并且行高为 20px...你还能做这个工作吗?原因是用户通过字体大小而不是设置的宽度/高度来定义按钮的大小......它仍然可能吗?
    【解决方案2】:

    我在尝试更改 :hover 上的背景颜色属性时遇到了类似的情况

    HTML

    <div class="demo"></div>
    

    CSS

    .demo{
        width: 50px;
        height: 150px;
        background: #388cb6;
        background: -moz-linear-gradient(top, #41a2c5 0%, #388cb6 50%, #327cab 100%);
        background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#41a2c5), color-stop(50%,#388cb6), color-stop(100%,#327cab));
        background: -webkit-linear-gradient(top, #41a2c5 0%,#388cb6 50%,#327cab 100%);
        background: -o-linear-gradient(top, #41a2c5 0%,#388cb6 50%,#327cab 100%);
        background: -ms-linear-gradient(top, #41a2c5 0%,#388cb6 50%,#327cab 100%);
        background: linear-gradient(to bottom, #41a2c5 0%,#388cb6 50%,#327cab 100%);
        filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#41a2c5', endColorstr='#327cab',GradientType=0 );
    
    }
    .demo:hover{
        background-color: red;
    }
    

    上述方法不起作用,但是如果您直接定位背景而不是更具体的背景颜色,它将起作用:

    .demo:hover{
        background: red;
    }
    

    似乎大多数浏览器更喜欢单个内联背景声明而不是单个属性。

    注意:我用了一个伪类来说明它不限于锚链接。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-18
      • 1970-01-01
      • 2021-02-23
      • 1970-01-01
      • 2016-10-25
      • 1970-01-01
      相关资源
      最近更新 更多