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

160
Views
Ejecutar ventana al cargar solo si existe clase

Necesito ejecutar $(window).on('load', function() solo si existe cierta clase en un elemento DOM (div).

Mi código actual es el siguiente:

 jQuery(document).ready(function($) { if (window.innerWidth > 767) { var $sticky = $('#sticky'); var $stickyrStopper = $('.footer'); if (!!$sticky.offset()) { // make sure ".sticky" element exists var generalSidebarHeight = $sticky.innerHeight(); var stickyTop = $sticky.offset().top; var stickOffset = 0; var stickyStopperPosition = $stickyrStopper.offset().top; var stopPoint = stickyStopperPosition - generalSidebarHeight - stickOffset; var diff = stopPoint + stickOffset; $(window).scroll(function(){ // scroll event var windowTop = $(window).scrollTop(); // returns number ////////////// if (stopPoint < windowTop) { $sticky.css({ position: 'absolute', top: diff }); } else if (stickyTop < windowTop+stickOffset) { $sticky.css({ position: 'fixed', top: stickOffset }); } else { $sticky.css({position: 'absolute', top: 'initial'}); }///////////// }); } } });

Y esta es mi idea, si la clase existe:

 jQuery(document).ready(function($) { if (window.innerWidth > 767) { if ($(".classexists")[0]){

Donde estaría el elemento:

 <div class="classexists">123</div>

Ejecute window on load antes de la función principal:

 $(window).on('load', function() {

si la clase no existe, en realidad no ejecute $(window).on('load', function() { , simplemente ejecute el resto del código normalmente.

Se supone que este código crea una barra lateral pegajosa.

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

0

Si estoy entendiendo tu problema correctamente, prueba esto:

 function example() { var $sticky = jQuery('#sticky'); var $stickyrStopper = jQuery('.footer'); if (!!$sticky.offset()) { // make sure ".sticky" element exists var generalSidebarHeight = $sticky.innerHeight(); var stickyTop = $sticky.offset().top; var stickOffset = 0; var stickyStopperPosition = $stickyrStopper.offset().top; var stopPoint = stickyStopperPosition - generalSidebarHeight - stickOffset; var diff = stopPoint + stickOffset; jQuery(window).scroll(function () { // scroll event var windowTop = jQuery(window).scrollTop(); // returns number ////////////// if (stopPoint < windowTop) { $sticky.css({ position: 'absolute', top: diff }); } else if (stickyTop < windowTop + stickOffset) { $sticky.css({ position: 'fixed', top: stickOffset }); } else { $sticky.css({ position: 'absolute', top: 'initial' }); }///////////// }); } } if (window.innerWidth > 767) { if (jQuery('.classexists').length > 0) { jQuery(window).on('load', function () { example(); }); } else { example(); } }
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!