jQuery -Function没有被正确调用

时间:2015-02-14 01:28:50

标签: javascript jquery function

好的,这个问题很简单。我试图调用我拥有的这个tele_in函数,但我无法运行它。我不确定我该怎么做。我在我尝试调用该功能的区域中发出警报并且它有效...我错过了什么?这是代码:我试图调用的函数位于顶部,称为" tele_in"。我试图称之为的地方已被评论到底部。我在这里做错了什么?

Var SpriteVis;

jQuery(document).ready(function tele_in($) {   // function to make sprite appear.
    $("#sprite").animate({bottom: '0px'}, 400, 'linear', function () {
        $("#sprite").css({
           'background-image': 'url(/images/Warp-Sprite.png)',
           'height': '50px',
           'width': '90px',
           'left': '300px',
           'bottom': '80px'
        });
        setTimeout(function () {
            $("#sprite").css({
            'background-image': 'url(/images/test-sprite.png)',
           'height': '120px',
           'width': '96px'
            });
        }, 80);
    });
    SpriteVis = true;
});


jQuery(function ($) {
    $(window).scroll(function () {
        if (SpriteVis == true) {  //if Sprite is present on screen, run the animation sequence to make it disappear.
            $("#sprite").css({
               'background-image': 'url(/images/Warp-Sprite.png)',
               'height': '50px',
               'width': '90px',
               'left': '300px',
               'bottom': '80px'
            });
            setTimeout(function () {
                $("#sprite").css({
               'background-image': 'url(/images/Teleport-Sprite.png)',
               'height': '188px',
               'width': '52px',
               'left': '330px'
                });
                $("#sprite").animate({bottom: '2000px'}, 400, 'linear', function () {
                });
            }), 80;
            SpriteVis = false;
        } else {

            //I need to call the "tele_in" function here .

        }
    });
});

3 个答案:

答案 0 :(得分:3)

你必须从jQuery调用中提取该函数声明:

function tele_in($) {   // function to make sprite appear.
    $("#sprite").animate({bottom: '0px'}, 400, 'linear', function () {
        $("#sprite").css({
           'background-image': 'url(/images/Warp-Sprite.png)',
           'height': '50px',
           'width': '90px',
           'left': '300px',
           'bottom': '80px'
        });
        setTimeout(function () {
            $("#sprite").css({
            'background-image': 'url(/images/test-sprite.png)',
           'height': '120px',
           'width': '96px'
            });
        }, 80);
    });
    SpriteVis = true;
});

Query(document).ready(tele_in);

使用函数实例化表达式创建的函数(如您的情况)没有将其名称导出到本地作用域。该名称仅在 inside 函数中可见。

通过将其移动到函数声明语句,可以在包含范围内创建一个始终可见的符号。

请注意,由于您定义该功能的方式,您必须像这样调用它:

    tele_in($);

或者函数中$的值为undefined

答案 1 :(得分:0)

您必须在jquery ready处理程序之外声明该函数:

function tele_in () {
    // ...
}

jQuery(document).ready(tele_in);

$在全局范围内可用,并且不需要作为参数传递给tele_in(除非你使用多个jquery对象,我没有这种情况的经验,甚至不是它是可行的。

答案 2 :(得分:0)

由于在文件就绪时定义了tele_in,所以不会发生任何事情,只需合并一切:

Var SpriteVis;

jQuery(document).ready(
  function tele_in() {   // function to make sprite appear.
    $("#sprite").animate({bottom: '0px'}, 400, 'linear', function () {
      $("#sprite").css({
       'background-image': 'url(/images/Warp-Sprite.png)',
       'height': '50px',
       'width': '90px',
       'left': '300px',
       'bottom': '80px'
      });
      setTimeout(function () {
        $("#sprite").css({
        'background-image': 'url(/images/test-sprite.png)',
       'height': '120px',
       'width': '96px'
        });
      }, 80);
    });
    SpriteVis = true;

    $(window).scroll(function () {
      if (SpriteVis == true) {  //if Sprite is present on screen, run the animation sequence to make it disappear.
       $("#sprite").css({
         'background-image': 'url(/images/Warp-Sprite.png)',
         'height': '50px',
         'width': '90px',
         'left': '300px',
         'bottom': '80px'
        });
        setTimeout(function () {
          $("#sprite").css({
           'background-image': 'url(/images/Teleport-Sprite.png)',
           'height': '188px',
           'width': '52px',
           'left': '330px'
            });
          $("#sprite").animate({bottom: '2000px'}, 400, 'linear', function () {
        });
      }), 80;
      SpriteVis = false;
    } else {
      tele_in();
    }
  });
});