在不同的屏幕尺寸下更改 jQuery 函数
我有一个脚本正在计算屏幕宽度 -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'});
}
}
}
I have a script that is working out the width of the screen -225px however I would like to change that when the screen hits 1100px so that the function simply uses the whole width.
Here is the code I am using:
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'});
}
And this is the way it's called:
$(document).ready(function() {
slide_holder();
$(window).bind('resize', slide_holder);
});
I had thought about trying it like this but I got a big FAIL:
$(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'});
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
阅读您对想要实现的效果的描述,并假设您不打算使用基于 CSS 媒体查询的解决方案,您应该能够通过对原始功能进行(相对)最小的更改来实现
:我正在做的是在函数开头一次获取宽度和高度,而不是连续调用
$(window).width()
和$(window ).height()
,然后设置布尔值smallMode
,用于决定与如下表达式一起使用的偏移量:其中表示如果
smallMode
为true
则减去255
否则减去0
(显然你应该替换你自己的偏移量而不是0
)。阅读更新的“失败”代码,您似乎想要根据 if / else if 条件的结果重新定义
slide_holder()
函数。从一般意义上讲,这是可能的,但不适用于您使用的语法。如果您使用以下形式的函数声明:JavaScript 会将声明“提升”到包含范围的顶部,即,它假装您的声明位于开头。这意味着使用该语法(在某些浏览器中可能存在不规则性)不可能在
if
块中有条件地声明函数。实际上,if 和else
块中的函数声明都会被“提升”到顶部,并且如果您声明两个具有相同功能的函数命名第二个覆盖第一个。声明函数的另一种方法是这样的:
虽然变量
slide_holder
也会被“提升”到作用域的顶部,但直到带有赋值的行之前,它不会被分配对实际函数的引用。这意味着您可以像这样进行有条件的这个或那个函数“声明”:就像我最初所说的那样,您可以使用原始函数的版本来实现效果,但从一般意义上来说,函数声明的这种行为很可能是 ( main) 更新后的代码存在问题。
Reading your description of the effect you are trying to achieve, and assuming you're not going to go with a CSS media query based solution, you should be able to do it with a (relatively) minimal change to your original function:
What I'm doing there is getting the width and height once at the beginning of the function rather than continuously calling
$(window).width()
and$(window).height()
, and then setting a booleansmallMode
that is used to decide what offset to use with an expression like this:Which says if
smallMode
istrue
subtract255
otherwise subtract0
(obviously you should substitute your own offsets rather than0
).Reading your updated "fail" code, you seem to want to redefine the
slide_holder()
function depending on the results of the if / else if conditions. In a general sense that is possible, but not with the syntax you are using. If you use a function declaration of the form:JavaScript "hoists" the declaration to the top of the containing scope, i.e., it pretends that your declaration was at the beginning. This means it is not possible with that syntax (subject to irregularities in some browsers) to conditionally declare functions in an
if
block. Effectively the function declarations from with both theif
and theelse
blocks get "hoisted" to the top and if you declare two functions with the same name the second overrides the first.Another way to declare a function is like this:
Although the variable
slide_holder
would also be "hoisted" to the top of the scope it would not be assigned a reference to the actual function until the line with the assignment. Which means you can do a conditional this or that function "declaration" like this:Like I said initially, you can achieve the effect with a version of your original function, but in a general sense this behaviour of function declarations is most likely the (main) problem with your updated code.