最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JS Exception : animate is not a function - Stack Overflow

programmeradmin1浏览0评论

I've an image inside a div, and the following Javascript code:

function animate_down(key1, key2) 
{
   cimage = document.getElementById('sslot_img' + key1);
   var topval = cimage.offsetTop;
   if (topval == -416) {
        cimage.animate({ top: '0px' }, 5000);
   }
   else {
        topval = topval - 32;
        var toptxt = { 'top': topval.toString() };
        cimage.animate(toptxt, 5000);
    }
}

I've test it in Firebug, when the debugger reaches .animate function it's throwing :

cimage.animate is not a function 

what am i missing?

I've an image inside a div, and the following Javascript code:

function animate_down(key1, key2) 
{
   cimage = document.getElementById('sslot_img' + key1);
   var topval = cimage.offsetTop;
   if (topval == -416) {
        cimage.animate({ top: '0px' }, 5000);
   }
   else {
        topval = topval - 32;
        var toptxt = { 'top': topval.toString() };
        cimage.animate(toptxt, 5000);
    }
}

I've test it in Firebug, when the debugger reaches .animate function it's throwing :

cimage.animate is not a function 

what am i missing?

Share Improve this question edited May 30, 2013 at 10:08 toro2k 19.2k8 gold badges65 silver badges71 bronze badges asked Sep 7, 2012 at 12:58 Alaa AlweishAlaa Alweish 9,10617 gold badges61 silver badges85 bronze badges 1
  • probably your code doesn't contain the latest version of jquery. – Codegiant Commented Sep 7, 2012 at 13:01
Add a ment  | 

4 Answers 4

Reset to default 4

getElementById is not a jQuery function, and will not yield a jQuery object. animate is only available on jQuery objects. You need to either wrap your cimage:

$(cimage).animate( ... )

or fetch it through jQuery rather than getElementById

var cimage = $('#sslog_img' + key1);

Note that if you go with the latter option, cimage will not have an offsetTop property, but rather, you'd have to use

cimage.offset().top

or

cimage[0].offsetTop

.animate() is a jquery function, you need to use it on a jquery object.

instead of

 cimage = document.getElementById('sslot_img' + key1);

use

 var cimage = $('#sslot_img' + key1);

try with

cimage = $('#sslot_img' + key1);
cimage = document.getElementById('sslot_img' + key1);

Is not a jQuery Object.

$('#sslot_img' + key1');

will return one you can animate.

发布评论

评论列表(0)

  1. 暂无评论