April 5, 2012

auto fit image in div / jquery

Question by tintincutes

How can I auto-fit an image if my code is like this:

<script type="text/javascript"> 
jQuery.noConflict();
jQuery(document).ready(function () {
    var count = 0;
    function animate() {
        jQuery(".fadein").eq(count).fadeIn(1000);
        count++;
        if(count <= jQuery(".fadein").length) {
            setTimeout(animate, 1000);
        } else {
            jQuery("a.fadein").fadeOut(1000);
            count = 0;
            animate();
        }      
    }
    animate();
});
</script>

Would this be correct?

<script type="text/javascript"> 
    jQuery.noConflict();
    jQuery(document).ready(function () {
        var count = 0;
        function animate() {
            jQuery(".fadein").eq(count).fadeIn(1000);
            count++;
            if(count <= jQuery(".fadein").length) {
                setTimeout(animate, 1000);
            } else {
                jQuery("a.fadein").fadeOut(1000);
                count = 0;
                animate();
            }      
        }
        animate();
    })
            function fitImagetoContainer() {   $("img").each(function(i) {
     $(this).width($(this).parent().width()); //or your logic   
       }); }

//Call the function at load 
      $(window).load(function() {    fitImagetoContainer(); });


//Also call the function when the window resizes  
      $(window).resize(function() {    fitImagetoContainer(); });;
    </script>

Answer by Starx

Here is my throw at your problem

//Create a function to resize the image with respect to parent
function fitImagetoContainer() {
  $("img").each(function(i) {
     $(this).width($(this).parent().width()); //or your logic
  });
}

//Call the function at load
$(window).load(function() {
   fitImagetoContainer();
});


//Also call the function when the window resizes 
$(window).resize(function() {
   fitImagetoContainer();
});
April 5, 2011

Why does the size of a div element change when changing position from fixed to absolute?

Question by ThiefMaster

I have a div containing some text. This div used to be position:fixed, but for some reasons I need it to be position:absolute.

However, when changing it from fixed to static its size changed (the “auto-sizing” during fixed display was nice and should be preserved).

Here’s a minimal example: http://jsfiddle.net/ThiefMaster/yBRa9/3/

I’m looking for a way to keep position:absolute without the element shrinking to the lowest possible size.

Using JavaScript it’s easy to achieve but if it could be done without additional JS it would be nice.

Answer by Starx

Here Check it. I used a span with inline-block, seems to do what you want (Of course, If I had understood your question properly).

...

Please fill the form - I will response as fast as I can!