0

我有一个 CSS3 Gradient 按钮,我试图在我的按钮内显示一个背景图像(确切地说是小径向发光)作为悬停状态。由于我的按钮正常状态已经占用了“背景”标签,所以当我在悬停中链接背景图像时,当我悬停在它上面时 CSS3 渐变效果消失了。我尝试使用 background-image 标签进行悬停,但它不起作用。

有没有一种方法可以在悬停时在正常状态之上显示悬停背景图像?

CSS:

    .submit {
    background: rgb(254,219,130); /* Old browsers */
    background: -moz-linear-gradient(top,  rgba(254,219,130,1) 0%, rgba(255,183,50,1) 24%, rgba(255,164,10,1) 100%); /* FF3.6+ */
    background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(254,219,130,1)), color-stop(24%,rgba(255,183,50,1)), color-stop(100%,rgba(255,164,10,1))); /* Chrome,Safari4+ */
    background: -webkit-linear-gradient(top,  rgba(254,219,130,1) 0%,rgba(255,183,50,1) 24%,rgba(255,164,10,1) 100%); /* Chrome10+,Safari5.1+ */
    background: -o-linear-gradient(top,  rgba(254,219,130,1) 0%,rgba(255,183,50,1) 24%,rgba(255,164,10,1) 100%); /* Opera 11.10+ */
    background: -ms-linear-gradient(top,  rgba(254,219,130,1) 0%,rgba(255,183,50,1) 24%,rgba(255,164,10,1) 100%); /* IE10+ */
    background: linear-gradient(to bottom,  rgba(254,219,130,1) 0%,rgba(255,183,50,1) 24%,rgba(255,164,10,1) 100%); /* W3C */
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#fedb82', endColorstr='#ffa40a',GradientType=0 ); /* IE6-9 */
}

.submit:hover {
    background-image: url(images/glow.png) center no-repeat;
}

HTML:

<button class="submit">Button</button>
4

1 回答 1

0

input元素不同,该button元素可以包含其他元素,因此最简单的方法是简单地将 a 嵌套spanbutton该元素中并将“发光”应用于该元素:

<button class="submit"><span>Button</span></button>​

.submit:hover span {
    background: url(data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiA/Pgo8c3ZnIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgd2lkdGg9IjEwMCUiIGhlaWdodD0iMTAwJSIgdmlld0JveD0iMCAwIDEgMSIgcHJlc2VydmVBc3BlY3RSYXRpbz0ibm9uZSI+CiAgPHJhZGlhbEdyYWRpZW50IGlkPSJncmFkLXVjZ2ctZ2VuZXJhdGVkIiBncmFkaWVudFVuaXRzPSJ1c2VyU3BhY2VPblVzZSIgY3g9IjUwJSIgY3k9IjUwJSIgcj0iNzUlIj4KICAgIDxzdG9wIG9mZnNldD0iMCUiIHN0b3AtY29sb3I9IiNmZmZmZmYiIHN0b3Atb3BhY2l0eT0iMSIvPgogICAgPHN0b3Agb2Zmc2V0PSIxMDAlIiBzdG9wLWNvbG9yPSIjZmZmZmZmIiBzdG9wLW9wYWNpdHk9IjAiLz4KICA8L3JhZGlhbEdyYWRpZW50PgogIDxyZWN0IHg9Ii01MCIgeT0iLTUwIiB3aWR0aD0iMTAxIiBoZWlnaHQ9IjEwMSIgZmlsbD0idXJsKCNncmFkLXVjZ2ctZ2VuZXJhdGVkKSIgLz4KPC9zdmc+);
    background: -moz-radial-gradient(center, ellipse cover, rgba(255,255,255,1) 0%, rgba(255,255,255,0) 100%);
    background: -webkit-gradient(radial, center center, 0px, center center, 100%, color-stop(0%,rgba(255,255,255,1)), color-stop(100%,rgba(255,255,255,0)));
    background: -webkit-radial-gradient(center, ellipse cover, rgba(255,255,255,1) 0%,rgba(255,255,255,0) 100%);
    background: -o-radial-gradient(center, ellipse cover, rgba(255,255,255,1) 0%,rgba(255,255,255,0) 100%);
    background: -ms-radial-gradient(center, ellipse cover, rgba(255,255,255,1) 0%,rgba(255,255,255,0) 100%);
    background: radial-gradient(ellipse at center, rgba(255,255,255,1) 0%,rgba(255,255,255,0) 100%);
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#ffffff', endColorstr='#00ffffff',GradientType=1 );
}​

