显示滚动向上时隐藏在滚动向下时的对象。

huangapple go评论84阅读模式
英文:

Displaying back on scroll up an object hidden on scroll down

问题

I've been successful on hiding a div on scroll, but I thought the code would display the div back again on scrolling back, but it does not. How can I do that?

<body>
  <div class="imagem-capa">
    <img src="https://www.collinsdictionary.com/images/full/apple_158989157.jpg"></div>
  <div class="random"></div>
<script src="pen.js"></script>
</body>
.random {
  background-color: red;
  height: 500px;
}

.imagem-capa img {
  height: 500px;
}
let capa = document.getElementsByClassName("imagem-capa");
console.log(capa);

document.addEventListener("scroll", function () {
  let scrollPosition = window.pageYOffset;

  if (scrollPosition <= 50) {
    capa[0].style.display = 'block' - scrollPosition / 50;
  } else {
    capa[0].style.display = 'none'  
  }
});
英文:

I've been successful on hiding a div on scroll, but I thought the code would display the div back again on scrolling back, but it does not. How can I do that?

<body>
  <div class="imagem-capa">
    <img src="https://www.collinsdictionary.com/images/full/apple_158989157.jpg"></div>
  <div class="random"></div>
<script src="pen.js"></script>
</body>
.random {
  background-color: red;
  height: 500px;
}

.imagem-capa img {
  height: 500px;
}
let capa = document.getElementsByClassName("imagem-capa");
console.log(capa);

document.addEventListener("scroll", function () {
  let scrollPosition = window.pageYOffset;

  if (scrollPosition <= 50) {
    capa[0].style.display = 'block' - scrollPosition / 50;
  } else {
    capa[0].style.display = 'none'  
  }
});

答案1

得分: 1

你的JavaScript代码中有一个错误,请移除 - scrollPosition / 50

if (scrollPosition <= 50) {
    capa[0].style.display = 'block';
} else {
    capa[0].style.display = 'none';
}
英文:

you made mistake in your js code, remove - scrollPosition / 50.

 if (scrollPosition &lt;= 50) {
capa[0].style.display = &#39;block&#39;;
} else {
 capa[0].style.display = &#39;none&#39;  
}

huangapple
  • 本文由 发表于 2023年2月24日 09:29:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75551870.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定