背景颜色“动画”使用 jQuery

发布于 2024-12-09 13:08:44 字数 1474 浏览 0 评论 0原文

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

我可以将文本放置在上面,但我还需要更改颜色,如果我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情绪失控 2024-12-16 13:08:44

有两件事:

  1. http://jsfiddle.net/WERFJ/13/ 它可能需要稍微调整一下,但这就是你想要的效果。基本上我只是从 h1 中取出文本并在其上放置 z 索引,使其始终位于前面,然后使用 .button:hover span { color: #000 }

  2. 使用 h1 来这样做不是 h1 的正确使用。我只想使用常规 div。

Two things:

  1. 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 }

  2. Using h1's to do this isn't a proper use of h1. I would just use regular div's.

獨角戲 2024-12-16 13:08:44

您可以将文本放在它的唯一层上:

<a href="#" class="button">
    <h1 class="front"><span></span></h1>
    <h1 class="back"><span></span></h1>
    <h1 class="text"><span>hello</span></h1>
</a>

http://jsfiddle.net/WERFJ/14/

也许还有完整的文本效果:

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

});

http://jsfiddle.net/WERFJ/17/

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>

http://jsfiddle.net/WERFJ/14/

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

});

http://jsfiddle.net/WERFJ/17/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文