Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery match div height with dynamic image height

I have a image which has a width set to 100% and a min-width of 1024px. I want to keep my 'shadow' div over the image, and also match it's height as the window size changes, which causes the image size to proportionately change to the window width. My current code appears to do nothing...

Template is here http://jordan.rave5.com/tmp/ you'll notice the backgroudn-overlay and background-gradient divs don't expand 100% of the document. That's another problem. Lol. I'm trying to get them to be the BG 100% width and height.

jQuery:

            $('.header-img').css('height', function(imgheight) {
                $('.image-grad').css({'height': + imgheight});
            });

CSS:

            .image-grad {
                position: absolute;
                z-index: 600;
                transition: width 2s;
                -webkit-transition: width 2s;
                width: 100%;
                min-height: 174px;
                max-height: 608px;
                background-image: url(images/header-img.png);
                background-repeat: repeat-x;
                background-position: bottom;
                top: 0;
                left: 0;
            }

            .header-img {
                position: relative;
                z-index: 500;
                width: 100%;
                min-width: 1024px;
                opacity: 0.0;
            }

HTML:

                    <img class="header-img" src="slides/fields.jpg" alt="Panoramic Fields" />
                    <div class="image-grad"></div>

How can I accomplish this?

like image 502
WASasquatch Avatar asked Apr 25 '13 07:04

WASasquatch


People also ask

How to set the height of a div dynamically using jQuery?

Answer: Use the JavaScript height() method You can set the height of a <div> box dynamically using the jQuery height() method.

How to dynamically set height of div?

The content height of a div can dynamically set or change using height(), innerHeight(), and outerHeight() methods depending upon the user requirement.

How to get height of div in jQuery?

innerHeight() - Returns the height of an element (includes padding) outerWidth() - Returns the width of an element (includes padding and border) outerHeight() - Returns the height of an element (includes padding and border)

How to change height with jQuery?

To set the width and height of an element using jQuery, use the width() and height() in jQuery.


2 Answers

you need to set the height of the div using .height

you can do something like the following:

http://jsfiddle.net/Q4DRp/

var imageGrad = $('.image-grad'),
    image = $('.header-img');

function resizeDiv () {
    imageGrad.height(image.height());
    imageGrad.width(image.width());
}

resizeDiv();

$(window).resize(function() { resizeDiv(); });
like image 131
Pete Avatar answered Oct 20 '22 18:10

Pete


This will help you resize your shadow image:

Solution (Not tested)

$('.image-grad').css('height', $('.header-img').attr('height'));

To get the height of your image, use .attr('height'). Then to set the height of the div, use .css('height','999').

like image 28
Mysteryos Avatar answered Oct 20 '22 20:10

Mysteryos