absolute - jquery return element to its original position -


i'm trying make div expand upward on hover, return original height on mouseout. pretty simple. div positioned absolute, bottom: 0, clings bottom of parent. catch don't have control on length of content in div, top value set auto.

therefore, need calculate actual top position, pass value second function in hover(), handles mouseout animations, in order restore original height.

    $('#blog3 .blog-grid li').hover(     function () { // mouse on         // calculate .content-box top position         var offset = $(this).find(".content-box").offset().top - $(this).find(".content-box").parent().offset().top;          // darken box         $(this).find(".content-box").css("background-color", "rgba(0,0,0,0.5)").animate({             queue: false,             duration: 500         });          // expand content div         $(this).find(".content-box").stop(true,true).animate({             top: 0,         }, {             queue : false,             duration : 500         });          $(this).find("p").fadein("slow");         $(this).find(".read-more").stop(true,true).fadein("slow");     },      function () { // mouse out         $(this).find(".content-box").css("background-color", "transparent").animate({             queue: false,             duration: 500         });          $(this).find(".content-box").stop(true,true).animate({             top: offset + 'px', // <-- restore original position         }, {             queue : false,             duration : 500         });          $(this).find("p").stop(true,true).fadeout("slow");         $(this).find(".read-more").stop(true,true).fadeout("slow");     } ); 

the problem, of course, can't pass 'offset' variable second (mouseout) function, nor can declare 'offset' global variable, because needs calculated on hover.

i'm sure there must way pass number second function, can't seem figure out...

answered own question ;)

set 'offset' global variable, perform position calculation inside mouseover function.

var offset = null; $('#blog3 .blog-grid li').hover(     function () {         //get .content-box top position         offset = $(this).find(".content-box").offset().top - $(this).find(".content-box").parent().offset().top;          // darken box         $(this).find(".content-box").css("background-color", "rgba(0,0,0,0.5)").animate({             queue: false,             duration: 500         });          // initialize position         $(this).find(".content-box").css("top", offset);          // expand content div         $(this).find(".content-box").stop(true,true).animate({             top: 0,         }, {             queue : false,             duration : 500         });          $(this).find("p").fadein("slow");         $(this).find(".read-more").stop(true,true).fadein("slow");     },      function () {         $(this).find(".content-box").css("background-color", "transparent").animate({             queue: false,             duration: 500         });          $(this).find(".content-box").stop(true,true).animate({             top: offset + 'px',         }, {             queue : false,             duration : 500         });          $(this).find("p").stop(true,true).fadeout("slow");         $(this).find(".read-more").stop(true,true).fadeout("slow");     } ); 

Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -