我正在尝试创建一个简单的动画,其中背景色滑入到位。到目前为止,它是成功的,但是有一个主要问题:街区上的文字移动了,我不知道该如何停止。
我可以在上面放置文本,但是我也需要更改颜色,如果我在jQuery中这样做,则会造成混乱。
CSS:
.button {
display: block;
width: 130px;
height: 40px;
cursor: pointer;
position: fixed;
z-index: 1;
top: 15px;
left: 15px;
text-align: center;
color: #FFF;
overflow: hidden;
text-decoration: none;
font-size: 23px;
text-transform: lowercase;
font-family: Helvetica, Arial, sans-serif;
background: #FFF
} .button:hover { color: #FFF }
.button h1 {
display: inline-block;
position: absolute;
top: 0;
left: 0;
width: 130px;
height: 40px;
font-size: 23px;
margin: 0;
background-color: blue
}
.button h1.back {
z-index: 999;
color: #000;
margin-left: -130px;
overflow: hidden;
background-color: red
}
.button h1 span {
position: absolute;
left: 0;
top: 10%;
width: 130px;
height: 40px;
text-align: center
}
然后是jQuery:
$('.button').hover(function() {
var el = $(this);
el.children('.back').animate({
marginLeft: '0'
}, 30, 'swing');
}, function() {
var el = $(this);
el.children('.back').animate({
marginLeft: '-130px'
}, 30, 'swing');
});
它创造了一个很棒的动画。但是文本随它而滑动。我该如何防止这种情况而不会出现故障的颜色变化?
这是jsfiddle:http://jsfiddle.net/WERFJ/
最佳答案
两件事情:
http://jsfiddle.net/WERFJ/13/可能需要进行一些微调,但这正是您所追求的。基本上,我只是将文本从h1中取出,并在其上放了一个z-index,因此它始终位于前面,然后使用.button:hover span { color: #000 }
使用h1执行此操作不是正确使用h1。我只会使用常规div。
关于jquery - jQuery的背景色“动画”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7720541/