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
Related Q & A:
- What's a fast way to lose weight?Best solution by Yahoo! Answers
- Is there a way to watch television over the internet?
- Is there any way to watch today's "The View" online anywhere?Best solution by hulu.com
- What's a fast way to lose 15 pounds?Best solution by Yahoo! Answers
- Is there a way to watch current football games online?Best solution by ChaCha
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
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.