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

304
Vistas
show and hide toTopButton according the scrolling but window.onscroll not work ..i can not fix the real problem

Please I need your support as I write this code to create a button and when I press it I move to the top of page ...the button is created good and move to top of page ... But I want the button hide when the scrolling in the top of page and appear again when I scroll down ...the scroll function not work I do not know the reason … I support you with the code in JavaScript and CSS related to my question …you are genius if you can fix it.

First JavaScript code

//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";
  }
}

CSS code

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

Note : when I change in CSS code and make display: none; the button hidden and when I change it to display: block; it appear again …this is not problem ...the real problem that the (to top button) not appear and hidden according to the scrolling ... I think the scroll function not work. Thanks for your support in advance.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Change this part of your code

// 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";
  }
}

To this

// 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";
  }
}

Note:

  • Attach the event to the document object
  • Look for the scrollingElement (it might not be the body element)
about 4 years ago · Juan Pablo Isaza Denunciar

0

The main problem in my code was the variable (toTopButton) this variable defined in the function ,so it is not declared or defined out the function ...when I create it a gain in the global area ...the function worked good

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