简体   繁体   English

在Javascript中强制dom重新加载/调整大小

[英]Force dom reload/resize in Javascript

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. 它只是在“ body”上切换一个类,从而在内容区域增加了一些边距并隐藏/显示了侧边栏。 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? 有没有办法触发元素大小刷新或dom刷新来解决此问题? Using Chrome 19.x. 使用Chrome19.x。

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

Edit: Seems like it might be a Webkit issue. 编辑:似乎可能是Webkit问题。 Works fine in Firefox. 在Firefox中工作正常。

Edit 2: Set up a simplified build at the following location: 编辑2:在以下位置设置简化的版本:

https://dl.dropbox.com/u/189605/misc/build-test/grid.html 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. 在Webkit中,您必须调整浏览器的大小以实现更大的空间。 Works in Firefox. 在Firefox中可以使用。

您可以在点击处理程序中触发调整大小,例如:

$(window).trigger('resize')

The workaround from my answer here works for your situation. 我在这里回答的解决方法适合您的情况。

Here's a quick demo: http://jsbin.com/amakex 这是一个快速演示: http : //jsbin.com/amakex

It works in both Chrome and Safari (unsurprisingly, your original demo also didn't work in Safari). 它可以在Chrome和Safari中使用(毫不奇怪,您的原始演示在Safari中也无法使用)。

您说-“但是我需要它在切换后无需调整窗口大小”。您可以使用jquery回调来做到这一点

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM