javascript - 在滚动时移动 DomElements 时进入滚动循环
问题描述
我正在构建一个虚拟网格,它将根据需要将内容放置到视口中,尤其是在用户滚动时。
我已经能够使用 React 实现这种行为,但我似乎在使用 svelte 时遇到了麻烦。
<script>
let scroll;
$: height = Math.floor(scroll/200)*200;
</script>
<svelte:window bind:scrollY={scroll} />
<div class="holder" style="height:{height}px"></div>
<div class="box"></div>
<style>
:global(body) {
height: 20000px;
}
.box {
width: 200px;
height: 200px;
background-color: aqua;
}
.holder {
}
</style>
当我滚动时,底部 div 的高度增加以提升蓝色框,某些东西触发了进一步的滚动,然后触发了进一步的高度增加,从而触发了进一步的滚动。
所以基本上当你滚动一点时,滚动条会变得疯狂并在电脑上向下滑动。
理想情况下,页面应该从用户输入正常滚动。
不确定这是否是 svelte 的问题,或者这是否是某些默认浏览器行为。
编辑:更改代码和 repl 以更多地反映我的要求,这说明了为什么设置“位置:固定”css 不起作用。
解决方案
在您的代码中设置 .holder 的高度会改变滚动。所以它陷入连续循环直到触底。
<script>
let scroll;
$: top = Math.floor(scroll/200)*200;
</script>
<svelte:window bind:scrollY={scroll} />
<div class="box" style="top:{top}px"></div>
<style>
:global(body) {
height: 20000px;
}
.box {
position: absolute;
top: 0;
width: 200px;
height: 200px;
background-color: aqua;
}
</style>
推荐阅读
- ruby-on-rails - 将 twitter bootstrap gem 从 2.x 升级到 3.x 或 4.x 会破坏 UI 样式
- javascript - 数据不显示。错误:无法获取/表达 API
- angular - 试图将事件从父级传递给子级不起作用。角 8
- node.js - /bin/sh: =: 从自定义映像运行 docker 容器时未找到
- java - 在 tomat 上部署的根映射(“/”)上的 Spring Boot 404。(轻松依赖问题)
- python - 如何修剪 keras ResNet50 预训练权重(.h5 文件)?我只有 .h5 权重文件。(我已经成功加载了 keras 权重)
- c# - 如何在 C# 中评估包含局部变量名称的字符串作为代码
- sql-server - SQL:消息“无效的对象名称”是什么意思?
- java - 底部导航图标高度 Android
- angular - 如何安装较低版本的 Angular Material UI