JS 小提琴演示

我无法访问您自己的图像,因此我使用了 CSS 渐变图像生成器作为替代。显然,将您自己的相关图像替换回去。


使用第二个background-image

随着 CSS3 的引入,一个元素可以使用逗号分隔的列表(例如,font-face声明)有多个背景,这允许以下操作:

.submit:hover {
    /* IE9 SVG, needs conditional override of 'filter' to 'none' */
    background: url(data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiA/Pgo8c3ZnIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgd2lkdGg9IjEwMCUiIGhlaWdodD0iMTAwJSIgdmlld0JveD0iMCAwIDEgMSIgcHJlc2VydmVBc3BlY3RSYXRpbz0ibm9uZSI+CiAgPHJhZGlhbEdyYWRpZW50IGlkPSJncmFkLXVjZ2ctZ2VuZXJhdGVkIiBncmFkaWVudFVuaXRzPSJ1c2VyU3BhY2VPblVzZSIgY3g9IjUwJSIgY3k9IjUwJSIgcj0iNzUlIj4KICAgIDxzdG9wIG9mZnNldD0iMCUiIHN0b3AtY29sb3I9IiNmZmZmZmYiIHN0b3Atb3BhY2l0eT0iMSIvPgogICAgPHN0b3Agb2Zmc2V0PSIxMDAlIiBzdG9wLWNvbG9yPSIjZmZmZmZmIiBzdG9wLW9wYWNpdHk9IjAiLz4KICA8L3JhZGlhbEdyYWRpZW50PgogIDxyZWN0IHg9Ii01MCIgeT0iLTUwIiB3aWR0aD0iMTAxIiBoZWlnaHQ9IjEwMSIgZmlsbD0idXJsKCNncmFkLXVjZ2ctZ2VuZXJhdGVkKSIgLz4KPC9zdmc+);
background: -moz-radial-gradient(center, ellipse cover, rgba(255,255,255,1) 0%, rgba(255,255,255,0) 100%), -moz-linear-gradient(top,  rgba(254,219,130,1) 0%, rgba(255,183,50,1) 24%, rgba(255,164,10,1) 100%); /* FF3.6+ */
    background: -webkit-gradient(radial, center center, 0px, center center, 100%, color-stop(0%,rgba(255,255,255,1)), color-stop(100%,rgba(255,255,255,0))), -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(254,219,130,1)), color-stop(24%,rgba(255,183,50,1)), color-stop(100%,rgba(255,164,10,1))); /* Chrome,Safari4+ */
    background: -webkit-radial-gradient(center, ellipse cover, rgba(255,255,255,1) 0%,rgba(255,255,255,0) 100%), -webkit-linear-gradient(top,  rgba(254,219,130,1) 0%,rgba(255,183,50,1) 24%,rgba(255,164,10,1) 100%); /* Chrome10+,Safari5.1+ */
    background: -o-radial-gradient(center, ellipse cover, rgba(255,255,255,1) 0%,rgba(255,255,255,0) 100%), -o-linear-gradient(top,  rgba(254,219,130,1) 0%,rgba(255,183,50,1) 24%,rgba(255,164,10,1) 100%); /* Opera 12+ */
    background: -ms-radial-gradient(center, ellipse cover, rgba(255,255,255,1) 0%,rgba(255,255,255,0) 100%), -ms-linear-gradient(top,  rgba(254,219,130,1) 0%,rgba(255,183,50,1) 24%,rgba(255,164,10,1) 100%); /* IE10+ */
    background: radial-gradient(ellipse at center, rgba(255,255,255,1) 0%,rgba(255,255,255,0) 100%), linear-gradient(to bottom,  rgba(254,219,130,1) 0%,rgba(255,183,50,1) 24%,rgba(255,164,10,1) 100%); /* W3C */
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#ffffff', endColorstr='#00ffffff',GradientType=1 ); /* IE6-8 fallback on horizontal gradient */
}​

JS 小提琴演示

我没有在filter属性下定义第二个背景,因为老实说我不知道​​微软的旧浏览器是否会理解第二个值,或者干脆放弃整个规则。

于 2012-11-17T22:45:02.270 回答