繁体   English   中英

更改滚动方向时使div旋转180度

[英]Make a div rotate 180 deg when changing scroll direction

我正在制作一个网页,其中此插图沿页面向下移动,当我更改滚动方向时,我希望它旋转180度以匹配滚动方向。 在互联网上找不到任何内容可以帮助我编写此行JS。 我对JS还是很陌生,想通过实践学习它。

尝试先确定滚动方向,然后每当滚动发生变化时:

 let direction = null; let oldScroll = 0; window.onscroll = function(e) { // print "false" if direction is down and "true" if up if(this.oldScroll > this.scrollY) { if(direction !== "up") { document.getElementById("box").style.transform = "rotate(180deg)"; } direction = "up" } else { if(direction !== 'down'){ document.getElementById("box").style.transform = ""; } direction = "down"; } this.oldScroll = this.scrollY; } 
 .box{ transition: all 2s; height: 50px; width: 150px; background-color: red; } .container{ height:1000px; } 
 <div class="container"> <div class="box" id="box"></div> </div> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM