1

I’m trying to style my container div with javascript if its content overflows.

The code I have so far is:

<div class="container" style="position: relative; height: 390px; margin-bottom: 90px; overflow-y: auto;">
     <div class="columns" style="height: auto; position: absolute;">
         some content
     </div> 
</div>

I’ve made this javascript function to dynamically add “box-shadow” to the bottom of the div if it overflows. It’s not working.

<script>
    $(window).resize(function(){
    $('.container').css('height', $(window).height() - 200);
    var columsheight = $('.colums').height();
    var containerheight = $ ('.container').height() -200;
    if (containerheight < columsheight){
         $('.container').css({"box-shadow" : "inset 0px -13px 8px -10px #868686"});
        }
    else{};
}).resize();
</script>

I would like some help to set it correctly.

2 Answers 2

3

Do you see the typo in your script?

 var columsheight = $('.colums').height();

should, according to your markup, be:

 var columsheight = $('.columns').height();

Oh. Ouch. Even more to the point... your whole jQ window resize function is in bad shape. Your resize() function is closing out before it's completed the rest of its processing:

 $(window).resize(function(){
     $('.container').css('height', $(window).height() - 200);
     var columsheight = $('.columns').height();
     var containerheight = $('.container').height();
     if (containerheight < columsheight){
         $('.container').css("box-shadow","inset 0px -13px 8px -10px #868686");
     };
}).resize();

Better yet, to clarify what you're doing, and to reduce the chances that your first resize() isn't missed, you should probably do something like this.

function resizeContainers(){
     $('.container').css('height', $(window).height() - 200);
     var columsheight = $('.columns').height();
     // open your browser's console, and watch as your code executes
     // you should see a line written every time it goes through this function
     console.log(".columns height is: " + columsheight);
     var containerheight = $('.container').height();
     console.log("containerheight height is: " + containerheight);
     if (containerheight < columsheight) {
         $('.container').css("box-shadow","inset 0px -13px 8px -10px #868686");
     }
}

$(window).resize(resizeContainers);

resizeContainers();

This way you can call the function independently if you need to, without having to trigger a full window resize() event. The window.resize() event is especially sensitive... it gets triggered by a lot of different things, and gets worse if you use it on a mobile device, as some mobile browsers interpret orientation changes as a window.resize().

Ok... now that the waters have now been muddied, I've put together a working example:

$(function () {
    // create your method for checking the resize
    function resizeContainers() {
        // get a reference to the .container element and the .columns element
        var $container = $('.container');
        var $cols = $('.columns');
        // set the height on $container
        $container.css('height', $(window).height() - 200);
        // THis is just here so you can see, as you resize the frame, 
        // that this is testing the sizes 
        $("#output").html("$cols.height() : " + $cols.height() + "\n$container.height() : " + $container.height());
        // Now compare the height of the $cols item to the $container height
        if ($cols.height() > $container.height()) {
            $container.css("box-shadow", "inset 0px -13px 8px -10px #868686");
        } else {
            // this will remove the box shadow when the content does not exceed
            // the container height
            $container.css("box-shadow","");   
        }
    }

    // Now, tell the window object to listen for resize events and call resizeContainers
    $(window).resize(resizeContainers);
    // Call it manually once
    resizeContainers();
});

You can see this in practice at http://jsfiddle.net/mori57/wV7Vt/

Watch the output div and drag the frame bar around the output window to watch the values change.

Sign up to request clarification or add additional context in comments.

5 Comments

I've corrected the code and now I have this: $(document).ready(function resizeContainers(){ $('.container').css('height', $(window).height() - 200); var el = document.getElementById('.container'); if (el.clientHeight < el.scrollHeight) { $('.container').setAttribute("box-shadow","inset 0px -13px 8px -10px #868686"); } }); $(window).resize(resizeContainers); resizeContainers(); And atill not working :/
Nope. You don't need to do what the other poster said... you're using jQuery, you don't need to getElementById(), and you're trying to pass it a class name in jQuery style, so it wouldn't work anyhow. Start with what I gave you, see if that works. If not, start putting console.log() calls in the code and watch the Javascript console in your browser as you resize the window, to see what values are triggered during resize.
Uau!! That's exactly what i need! Don't know why is not working in my page. Trying to find out. Ah! The shadow just appears when I resize the window to a smaler height.
Do you have enough content in your .columns div to exceed the height of the .container div? If not, you're not going to see anything happen. I added in text just so you could see what it would do if there was content. At this point, I don't know that I can provide further help unless you can see an error of some sort in your JS console. Good luck!
As written, it looks like it's doing what I'd expect, in Firefox... it's adding in the bottom inner box-shadow. What web browser are you testing in?
1
  • ccolumsheight should be columsheight. Or even better, columsheight should be columnsheight.

  • class:"container" should be class="container"

  • $('.colums') should be $('.columns')

You gotta watch out for typos and console errors!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.