背景颜色“动画”使用 jQuery
我正在尝试创建一个简单的动画,其中背景颜色滑动到位。到目前为止,它是成功的,但有一个主要问题:块上的文本移动了,我不知道如何阻止它。
我可以将文本放置在上面,但我还需要更改颜色,如果我在 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/
I'm trying to create a simple animation in which the background-color slides into place. So far it's successful, but there's one major issue: the text on the block moves and I can't figure out how to stop it.
I could position the text above, but I need to change the color as well and if I do that in jQuery it creates a mess.
The 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
}
Then the 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');
});
It creates a great animation. But the text, it slides with it. How can I prevent this without glitchy colour changes?
Here is the jsfiddle: http://jsfiddle.net/WERFJ/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有两件事:
http://jsfiddle.net/WERFJ/13/ 它可能需要稍微调整一下,但这就是你想要的效果。基本上我只是从 h1 中取出文本并在其上放置 z 索引,使其始终位于前面,然后使用
.button:hover span { color: #000 }
使用 h1 来这样做不是 h1 的正确使用。我只想使用常规 div。
Two things:
http://jsfiddle.net/WERFJ/13/ It might need a little tweaking but that's the effect you were after. Basically I just took the text out of the h1's and put a z-index on it so it's always in front, then used
.button:hover span { color: #000 }
Using h1's to do this isn't a proper use of h1. I would just use regular div's.
您可以将文本放在它的唯一层上:
http://jsfiddle.net/WERFJ/14/
也许还有完整的文本效果:
http://jsfiddle.net/WERFJ/17/
You could put the text on it's only layer:
http://jsfiddle.net/WERFJ/14/
And maybe with a text effect on complete:
http://jsfiddle.net/WERFJ/17/