• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

120
Views
Observador de intersección para jquery.

Quiero usar el observador de intersección en mi proyecto y quiero usar jquery cómo funciona el observador de intersección en jQuery. Traté de pasar el elemento jQuery en la función de observación pero no funcionó.

 const aboutUsObserver = new IntersectionObserver((entries, observer) => { entries.forEach(entry => { if(entry.isIntersecting) { $(".active").removeClass("underlined"); $("#aboutUsNavItem").toggleClass("underlined"); } else { $(".active").removeClass("underlined"); } }); }, {}); aboutUsObserver.observe($("#about-us-section"));
almost 3 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Debe pasar un elemento real al llamar a observe() , y no un objeto jQuery. Puede acceder al elemento subyacente de un objeto jQuery usando .get() o [0] :

 // Option 1: aboutUsObserver.observe($("#about-us-section").get()); // Option 2: aboutUsObserver.observe($("#about-us-section")[0]);

Aún mejor: ¿ realmente necesitas jQuery aunque?

 // Use document.querySelector aboutUsObserver.observe(document.querySelector("#about-us-section")); // Or use document.getElementById aboutUsObserver.observe(document.getElementById("about-us-section"));
almost 3 years ago · Juan Pablo Isaza Report

0

Simplemente agregue [0] al final del selector.

 const aboutUsObserver = new IntersectionObserver((entries, observer) => { entries.forEach(entry => { if(entry.isIntersecting) { console.log("Here!") $(".active").removeClass("underlined"); $("#aboutUsNavItem").toggleClass("underlined"); } else { $(".active").removeClass("underlined"); } }); }, {}); aboutUsObserver.observe($("#about-us-section")[0]);
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="about-us-section" style="position:absolute; top:1000px">test</div>

almost 3 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 Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error