jQuery CSS 关键帧

发布于 2024-12-18 19:54:13 字数 649 浏览 2 评论 0原文

尝试使用 jQuery 制作一个简单的重复关键帧动画,

$(document).ready(function() {
    $('body').mouseover(function() {
        var animateloop = 0;

        $(this).mouseout(function(){
            animateloop++;
        });

        while (animateloop !== 1) {
            $(this).delay(40).css(
                'font-family':'Homestead'
            ).delay(40).css(
                'font-family':'Arvo'
            );
        }
    });
});

我认为上面的代码可以工作,但我不太了解 jQuery,所以我无法让它工作。

您可以在此处看到 JSFIDDLE:

http://jsfiddle.net/JamesKyle/nPVxb/

Trying to make a simple repeated keyframed animation with jQuery

$(document).ready(function() {
    $('body').mouseover(function() {
        var animateloop = 0;

        $(this).mouseout(function(){
            animateloop++;
        });

        while (animateloop !== 1) {
            $(this).delay(40).css(
                'font-family':'Homestead'
            ).delay(40).css(
                'font-family':'Arvo'
            );
        }
    });
});

I thought this code above would work, but I don't understand jQuery all that much so I can't make it work.

You can see this a JSFIDDLE here:

http://jsfiddle.net/JamesKyle/nPVxb/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

温折酒 2024-12-25 19:54:13

第一个错误:

$(this).delay(40).css(
   'font-family':'Homestead'
)

冒号:

$(this).delay(40).css(
   'font-family','Homestead'
)

one error first:

$(this).delay(40).css(
   'font-family':'Homestead'
)

the colon:

$(this).delay(40).css(
   'font-family','Homestead'
)
末蓝 2024-12-25 19:54:13

这有效。

$(this).delay(400).css({
   'font-family':'Homestead'
});

问题是您的 .delay() 而不是您的 .css()

.delay() 用于属于队列一部分的项目,例如动画。

您可以使用 .queue() 或 setTimeout()

阅读此线程的更多信息:jQuery 延迟不起作用

类似于:

   $(document).ready(function() {
    $('body').mouseover(function() {

        setTimeout(function() {changefont('arial');}, 400);
        setTimeout(function() {changefont('courrier new');}, 800);
        setTimeout(function() {changefont('impact');}, 1200);

    });
});


function changefont(fontname) {
    $('body').css({'font-family':fontname});
}

小提琴:http://jsfiddle.net/nPVxb/74/

This works.

$(this).delay(400).css({
   'font-family':'Homestead'
});

The problem is your .delay() and not your .css()

.delay() is used for items that are part of a queue, like animations.

You can use .queue() or setTimeout()

Read more on this thread: jQuery delay not working

Something like :

   $(document).ready(function() {
    $('body').mouseover(function() {

        setTimeout(function() {changefont('arial');}, 400);
        setTimeout(function() {changefont('courrier new');}, 800);
        setTimeout(function() {changefont('impact');}, 1200);

    });
});


function changefont(fontname) {
    $('body').css({'font-family':fontname});
}

Fiddle : http://jsfiddle.net/nPVxb/74/

人间不值得 2024-12-25 19:54:13

假设您想要一个无限循环并且正在对象的范围内工作...

...
animation : ["first","second","third","etc"],
frameDelay : 400,
frameIndex : 0,
animationTimer : null,
start : function() {
    // remember the scope of this object.
    var handle = this;
    // start the animation.
    handle._nextFrame(handle);
},
_nextFrame : function(handle) {
    // TODO: use frameIndex to render stuff... such as:
    var animation = handle.animation[frameIndex];
    $('body').html('<p>'+animation+'</p>');
    // count your frames. You might add stuff to the sequence elsewhere.
    var numFrames = handle.animation.length;
    frameIndex = frameIndex < numFrames ? frameIndex++ : 0;
    handle.animationTimer = window.setTimeout(function() {
        handle._nextFrame(handle); }, handle.frameDelay);
},
_destroy : function() {
    var handle = this;
    clearTimeout(handle.animationTimer);
}...

注意:
我使用一种老式方法在界面上强调私有函数。您不必以这种方式命名变量,而且它们不是私有的。

您会注意到我将“this”存储到“handle”中。您不能总是依赖于 this 的范围,例如从事件气泡调用对象函数、从公共接口调用它或在对象内部引用函数。所以我只是按照惯例这样做。

将此代码放入您的对象中,调用“start”函数,它应该继续执行其操作,直到您离开页面。另外,请确保清除递归超时,以免在页面刷新或页面导航时出现错误。

Assuming you want an infinite loop and are working within the scope of an object...

...
animation : ["first","second","third","etc"],
frameDelay : 400,
frameIndex : 0,
animationTimer : null,
start : function() {
    // remember the scope of this object.
    var handle = this;
    // start the animation.
    handle._nextFrame(handle);
},
_nextFrame : function(handle) {
    // TODO: use frameIndex to render stuff... such as:
    var animation = handle.animation[frameIndex];
    $('body').html('<p>'+animation+'</p>');
    // count your frames. You might add stuff to the sequence elsewhere.
    var numFrames = handle.animation.length;
    frameIndex = frameIndex < numFrames ? frameIndex++ : 0;
    handle.animationTimer = window.setTimeout(function() {
        handle._nextFrame(handle); }, handle.frameDelay);
},
_destroy : function() {
    var handle = this;
    clearTimeout(handle.animationTimer);
}...

Notes:
I use an old school method of underscoring private functions on an interface. You don't have to name your variables that way, and they are not private.

You will notice that I store "this" into "handle". You can't always rely on the scope of this, such as calling an object function from an event bubble, calling it from a public interface, or referencing a function internally to the object. So I just do this as a convention.

Put this code into your object, call the 'start' function, and it should continue doing its thing until you leave the page. Also, make sure to clean up your recursive timeouts, less you get an error on page refresh or page navigation.

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