2

我正在尝试使用 JS 完成悬停动画,但我对所述语言的技能非常有限。我的 script.js 文件中有这个脚本,因为它是我使用的唯一脚本:

$(document).ready(function() {

    animationHover('#intro-logo' 'tada')

function animationHover(element, animation){
    element = $(element);
    element.hover(
        function() {
            element.addClass('animated ' + animation);          
        },
        function(){
            //wait for animation to finish before removing classes
            window.setTimeout( function(){
                element.removeClass('animated ' + animation);
            }, 2000);           
        });
}

}

})();

目标是当我将鼠标悬停在#intro-logo 上时,脚本将添加 .animated 和 .tada 类,它们将为该死的东西设置动画!但是我得到了 Uncaught SyntaxError: Unexpected string 这让我

animationHover('#intro-logo' 'tada')

我不知道我做错了什么,我在这里使用了一个教程,它对那个人有用,但我没有这样的运气。我真诚地感谢任何人的帮助。

提前谢谢你,这个社区很棒。这是我的第一个问题,但你们所有人帮助我解决了数百个问题,一路走来,我的 Web 开发能力很艰难(其中大部分显然仍然遥遥领先)。

编辑:我添加了缺少的逗号,现在看来我结束文档的方式存在问题。发布的是我的整个 JS 文件,如何正确关闭所有内容?})(); 似乎不起作用。

4

2 回答 2

0

如果你不一定需要 JS,你可以使用 jQuery:

$(document).ready(function() {
        $("#intro-logo").hover(function () {
             $(this).toggleClass('animated tada');
     });
   });

工作jsfiddle

或者,您也可以使用 :hover 伪类在纯 CSS 中执行此操作

于 2013-08-10T02:13:16.990 回答
0

工作示例

$(document).ready(function () {

    animationHover('#intro-logo', 'tada')

    function animationHover(element, animation) {
        element = $(element);
        element.hover(

        function () {
            element.addClass('animated ' + animation);
        },

        function () {
            //wait for animation to finish before removing classes
            window.setTimeout(function () {
                element.removeClass('animated ' + animation);
            }, 2000);
        });
    }

});

删除了不必要的}()最后,以及添加了逗号。

于 2013-08-09T19:13:48.220 回答