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

299
Views
mostrar y ocultar toTopButton de acuerdo con el desplazamiento, pero window.onscroll no funciona ... no puedo solucionar el problema real

Necesito su apoyo mientras escribo este código para crear un botón y cuando lo presiono me muevo a la parte superior de la página... el botón se crea bien y se mueve a la parte superior de la página... Pero quiero que el botón se oculte cuando el desplazamiento en la parte superior de la página y aparece de nuevo cuando me desplazo hacia abajo ... la función de desplazamiento no funciona, no sé la razón ... Te apoyo con el código en JavaScript y CSS relacionado con mi pregunta ... eres un genio si puedes arreglalo.

Primer código JavaScript

 //here i create a function to make scroll smooth...i can write it in css file but i want to show what is i lrarned: const scrollSmoothly = function () { const myHTML = document.querySelector("html"); myHTML.style.scrollBehavior = "smooth"; }; //create the to Top Button // scrollSmoothly(); function toTopButton_create() { const toTopButton = document.createElement("botton"); const textnodeTOP = document.createTextNode("TO_Top"); toTopButton.appendChild(textnodeTOP); //document.querySelector("footer").appendChild(toTopButton); const rr = document.querySelector("footer"); rr.insertAdjacentElement("beforebegin", toTopButton); toTopButton.setAttribute("id", "ourBtn"); toTopButton.addEventListener("click", test2); } toTopButton_create(); //below function to go to the top of sheet function test2() { document.body.scrollTop = 0; document.documentElement.scrollTop = 0; } // below function to hide the (to top button) in the top and appear again when we scroll down window.onscroll = function () { scrollFunction(); }; function scrollFunction() { if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) { toTopButton.style.display = "block"; } else { toTopButton.style.display = "none"; } }

codigo css

 #ourBtn { padding: 16px; right: 31px; bottom: 10px; font-size: 19px; z-index: 98; border-radius: 5px; border: none; outline: none; display: block; background-color: rgb(17, 199, 231); color: rgb(29, 2, 2); position: fixed; cursor: pointer; } #ourBtn:hover { background-color: rgb(219, 8, 8); }

Nota: cuando cambio el código CSS y hago display: none; el botón oculto y cuando lo cambio para display: block; aparece de nuevo... esto no es un problema... el verdadero problema es que (el botón superior) no aparece y se oculta de acuerdo con el desplazamiento... Creo que la función de desplazamiento no funciona. Gracias por su apoyo de antemano.

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

0

Cambia esta parte de tu código

 // below function to hide the (to top button) in the top and appear again when we scroll down window.onscroll = function () { scrollFunction(); }; function scrollFunction() { if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) { toTopButton.style.display = "block"; } else { toTopButton.style.display = "none"; } }

A esto

 // below function to hide the (to top button) in the top and appear again when we scroll down document.onscroll = scrollFunction; function scrollFunction(event) { if (event.srcElement.scrollingElement.scrollTop > 20 || document.documentElement.scrollTop > 20) { toTopButton.style.display = "block"; } else { toTopButton.style.display = "none"; } }

Nota:

  • Adjunte el evento al objeto del documento
  • Busque el scrollingElement (puede que no sea el elemento del body )
about 4 years ago · Juan Pablo Isaza Report

0

El problema principal en mi código era la variable (toTopButton) esta variable definida en la función, por lo que no se declara ni se define fuera de la función... cuando la creo una ganancia en el área global... la función funcionó bien

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!