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>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); }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>