Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

170
Views
Using pure javaScript without "onMount" in Sveltekit framework

Is there a way to using simple JavaScript without using onMount ?
How to improve this code and don't use onMount?
(I'd like to shoe each content box after user scrolls)
enter image description here

<script>
    import { onMount } from 'svelte';
    onMount(() => {
        const boxes = document.querySelectorAll('.content');
        window.addEventListener('scroll', checkBoxes);
        checkBoxes();
        function checkBoxes() {
            const triggerBottom = (window.innerHeight / 5) * 4;
            boxes.forEach((box) => {
                const boxTop = box.getBoundingClientRect().top;

                if (boxTop < triggerBottom) {
                    box.classList.add('show');
                } else {
                    box.classList.remove('show');
                }
            });
        }
    });
</script>

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Assuming your content boxes are generated in an array you could use a combination of bind:this and svelte:window

<script>
 let boxes = [];
 let content = [];

 function checkBoxes() {
   // use boxes here
 }
</script>

<svelte:window on:scroll={checkBoxes} />

{#each content as c, i}
 <div bind:this={boxes[i]}>
   ....
 </div>
{/each}

Alternatively, look into actions and IntersectionObservers

about 4 years ago · Juan Pablo Isaza Report

0

How about -

document.addEventListener("DOMContentLoaded", function(){
    // your code here will be executed on page ready/load
});
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!