javascript - jQuery function callback does not work in custom class/object -
so newbie programming excuse me if novice question. creating simple wave effect create illusion of moving water. did simple function in jquery , worked fine. learning oop code more organized project working on. created class handle animation. animation works fires once. need keep firing create illusion. function callback works normal procedual way not seem work within object class. have researched , researched , can not find answer specific problem. appreciated. code below.
function waves(selector){ this.selector = selector; this.animatewaves = function(forward,backward,speed){ $(selector).velocity({translatex: forward},speed); $(selector).velocity({translatex: backward},speed,animatewaves); }; }; var surfacewaves = new waves('#topwaves'); surfacewaves.animatewaves('+=40','-=40','1000');
edit actually, first solution not work because context of callback element, not object. here working code , jsfiddle:
function waves(selector){ this.selector = selector; this.animatewaves = function(forward,backward,speed){ var self = this; $(selector).velocity({translatex: forward},speed); $(selector).velocity({translatex: backward}, speed, function () { self.animatewaves(forward,backward,speed); }); }; }; var surfacewaves = new waves('#topwaves'); surfacewaves.animatewaves('+=40','-=40','1000');
removed old code incorrect code
Comments
Post a Comment