Is there any way to resize an animated pic?

Is there a fast and relieable way to watch  $(window).resize() width() via jQuery and saving the result to a global var?

  • I tried it several ways. My problem: I want to layout a fluid design where some <div> floating in a wrapper. I declared a var (based on the document.width) on top of my .js and use this for some math operations document-wide. Looks fine so far, but if I resize my screen (what a wonder…) my whole layout collapses. Now, I played around with $(window).resize() but this does not really solve my problem: I don’t want to rewrite all those jQuery operations…. My idea? What about just rewriting the »var« during the resizing process and make that valid for the whole document? Sth. like: $(document).ready(function() {   // first declaration   var viewport = Math.floor($(window).width());   // on resize   $(window).resize(function() {     var viewport = Math.floor($(window).width());   });    // (…)  }); Seems that I’ve got a knot in my brain because this does not work (I guess the new »viewport« is not a global var)? Pretty happy if someone could help me! Have a nice christmas weekend!

  • Answer:

    The following code will store the width/height of the window whenever it is resized by the user: // I don't generally condone the practice of // declaring vars in the global context var $dims = { width: null, height: null }; $(function () { $(window).bind( "resize", function ( event ) { var $this = $(this); $.each( $dims, function (key) { $dims[ key ] = $this[ key ](); }); }); }); The dimensions are stored in the globally available `$dims` object, as `$dims.width` and `$dims.height`

Rick Waldron at Quora Visit the source

Was this solution helpful to you?

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.