背景色;动画";使用jQuery
我正在尝试创建一个简单的动画,背景色在其中滑动到位。到目前为止,它是成功的,但有一个主要问题:块上的文本移动,我不知道如何阻止它 我可以定位上面的文本,但我也需要更改颜色,如果我在jQuery中这样做,就会造成混乱 CSS:背景色;动画";使用jQuery,jquery,css,jquery-animate,Jquery,Css,Jquery Animate,我正在尝试创建一个简单的动画,背景色在其中滑动到位。到目前为止,它是成功的,但有一个主要问题:块上的文本移动,我不知道如何阻止它 我可以定位上面的文本,但我也需要更改颜色,如果我在jQuery中这样做,就会造成混乱 CSS: .button { display: block; width: 130px; height: 40px; cursor: pointer; position: fixed; z-index: 1; top: 15px
.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');
});
它创造了一个伟大的动画。但是文本,它随它一起滑动。我怎样才能在不出现颜色变化的情况下防止这种情况
这是JSFIDLE:您可以将文本放在它唯一的层上:
<a href="#" class="button">
<h1 class="front"><span></span></h1>
<h1 class="back"><span></span></h1>
<h1 class="text"><span>hello</span></h1>
</a>
有两件事:
。按钮:悬停span{color:#000}
$(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');
});
});
});