【发布时间】:2012-02-29 12:59:17
【问题描述】:
我有一个脚本正在计算屏幕的宽度 -225px,但是我想在屏幕达到 1100px 时更改它,以便函数只使用整个宽度。
这是我正在使用的代码:
function slide_holder(){
$('#main').css({'width': (($(window).width()) - 225 )+'px'});
$('.flexslider-container').css({'height': (($(window).height()) - 85 )+'px', 'width': (($(window).width()) - 225 )+'px'});
$('.flexslider').css({'height': (($(window).height()) - 275 )+'px'});
$('#tS3').css({'height': (($(window).height()) - 200 )+'px'});
$('#foot').css({'width': (($(window).width()) - 325 )+'px'});
}
这就是它的称呼:
$(document).ready(function() {
slide_holder();
$(window).bind('resize', slide_holder);
});
我曾想过这样尝试,但我失败了:
$(window).bind("resize", resizeWindow);
function resizeWindow(e){
var newWindowWidth = $(window).width();
// If width width is below 600px, switch to the mobile stylesheet
if(newWindowWidth < 1100){
function slide_holder(){
$('#main').css({'width': (($(window).width()) - 0 )+'px'});
$('.flexslider-container').css({'height': (($(window).height()) - 85 )+'px', 'width': (($(window).width()) - 0 )+'px'});
$('.flexslider').css({'height': (($(window).height()) - 275 )+'px'});
$('#tS3').css({'height': (($(window).height()) - 200 )+'px'});
$('#foot').css({'width': (($(window).width()) - 105 )+'px'});
}
}
// Else if width is above 600px, switch to the large stylesheet
else if(newWindowWidth > 1100){
function slide_holder(){
$('#main').css({'width': (($(window).width()) - 225 )+'px'});
$('.flexslider-container').css({'height': (($(window).height()) - 85 )+'px', 'width': (($(window).width()) - 225 )+'px'});
$('.flexslider').css({'height': (($(window).height()) - 275 )+'px'});
$('#tS3').css({'height': (($(window).height()) - 200 )+'px'});
$('#foot').css({'width': (($(window).width()) - 325 )+'px'});
}
}
}
【问题讨论】:
-
你最好使用 css 媒体查询...
-
“但是我失败了” - 在什么意义上?您是否收到错误消息,它只是没有运行,它是否运行但做错了?
-
我同意 elclanrs。去年我参加了一个设计会议,其中一位演讲者讨论了“响应式网页设计”,即创建一个响应正在呈现的设备的站点。通过使用 elclanrs 评论的媒体查询,您可以轻松构建一个响应浏览器更改的页面。查看此 URL,了解一些人对响应式 Web 设计所做的一些示例:designmodo.com/responsive-design-examples 虽然它没有回答您的问题,但它确实让您对即将到来的网络设计运动有所了解
标签: jquery resize width media-queries window-resize