简体   繁体   English

如何在滚动上为 Div 设置动画

[英]How to animate a Div on Scroll

I have been trying to figure out how to rotate a div based off of my scrolling.我一直在试图弄清楚如何根据我的滚动来旋转div I have a codepen that demonstrates the behaviour that I would like to achieve.我有一个代码笔来演示我想要实现的行为。

However, I do not know hot to make it work with scrolling.但是,我不知道如何让它与滚动一起工作。 Essentially, I would like to scroll down and have the word Data move in a counter-clockwise rotation.本质上,我想向下滚动并让Data一词逆时针旋转。 And if I decide to scroll up and down it should move with the speed of the scroll.如果我决定上下滚动,它应该随着滚动的速度移动。 I only want it to move 90deg up or down.我只希望它向上或向下移动 90 度。 Thanks!谢谢!

 var btn = document.querySelector('.button'); var btnUp = document.querySelector('.button-up') btnUp.addEventListener('click', function() { document.querySelector(".parent").style.webkitTransform = "rotate(-90deg)"; document.querySelector(".child").style.webkitTransform = "rotate(90deg)"; }); btn.addEventListener('click', function() { document.querySelector(".parent").style.webkitTransform = "rotate(0deg)"; document.querySelector(".child").style.webkitTransform = "rotate(0deg)"; });
 .parent { position: relative; width: 300px; height: 300px; border-radius: 50%; transform: rotate(-0deg); transition: transform 0.7s linear; margin: 100px auto 30px auto; } .child { position: absolute; transform: rotate(-0deg); transition: transform 0.7s linear; border-radius: 50%; width: 40px; height: 40px; text-align: center; top: 278px; /* -child size/2 */ left: 130px; /* parent size/2 - child size/2 */ }
 <div class="parent"> <div class="child"> Data </div> </div> <button class="button">Click Down</button> <button class="button-up">Click Up</button>

Add添加

<link href="https://unpkg.com/aos@2.3.1/dist/aos.css" rel="stylesheet">
<script src="https://unpkg.com/aos@2.3.1/dist/aos.js"></script>

and in script并在脚本中

<script>
  AOS.init();
</script>

then in div然后在div

<div data-aos="fade-down"></div> //you can use whatever you want's

for further information了解更多信息

this website helps you on how to make it work 该网站可帮助您了解如何使其工作

and

You can find out more at AOS Animation您可以在AOS 动画中找到更多信息

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

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