[英]Javascript: Code doesn't get executed when put into function
I have this working Javascript code which does some resizing on init
and resize
: 我有这个工作的Javascript代码,它对
init
和resize
一些resize
:
(function (w) {
w.init = function () {
var w = window.innerWidth;
var h = window.innerHeight;
//resize_font;
var fs = parseInt(w / 26);
document.body.style.fontSize = fs + "px";
//resize_cover;
var logo = get_by_id("logo");
var logo_height = logo.clientHeight;
var menu = get_by_id("menu");
var menu_height = menu.clientHeight;
var cover_height = logo_height + menu_height;
var distance_top = (h - cover_height) / 2.5;
var container = get_by_id("container");
container.style.paddingTop = distance_top + "px";
}
w.resize = function () {
var w = window.innerWidth;
var h = window.innerHeight;
//resize_font;
var fs = parseInt(w / 26);
document.body.style.fontSize = fs + "px";
//resize_cover;
var logo = get_by_id("logo");
var logo_height = logo.clientHeight;
var menu = get_by_id("menu");
var menu_height = menu.clientHeight;
var cover_height = logo_height + menu_height;
var distance_top = (h - cover_height) / 2.5;
var container = get_by_id("container");
container.style.paddingTop = distance_top + "px";
}
}
Now I want to refactor the code for resizing font and cover into a function, but I can't get it working. 现在,我想重构用于调整字体大小和覆盖范围的代码,但是无法正常工作。 I tried this:
我尝试了这个:
(function (w) {
w.init = function () {
var w = window.innerWidth;
var h = window.innerHeight;
resize_font;
resize_cover;
}
w.resize = function () {
var w = window.innerWidth;
var h = window.innerHeight;
resize_font;
resize_cover;
}
function resize_cover() {
var logo = get_by_id("logo");
var logo_height = logo.clientHeight;
var menu = get_by_id("menu");
var menu_height = menu.clientHeight;
var cover_height = logo_height + menu_height;
var distance_top = (h - cover_height) / 2.5;
var container = get_by_id("container");
container.style.paddingTop = distance_top + "px";
}
function resize_font() {
var fs = parseInt(w / 26);
document.body.style.fontSize = fs + "px";
}
}
The console doesn'T show any errors, but resizing isn't happening. 控制台不会显示任何错误,但不会调整大小。 What am I doing wrong?
我究竟做错了什么?
You need to invoke your functions in your init and resize event functions. 您需要在init和resize事件函数中调用函数。 You have:
你有:
resize_font;
resize_cover;
You need: 你需要:
resize_font();
resize_cover();
The name of a function is just a reference to it, which is why you don't see any errors. 函数的名称只是对该函数的引用,这就是为什么看不到任何错误的原因。 What you currently have is similar to saying:
您目前所拥有的类似于:
var x = 1;
x;
Looks like your problem is this: 看来您的问题是这样的:
resize_font;
resize_cover;
I think you mean 我想你是说
resize_font();
resize_cover();
It's seems like you forgot to execute the functions, just mentioned the pointer. 似乎您忘记了执行功能,只是提到了指针。
please try to change: 请尝试更改:
resize_font;
resize_cover;
to 至
resize_font();
resize_cover();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.