javascript - offset not changing after jQuery animation -


http://plnkr.co/edit/c6oaitttvlhltk7b3muf?p=preview

i'm working on jquery animation , need have animated divs retain new offset after animation ends.

the goal of animation able click on 1 of smaller circles @ bottom , swap places/size larger circle @ top.

function leadershiptoshow(id) {   console.log('leadershiptoshow (' + id + ')');   var x = $('.activeleadership').offset().left;   var y = $('.activeleadership').offset().top;   var h = $('.activeleadership').height();   var w = $('.activeleadership').width();   if($(id).hasclass('activeleadership')){ console.log('already selected');return false; }   console.log('x ' + x + ', y ' + y);   console.log($(id).offset());   var xi = $(id).offset().left;   var yi = $(id).offset().top;   console.log('xi ' + xi + ', yi ' + yi);   var hi = $(id).height();   var wi = $(id).width();   var xoffset = math.abs(x - xi);   var yoffset = math.abs(y - yi);    console.log('xoffset ' + xoffset + ', yoffset ' + yoffset);    $(id).animate({   left: -xoffset + 15,   top: -yoffset,    height: h,   width: w   }, 500, 'linear', function() {console.log('id animation complete');});    var selected = $('.activeleadership');    console.log(selected.offset());   selected.animate({   left: xoffset + 15,   top: yoffset,    height: hi,   width: wi   }, 500, 'linear',function() {     console.log('selected animation complete');     console.log('new active ' + $('.activeleadership').attr('id'));     console.log('new active position x ' +   $('.activeleadership').offset().left      + ', y ' +   $('.activeleadership').offset().top);     console.log('previous active ' + selected.attr('id'));     console.log('previous active position x ' + selected.offset().left + ', y   '          + selected.offset().top);}    );   $(id).addclass('activeleadership');   selected.removeclass('activeleadership');  } 

i got , figured out. solution have take offsets of divs based on first offset locations. after they've been animated, moving them again relative starting offset. make work have store starting offset locations in global variable outside of function. if statement associate id of div variable of initial offset. matter of taking difference between div want animate's initial offset , location want move to.

updated plunker http://plnkr.co/edit/c6oaitttvlhltk7b3muf?p=preview

var circle0 = { 'left': $('#circle0').offset().left, 'top': $('#circle0').offset().top }; var bigcircle = { 'height': $('.activeleadership').height(), 'width': $('.activeleadership').width() }; var circle1 = { 'left': $('#circle1').offset().left, 'top': $('#circle1').offset().top }; var circle2 = { 'left': $('#circle2').offset().left, 'top':              $('#circle2').offset().top }; var circle3 = { 'left': $('#circle3').offset().left, 'top':       $('#circle3').offset().top }; var circle4 = { 'left': $('#circle4').offset().left, 'top':   $('#circle4').offset().top }; var othercircles = { 'height': $('#circle1').height(), 'width':   $('#circle1').width() }; var time = 400;  function leadershiptoshow(id) {   var circletomoveup = $(id);   if(circletomoveup.hasclass('activeleadership')){ return false; }   var originofcircletomoveup;   if(id === '#circle0'){ originofcircletomoveup = circle0; }   else if(id === '#circle1'){ originofcircletomoveup = circle1; }   else if(id === '#circle2'){ originofcircletomoveup = circle2; }   else if(id === '#circle3'){ originofcircletomoveup = circle3; }   else if(id === '#circle4'){ originofcircletomoveup = circle4; }   var xoffsetup = circle0.left - originofcircletomoveup.left;   var yoffsetup = circle0.top - originofcircletomoveup.top;    var circletomovedown = $('.activeleadership');   var originofcircletomovedown;   if(circletomovedown.attr('id') === 'circle0'){ originofcircletomovedown = circle0; }   else if(circletomovedown.attr('id') === 'circle1'){ originofcircletomovedown = circle1; }   else if(circletomovedown.attr('id') === 'circle2'){ originofcircletomovedown = circle2; }   else if(circletomovedown.attr('id') === 'circle3'){ originofcircletomovedown = circle3; }   else if(circletomovedown.attr('id') === 'circle4'){     originofcircletomovedown = circle4; }   var xoffsetdown = circletomoveup.offset().left -  originofcircletomovedown.left;   var yoffsetdown = circletomoveup.offset().top -   originofcircletomovedown.top;    /* small circle big circle animation */   circletomoveup.animate({     left: xoffsetup + 15,     top: yoffsetup,      height: bigcircle.height,     width: bigcircle.width   }, time, 'linear');   /* big circle small circle animation */   circletomovedown.animate({     left: xoffsetdown + 15,     top: yoffsetdown,      height: othercircles.height,     width: othercircles.width   }, time, 'linear');    circletomoveup.addclass('activeleadership');   circletomovedown.removeclass('activeleadership'); } 

Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -