gpt4 book ai didi

Javascript:代码在放入函数时不会被执行

转载 作者:行者123 更新时间:2023-11-28 12:22:06 25 4
gpt4 key购买 nike

我有这个有效的 Javascript 代码,它对 initresize 进行了一些调整:

(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";
}
}

现在我想将调整字体大小和封面的代码重构为一个函数,但是我无法让它工作。我试过这个:

(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";
}
}

控制台没有显示任何错误,但没有发生调整大小。我做错了什么?

最佳答案

您需要在 init 和 resize 事件函数中调用函数。你有:

resize_font;
resize_cover;

您需要:

resize_font();
resize_cover();

函数的名称只是对它的引用,这就是为什么您看不到任何错误。您当前所拥有的内容类似于:

var x = 1;
x;

关于Javascript:代码在放入函数时不会被执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35734937/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com