没有Javascript(只有CSS)可以做同样的事情(悬停效果)吗?
我正在尝试创建一个带有图标的按钮像这样:
I'm trying to create a button with icon like this:
HTML:
<div id='button'>
<div class='icon'></div>
<span>Send Email</span>
</div>
CSS:
#button {
width: 270px;
height: 50px;
font-size: 1.4em;
color: #333;
background-color: #555;
position: relative;
}
#button > .icon {
width: 61px;
height: 39px;
background-image: url('http://i55.tinypic.com/2agsutj.png');
background-repeat: no-repeat;
background-position: 0px 0px;
position: absolute;
left: 40px;
top: 5px;
}
#button > span {
position: absolute;
left: 130px;
top: 10px;
}
#button:hover {
color: #fff;
cursor: pointer;
}
JS:
$(function() {
$('#button').hover(function() {
$('#button > .icon').css('background-position', '0px -39px');
}, function() {
$('#button > .icon').css('background-position', '0px 0px');
});
});
我的问题是:这种简单的功能真的需要 Javascript,还是只能使用 CSS 来完成?
My question is: Is Javascript really necessary for such simple functionality, or it can be done using CSS only ?
推荐答案
有一个 :hover
伪类.
#button:hover > .icon {
background-position: 0px -39px;
}
http://www.w3.org/TR/CSS2/selector.html#dynamic-pseudo-classes
http://www.w3schools.com/css/pr_pseudo_hover.asp
相关文章