根据屏幕大小重新定位元素



我需要根据浏览器的大小在页面上放置一个元素。我不能隐藏和显示这个元素,因为它只能在dom。看起来很简单,但我被困住了。它似乎起了部分作用。我需要做一些分离/附加吗?对不起,我已经有一段时间没有使用纯JS了。

srPos();
window.onresize = srPos();
function srPos() {
var ww = window.innerWidth,
sr = document.getElementById("gcs-box"),
pp = document.getElementById("primary-panel"),
sp = document.getElementById("secondary-panel");

if (ww > 991) {
sp.prepend(sr);
} else {
pp.prepend(sr);
}        
}

我错过了什么?

您正在将srPos返回的值而不是函数本身赋值给onresize属性。

去掉括号:

srPos();
window.onresize = srPos;
function srPos() {
var ww = window.innerWidth,
sr = document.getElementById("gcs-box"),
pp = document.getElementById("primary-panel"),
sp = document.getElementById("secondary-panel");

if (ww > 991) {
sp.prepend(sr);
} else {
pp.prepend(sr);
}        
}

最新更新