javascript - 无论调整大小如何,如何使移动元素保持一定的高度和宽度?
问题描述
我试图在我的标题中显示移动的单词,并希望无论浏览器大小如何,这些单词都保留在该特定 div 内。我在控制台记录宽度和高度,它似乎保持在参数内,但在页面上它超出了这些范围。我已经尝试过 offSetHeight/Width 和 innerHeight/innerWidth 但这些都不起作用。我正在使用 clientHeight/Width 并且在控制台中有效,但在页面上无效。我已将反弹 div 设置为高度:100vh,宽度设置为 100%。有人能指出我正确的方向吗?
Here is my code:
const bounceDiv = document.querySelector('.bounce')
for (let i = 0; i < words.length; i++) {
setInterval(function timer() {
let width = Math.floor(Math.random() * bounceDiv.clientWidth);
console.log(width + ":" + "width")
let height = Math.floor(Math.random() * bounceDiv.clientHeight);
console.log(height + ":" + "height")
words[i].style.transform = `translate(${width}px, ${height}px) `;
words[i].style.transition = '1s ease-in-out'
}, i * 2000);
}
解决方案
推荐阅读
- reactjs - ReactJS - 为什么表单元素返回 null?
- node.js - Soap 请求:TypeError:无法使用“in”运算符在 svcutil.exe 中搜索“A”
- php - 将复选框值传递给 laravel 控制器以进行删除
- java - 更改 JTabbedPane 中选定选项卡的背景颜色
- excel - VBA 中的 getElementsByClassName 产生错误:对象不支持此属性或方法
- reactjs - Material UI 组件在 react-frame-component 中丢失了样式
- postgresql - 在 postgres 中 FOR KEY SHARE 的实际用途是什么?
- firebase - 如何每周一个接一个地发送两条重复的云消息并从 Firebase 控制台再次重复?
- ios - 无法存档在 Flutter 中构建的 iOS 应用程序
- asp.net - 在 asp.net core 中上传文件