Capturer dynamicment une position div

Existe-t-il un moyen d’afficher / capturer une valeur supérieure divs pendant qu’elle change d’animation, comme dans l’onglet Eléments de l’outil de développement Chrome? J’ai essayé d’obtenir getComputedStyle et getPropertyValue mais cela ne fonctionne pas. C’est ce que j’ai essayé jusqu’à présent

var top_sq = document.getElementById("square"), style_sq_top = getComputedStyle(top_sq), sq_top = style_sq_top.getPropertyValue("top"), act_sq_top = sq_top.subssortingng(0, 3); var tv = document.getElementById("top_value").text = sq_top; $("#btn1").click(function(){ $("#square").animate({top:"300px"}, 3000); tv.toSsortingng; }); 

http://jsfiddle.net/QYVQT/

Le problème avec le code que vous avez fourni est que tv ne change pas dynamicment car la boîte est animée. N’oubliez pas que sq_top est simplement un nombre. Lorsque vous définissez tv égal à sq_top , vous le définissez à un nombre. Ainsi, la valeur de tv ne change pas lorsque la boîte se déplace.

Au lieu de cela, recalculez-le à chaque fois. Vous pouvez utiliser la propriété progress de la fonction .animate() pour cela. Voir ici: http://www.bennadel.com/blog/1856-using-jquery-s-animate-step-callback-function-tcreate-ceate-animations.htm .

Code utilisant la fonction $.progress() :

 var square = document.getElementById("square"), squareTop = null, squareTopSsortingng = null, tvDiv = document.getElementById('top_value'); $("#btn1").click(function(){ $("#square").animate( {top:"300px"}, { duration: 3000, progress: function(){ /* get the top of the square */ squareTop = square.style.top; /* format it correctly */ dotLoc = squareTop.indexOf('.'); if(dotLoc == -1){ squareTopSsortingng = squareTop; } else{ squareTopSsortingng = squareTop.subssortingng(0, dotLoc) + 'px'; } /* display the current top position, eg "200px" */ tvDiv.innerHTML = squareTopSsortingng; }, easing: "swing" } ); }); 

Et un violon: http://jsfiddle.net/KLhzp/3/

Voici un exemple d’utilisation du rappel step() jQuery animate() .

 var top_sq = document.getElementById("square"), style_sq_top = getComputedStyle(top_sq), sq_top = style_sq_top.getPropertyValue("top"), act_sq_top = sq_top.subssortingng(0, 3); var $tv = $("#top_value") $tv.text(act_sq_top); $("#btn1").click(function () { $("#square").animate({ top: "300px" }, { duration: 3000, step: function (now, fx) { pos = Math.round(now); $tv.text(pos); } }); }); 

Et voici un violon http://jsfiddle.net/QYVQT/2/