1

I have a sidebar in my app that can be hidden/shown via a toggle button. It simply toggles a class on "body" that adds some margin left to the content area and hides/shows the sidebar. Trouble is that the content area isn't resizing its child content when this is toggled. Once I adjust the size of the browser, the content area adjusts to fit the content, but I need it to do this after the toggle without the need to resize the window. Is there a way to trigger an element size refresh or dom refresh to solve this issue? Using Chrome 19.x.

 $('#sidebar-toggle').click(function(event) {
   event.preventDefault();
   $('body').toggleClass('with-sidebar-left');
 });

Edit: Seems like it might be a Webkit issue. Works fine in Firefox.

Edit 2: Set up a simplified build at the following location:

https://dl.dropbox.com/u/189605/misc/build-test/grid.html

You can see the boxes are float: left and when you minimize the sidebar using the little arrow button, it should adjust the right so more boxes will fit. In Webkit, you have to resize the browser for it to realize it's got more space. Works in Firefox.

9
  • 3
    Can you provide a JSFiddle.net example of the problem? Commented Jun 12, 2012 at 21:46
  • Are you sure $('sidebar-toggle') is correct? Maybe it should be $('#sidebar-toggle') instead? Commented Jun 12, 2012 at 21:47
  • @thirtydot yeah, it should, but that's not the issue. typo. Commented Jun 12, 2012 at 21:57
  • 1
    I've seen something similar but only on chrome whereby the repaint engine needs to be set, but thats also only if youre using transforms in css Commented Jun 12, 2012 at 23:02
  • 1
    heres a couple of links: stackoverflow.com/questions/4641522/… stackoverflow.com/questions/8840580/… Commented Jun 12, 2012 at 23:16

3 Answers 3

7

you could just trigger a resize in your click handler, eg:

$(window).trigger('resize')
Sign up to request clarification or add additional context in comments.

5 Comments

Instead of triggering an event, the listener function can be directly called.
Has no effect, unfortunately. That was the first thing I tried.
@MaxArt if there are multiple listener functions then it's better to trigger the event. That way they all fire.
Oh dear, I really hope there aren't multiple onresize listeners :)
I had a use-case this applied to and it solved my problem so thanks!
2

The workaround from my answer here works for your situation.

Here's a quick demo: http://jsbin.com/amakex

It works in both Chrome and Safari (unsurprisingly, your original demo also didn't work in Safari).

Comments

1

you said-"but I need it to do this after the toggle without the need to resize the window".you can use jquery callback to do that

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.