javascript - jQuery expand element from center not working -


first say: have read numerous web articles questions , answers find here. many helpful, not solution. also, started learning jquery 5 days ago :)

i trying make zoom-like effect on hover event in gallery excercise. need image seem expand center, not down , right. if understood correctly, need move half way left , work. also, i'm using ems, try convert ems pixels here. please help!

relevant html:

<div id="gallery">   <img src="img/cool1.gif">   <img src="img/cool2.gif" id="gal2">   <img src="img/cool3.gif" id="gal3"> </div> 

css:

#gallery {   width: 31em;   margin-left: auto;   margin-right: auto; }  #gallery img {   width: 10em;   height: auto;   position: absolute; }  #gal2 {   margin-left: 10em; }  #gal3 {   margin-left: 20em; } 

finally, jquery:

var fontsize = $("#gallery img").css("font-size");//equal 1em? var fontint = parseint(fontsize);  var t = $("#gallery img").position().top; var tnew = t - (5 * fontint);//top position  var l = $("#gallery img").position().left; var lnew = l - (5 * fontint);//left position   $("#gallery img").hover(     function() {          $(this).stop().css("zindex", "1").animate({             height : "20em",             width : "20em",             top : tnew,             left : lnew          }, 400);     }, //end mouseover     function() {         $(this).stop().animate({             height : "10em",             width : "10em",             top : t,             left : l,             zindex : "0"         }, 400);     } //end mouseout );//end hover 

edit 1 images expand , change position, not expected. also, don't return on mouseout. racil hilan solving em-px conversion problem!

edit 2 problem moslty solved fixing variable scope – position values moved before hover() function. remaining bug pictures escape top right corner of body before returning place on first interaction. afterwards, runs expected. also, explain why works when fontint variable multiplied five, not 10?

edit 3 – solution mauricio santamaria said below, add css() function setting top , left parameters before hover on #gallery img element so:

$("#gallery img").css({"top" : t, "left" : l}).hover(...); 

the rest stays same.

i improvised fiddle this, too: http://jsfiddle.net/dzenesiz/wudw5hmu/15/

the problem $(this).css("font-size"); returns size unit (e.g. 16px) not number , calculation results in nan.

a quick solution parse integer this:

var fontsize = parseint($(this).css("font-size")); //equal 1em? 

Comments

Popular posts from this blog

java - Static nested class instance -

c# - Bluetooth LE CanUpdate Characteristic property -

JavaScript - Replace variable from string in all occurrences -