简体   繁体   English

jQuery宽松插件问题

[英]jQuery easing plugin problem

I found a tutorial on the internet. 我在互联网上找到了一个教程。 To animate things with jquery. 用jquery制作动画。 I would animate a h1 tag in the header of my website. 我会在网站标题中为h1标签设置动画。 And i would like a logo on my website in the header. 我想在我的网站标题中找到一个徽标。 I used this code for the animation. 我将此代码用于动画。

/* Slogan (h1) effect header */
$(function () {
    // make sure your h2's have a position relative
    $('#header h1').css({
        left: '600px'
    })

    jQuery.easing.def = 'easeOutBounce';
    setTimeout(animate, 1000);
});

function animate() {
    $('#header h1').animate({
        left: 0
    }, 1000);
}

/* Effect op logo */
$(function () {

    $('#header .logo').css({
        top: '-600px'
    })

    jQuery.easing.def = 'easeOutBounce';
    setTimeout(animate, 1000);
});

function animate() {
    $('#header .logo').animate({
        top: 0
    }, 1000);
}

For this animation, i used the jquery.easing1.3 plugin. 对于此动画,我使用了jquery.easing1.3插件。 Now come the problem. 现在来问题。 With the code that i make. 用我制作的代码。 Only the effect on the logo will play. 只会播放徽标上的效果。 The effect on the h1 will nog play. h1上的效果将开始播放。 What i must do? 我应该做什么? That the h1 logo and the header.logo animate???? 那h1徽标和header.logo动画了吗???

Thanks !!!! 谢谢 !!!!

you have overwritten the second animate() ... you can rename it to fixed the problem... 您已经覆盖了第二个animate() ...您可以将其重命名以解决问题...

/* Effect op logo */

$(function () {

    $('#header .logo').css({
        top: '-600px'
    })

    jQuery.easing.def = 'easeOutBounce';
    setTimeout(animate2, 1000);
});

function animate2() {
    $('#header .logo').animate({
        top: 0
    }, 1000);
}
/* Slogan (h1) effect header */
$(function () {
    // make sure your h2's have a position relative
    $('#header h1').css({
        left: '600px'
    })

    jQuery.easing.def = 'easeOutBounce';
    setTimeout(animate, 1000);
});

function animate() {
    $('#header h1').animate({
        left: 0
    }, 1000);
}

/* Effect op logo */
$(function () {

    $('#header .logo').css({
        top: '-600px'
    })

    jQuery.easing.def = 'easeOutBounce';
    setTimeout(animate, 1000);
});

function animateAgain() {
    $('#header .logo').animate({
        top: 0
    }, 1000);
}

Although Neurofluxation's solution will work (except for the typo that no doubt will be edited away by the time I'm done with this post...), there are some general bad practice issues here. 尽管Neurofluxation的解决方案可以工作(除了打字错误,毫无疑问,我在写完这篇文章的时间之前,打字错误将被删除……),但这里仍然存在一些常规的不良做法问题。

To start with, if you have all of this code in the same page, there's a lot of repetition. 首先,如果您在同一页面上拥有所有这些代码,则会有很多重复。 I suggest you refactor some, to make your code more reusable. 我建议您重构一些代码,以使代码更可重用。 Also, even though the $(function () { }); 另外,即使$(function () { }); statements will be merged, there's no need to explicitly write them apart. 语句被合并,无需明确地将它们分开。 That just increases the amount of code that you're not really interested in. 这只会增加您并不真正感兴趣的代码量。

This is how I'd do it: 我就是这样做的:

$.easing.def = 'easeOutBounce';

$(function () {
    $('#header h1').css({ left: '600px' });
    $('#header .logo').css({ top: '-600px' });

    setTimeout(animateAll, 1000);
});

function animateAll() {
    $('#header h1').animate({ left: 0 }, 1000);
    $('#header logo').animate({ top: 0 }, 1000);
}

Nice and DRY, don't you think? 不错,干,你不觉得吗? =) =)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM