javascript - 根据屏幕大小重新定位元素
问题描述
我需要根据浏览器大小在页面上放置一个元素。我无法隐藏和显示此元素,因为它只能在 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);
}
}
推荐阅读
- java - 在同一个套接字上的文件之后发送字符串
- java - 在 finally 块内不接受输入
- angular - 如果我更改可观察对象,异步管道是否会自动取消订阅可观察对象?
- reactjs - npm 错误!404 未找到 - 获取 https://registry.npmjs.org/@typescript-eslint%2feslint-plugin - 未找到
- rust - 在 impl 中为带有阴影方法名称的 trait 指定方法实现
- asp.net-core-webapi - Swagger UI 响应类型返回 Status (400) 400BadRequest,而不是 (201) Status201Created 但记录插入成功
- sql - 即使第二个表中缺少 id,也可以加入 2 个表
- flutter - 在颤振中我们如何使用audio_service来获取动态数据
- python-3.x - 如何在 Python 结果中添加 ( 和 )
- c++ - 为什么在 C++ 中返回一个虚拟类 *by value* 会改变 v-table?