Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
背景色;动画";使用jQuery_Jquery_Css_Jquery Animate - Fatal编程技术网

背景色;动画";使用jQuery

背景色;动画";使用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

我正在尝试创建一个简单的动画,背景色在其中滑动到位。到目前为止,它是成功的,但有一个主要问题:块上的文本移动,我不知道如何阻止它

我可以定位上面的文本,但我也需要更改颜色,如果我在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');
});
它创造了一个伟大的动画。但是文本,它随它一起滑动。我怎样才能在不出现颜色变化的情况下防止这种情况


这是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>
有两件事:

  • 可能需要稍微调整一下,但这就是你想要的效果。基本上,我只是从h1中取出文本,在其上放置一个z索引,使其始终位于前面,然后使用
    。按钮:悬停span{color:#000}

  • 使用h1来实现这一点并不是正确使用h1。我会用普通的div

  • $(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');
            });
        });
    
    });