Reputation: 646
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/
Upvotes: 1
Views: 1298
Reputation: 8941
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.
Upvotes: 2
Reputation: 49188
You could put the text on it's only layer:
<a href="#" class="button">
<h1 class="front"><span></span></h1>
<h1 class="back"><span></span></h1>
<h1 class="text"><span>hello</span></h1>
</a>
And maybe with a text effect on complete:
$(function() {
var $text = $('.text');
$('.button').hover(function() {
$(this).children('.back').animate({
marginLeft: '0',
}, 100, 'swing', function(){
$text.css('color','white');
});
}, function() {
$(this).children('.back').animate({
marginLeft: '-130px'
}, 100, 'swing', function(){
$text.css('color','black');
});
});
});
Upvotes: 0