在 HTML5 响应式布局中动态调用 jQuery 函数
我正在 Drupal 中使用响应式主题(Omega),它可以检测浏览器尺寸并根据为某些屏幕尺寸(700px+、900px+ 和 1100px+)提供的 CSS 更改页面布局。
我相信它是通过添加媒体查询和/或向 body 标记添加 css 类来实现的。
我有一个稍微不同的函数,我想调用它来响应浏览器窗口大小的变化,但它必须响应,IE 调用相关函数而不重新加载页面
可以这样做吗?
我目前有这样的东西:
$(window).resize(function(){
var width = $("body").width();
if(width>1100)
{
$('.accordion').gridAccordion({width:1160, height:526, columns:4, distance:2, closedPanelWidth:50, closedPanelHeight:50, alignType:'centerCenter', slideshow:true,
panelProperties:{
}});
}
if(width>900)
{
$('.accordion').gridAccordion({width:920, height:417, columns:4, distance:2, closedPanelWidth:50, closedPanelHeight:50, alignType:'centerCenter', slideshow:true,
panelProperties:{
}});
}
});
这样做的问题是,在我调整窗口大小之前,它不会加载我的 div,并且它还需要刷新才能识别窗口已调整大小。
我很感激我能得到的任何帮助,提前致谢。
编辑
所以我修改了我的代码,使其看起来像这样
$(window).bind('resize load',function(){ var width = $("#zone-header").width(); if(width>1100)...
现在已经解决了 div 未立即加载的问题,但它仍然无法解决我的调整大小问题,除非我调整大小然后刷新页面。我情不自禁地觉得自己已经非常接近解决方案了,但就是不知道接下来该何去何从。
I am working with a responsive theme (Omega) in Drupal which detects browser size and changes the page layout based on CSS provided for certain screen sizes (700px+, 900px+ and 1100px+).
It does this I believe by adding media queries and/or adding css classes to the body tag.
I have a slightly varying function that I want called in reaction to browser window size changes, but it has to be responsive, I.E. calls the relative function without a page reload
Can this be done?
I currently have something like this:
$(window).resize(function(){
var width = $("body").width();
if(width>1100)
{
$('.accordion').gridAccordion({width:1160, height:526, columns:4, distance:2, closedPanelWidth:50, closedPanelHeight:50, alignType:'centerCenter', slideshow:true,
panelProperties:{
}});
}
if(width>900)
{
$('.accordion').gridAccordion({width:920, height:417, columns:4, distance:2, closedPanelWidth:50, closedPanelHeight:50, alignType:'centerCenter', slideshow:true,
panelProperties:{
}});
}
});
The problem with this is it doesn't load my div until I resize the window, and it also requires a refresh before it recognises the window has been resized.
I appreciate any help I can get on this, thanks in advance.
EDIT
So I've modified my code to look like this
$(window).bind('resize load',function(){
var width = $("#zone-header").width();
if(width>1100)...
This now has now fixed the issue with the div not loading right away, but it still doesn't fix my resizing issue unless I resize and then refresh the page. I can't help but feel that I'm so close to a resolution, but just don't know where to go from here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最初不被调用的问题可以通过最初自己调用来轻松解决:
就需要刷新屏幕而言,不应该是这样。我怀疑问题出在您使用的插件如果多次调用则无法正常工作,但我不熟悉该插件。
The not being called initially can easily be solved by simply calling it yourself initially:
As far as needing to refresh the screen, that should not be the case. I would suspect the issue is with plugin you are using not working properly if called multiple times, but I am unfamiliar with that plugin.