I’m making some Rocket launching effect by jQuery. When I click on Rocket, it’ll swap with another rocket image, and then launch up. When I click “Reset” link, Rocket must reset starting location and image must revert back. But there are two problems. First, “my rocket image is not reverting back”. Second – after it’s reverted to initial location, if I click on Rocket again, it’s not launching. Can you see and find me solutions?
http://jsfiddle.net/thisizmonster/QQRsW/
$("#resetlink").click(function(){
clearInterval(timerRocket);
$("#wrapper").css('top', '250px');
$("#rocket").attr('src', 'http://www.kidostore.com/images/uploads/P-SAM-rocket-blk.jpg');
});
You can see $(“rocket”).attr() row.
You remove the original image here:
And all that’s left behind is
newImg. Then you reset link references the image using#rocket:But your
newImgdoesn’t have anidattribute let alone anidofrocket.To fix this, you need to remove
imgand then set theidattribute ofnewImgtorocket:And then you’ll get the shiny black rocket back again: http://jsfiddle.net/ambiguous/W2K9D/
UPDATE: A better approach (as noted by mellamokb) would be to hide the original image and then show it again when you hit the reset button. First, change the reset action to something like this:
And in the
newImg.loadfunction, grab the images original size:And finally, the callback for finishing the morphing animation becomes this:
New and improved: http://jsfiddle.net/ambiguous/W2K9D/1/
The leaking of
$('.throbber, .morpher')outside the plugin isn’t the best thing ever but it isn’t a big deal as long as it is documented.