Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

124
Vistas
Getting into a scrolling loop when shifting DomElements on scroll

I'm building a virtual grid of sorts, that will place content into the viewport as it's required, particularly on user scrolling.

I've been able to achieve this kind of behavior with React, but I seem to be having trouble with 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>

REPL

As I scroll, and the bottom div is increased in height to raise the blue box, something triggers a further scroll, which then triggers a further height increase which triggers a further scroll.

So basically as you scroll a little the scroll bar goes crazy and slides down on pc.

Ideally the page should scroll normally from user inpu.

Not sure if this is a problem with svelte, or if this is some default browser behavior.

Edit: change code and repl to reflect my requirement a bit more, which shows why setting "position: fixed" css wouldn't work.

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Remove the .holder and instead use position: fixed and top: 8px.

<script>
    let scroll;
</script>

<svelte:window bind:scrollY={scroll} />

<div class="box" style="position:fixed;top:8px"></div>

<style>
    :global(body) {
        height: 20000px;
    }
    .box {
        width: 200px;
        height: 200px;
        background-color: aqua;
    }
</style>
about 4 years ago · Juan Pablo Isaza Denunciar

0

In your code setting the height of the .holder changes the scroll. So it falls in continuous loop till meets bottom.

<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>
about 4 years ago · Juan Pablo Isaza Denunciar

0

If you need to increase the height, you can do it like follow code.

<script>
    let scroll;
    $: height = Math.floor((scroll - 1)/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>
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda