Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

300
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda