javascript - Moving an element back to its original position after animation, jQuery - Stack Overflow

I'm struggling with trying to get this piece of code working properly. I'm moving an element

I'm struggling with trying to get this piece of code working properly. I'm moving an element with

$("#element").hide("slide", { direction: "right" }, 200);

Which works just great, but I need to reset it back to the original position before the animation, once the animation is plete so that it will animate again next time.

Any help on this would be greatly appreciated.

        var position = jQuery("#"+that.currentIconSlide).find('.slideInner').position();

        jQuery("#"+that.currentIconSlide).find(".slideInner").hide("slide", { direction: "right" }, 200, function(){
            jQuery(this).css({"left":position.left,"top":position.top}).parent().hide();    
        });     

JS Fiddle - /

I'm struggling with trying to get this piece of code working properly. I'm moving an element with

$("#element").hide("slide", { direction: "right" }, 200);

Which works just great, but I need to reset it back to the original position before the animation, once the animation is plete so that it will animate again next time.

Any help on this would be greatly appreciated.

        var position = jQuery("#"+that.currentIconSlide).find('.slideInner').position();

        jQuery("#"+that.currentIconSlide).find(".slideInner").hide("slide", { direction: "right" }, 200, function(){
            jQuery(this).css({"left":position.left,"top":position.top}).parent().hide();    
        });     

JS Fiddle - http://jsfiddle/zalun/E4mz9/

Share Improve this question edited Feb 9, 2012 at 18:34 David asked Feb 9, 2012 at 17:54 DavidDavid 36.5k14 gold badges51 silver badges81 bronze badges 4
  • 1 save its position before the animation – Ibu Commented Feb 9, 2012 at 17:55
  • Example of what i'm doing been added, but not working at the moment. – David Commented Feb 9, 2012 at 18:02
  • Could you post an example code on jsfiddle? – Koes Bong Commented Feb 9, 2012 at 18:10
  • jsfiddle's read-only, at the moment – Timothy Aaron Commented Feb 9, 2012 at 18:13
Add a ment  | 

3 Answers 3

Reset to default 2
$("#element").animate({'left':'300','opacity':'0'}, 2000, function(){

    $(this).css({'left':'0','opacity':'1'});

});

What I would do is not use jQuery - I would use plain JavaScript, like this:

elem.style.position = "relative";
// animate by adjusting `left` and `top` relative to the start position
elem.style.position = "static"; // jump back to start position.

Things are a lot easier in plain JS ;)

just writing code off my mind, might not execute, but you get the idea

var position = jQuery("#"+that.currentIconSlide).find('.slideInner').position();

function slide(){
    var original_position = 0;
        jQuery("#"+that.currentIconSlide).find(".slideInner").hide("slide", { direction: "right" }, 200, function(){
            jQuery(this).css({"left":position.left,"top":position.top}).parent().hide(); //? what is this for?
            jQuery(this).css("left":original_position); //set back to original left
            slide(); //run again
        });
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745354428a4624030.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信