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

186
Vistas
Cómo hacer 2 funciones de desplazamiento diferentes

Hola, hice 2 scripts para desplazarse, uno hace que la barra de navegación sea más pequeña y el segundo muestra el botón para ir al inicio de la página. Cuando agregué el segundo (para el botón), el primero para la barra de navegación no funcionó. Eliminé el segundo y funcionó. ¿Qué necesito cambiar?

para la barra de navegación

 window.onscroll = function() { var el = document.getElementsByClassName('header')[0]; var className = 'small'; if (el.classList) { if (window.scrollY > 10) el.classList.add(className); else el.classList.remove(className); } }; window.smoothScroll = function(target) { var scrollContainer = target; do { //find scroll container scrollContainer = scrollContainer.parentNode; if (!scrollContainer) return; scrollContainer.scrollTop += 1; } while (scrollContainer.scrollTop == 0); var targetY = 0; do { //find the top of target relatively to the container if (target == scrollContainer) break; targetY += target.offsetTop; } while (target = target.offsetParent); scroll = function(c, a, b, i) { i++; if (i > 30) return; c.scrollTop = a + (b - a) / 30 * i; setTimeout(function(){ scroll(c, a, b, i); }, 20); } // start scrolling scroll(scrollContainer, scrollContainer.scrollTop, targetY, 0); }

para botón

 //Get the button var mybutton = document.getElementById("myBtn"); // When the user scrolls down 20px from the top of the document, show the button window.onscroll1 = function() {scrollFunction()}; function scrollFunction() { if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) { mybutton.style.display = "block"; } else { mybutton.style.display = "none"; } } // When the user clicks on the button, scroll to the top of the document function topFunction() { document.body.scrollTop = 0; document.documentElement.scrollTop = 0; }

HTML

 <button onclick="topFunction()" id="myBtn" title="Go to top">Top</button> <div class="header"> <div class="container"> <ul class="nav"> <li><a href="#">Home</a></li> </ul> </div> </div>
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

encontré otra solución

Algo como esto

 var mybutton = document.getElementById("myBtn"); window.onscroll = function() {scrollFunction()};{ function scrollFunction() { var el = document.getElementsByClassName('header')[0]; // Vrh button prikazivanje var className = 'small'; if (el.classList) { if (window.scrollY > 10) el.classList.add(className); else el.classList.remove(className); } if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) { mybutton.style.display = "block"; } else { mybutton.style.display = "none"; } } }; window.smoothScroll = function(target) { var scrollContainer = target; do { //find scroll container scrollContainer = scrollContainer.parentNode; if (!scrollContainer) return; scrollContainer.scrollTop += 1; } while (scrollContainer.scrollTop == 0); var targetY = 0; do { //find the top of target relatively to the container if (target == scrollContainer) break; targetY += target.offsetTop; } while (target = target.offsetParent); scroll = function(c, a, b, i) { i++; if (i > 30) return; c.scrollTop = a + (b - a) / 30 * i; setTimeout(function(){ scroll(c, a, b, i); }, 20); } // start scrolling scroll(scrollContainer, scrollContainer.scrollTop, targetY, 0); }
about 4 years ago · Juan Pablo Isaza Denunciar

0

Solo comenta la segunda window.onscroll = function() {scrollFunction()}; desde su botón y agregue scrollFunction(); en su window.onscroll de la window.onscroll1 de navegación. altura para probar el desplazamiento

 window.onscroll = function () { scrollFunction(); var el = document.getElementsByClassName("header")[0]; var className = "small"; if (el.classList) { if (window.scrollY > 10) el.classList.add(className); else el.classList.remove(className); } }; window.smoothScroll = function (target) { var scrollContainer = target; do { //find scroll container scrollContainer = scrollContainer.parentNode; if (!scrollContainer) return; scrollContainer.scrollTop += 1; } while (scrollContainer.scrollTop == 0); var targetY = 0; do { //find the top of target relatively to the container if (target == scrollContainer) break; targetY += target.offsetTop; } while ((target = target.offsetParent)); scroll = function (c, a, b, i) { i++; if (i > 30) return; c.scrollTop = a + ((b - a) / 30) * i; setTimeout(function () { scroll(c, a, b, i); }, 20); }; // start scrolling scroll(scrollContainer, scrollContainer.scrollTop, targetY, 0); }; //Get the button var mybutton = document.getElementById("myBtn"); // When the user scrolls down 20px from the top of the document, show the button //window.onscroll1 = function() {scrollFunction()}; function scrollFunction() { if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) { mybutton.style.display = "block"; } else { mybutton.style.display = "none"; } } // When the user clicks on the button, scroll to the top of the document function topFunction() { document.body.scrollTop = 0; document.documentElement.scrollTop = 0; }
 .container { height: 1999px; } #myBtn { position: fixed; bottom: 0; right: 0; display: none; }
 <button onclick="topFunction()" id="myBtn" title="Go to top">Top</button> <div class="header"> <div class="container"> <ul class="nav"> <li><a href="#">Home</a></li> </ul> </div> </div>

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