简体   繁体   English

如何使元素顺利向上滑动?

[英]How to make an element slide up the page smoothly?

I have the following code that contains positioned hidden div (.converter) and I use JS to make it slide up and down based on the user's scroll. 我有以下代码,其中包含定位的隐藏div(.converter),我使用JS使其根据用户的滚动向上和向下滑动。 But I would like to make it smoothly and I thought transition would do the job. 但我想顺利完成,我认为过渡会做得很好。

 window.addEventListener('scroll', function() { var scroll = window.pageYOffset || document.documentElement.scrollTop; var element = document.querySelector(".converter"); if (scroll >= 400) { element.classList.add("atcbottomactive"); } else { element.classList.remove("atcbottomactive"); } }); 
 .converter { position: fixed; height: 60px; width: 100%; bottom: -200; background: red; transition: 1s; z-index: 10000; } .ccontent { display: inline-flex; width: 100%; padding: 10px 5%; } .atcbottomactive{ bottom:0; transition: 1s; } .page { background: green; height: 1500px; width: 100%; } 
 <div class="page">Scroll me: 400px</div> <div class="converter"> <div class="ccontent"></div> </div> 

Thanks in advance 提前致谢

So close! 很近! All you were missing was the 'px' on your .converter class bottom attribute. 您所缺少的只是.converter类底部属性上的'px'。 Because -200 isn't a valid bottom, you were going from an unset bottom to 0px, which cant be animated/ transitioned. 因为-200不是一个有效的底部,你是从一个未设置的底部到0px,它不能被动画/转换。

 window.addEventListener('scroll', function() { var scroll = window.pageYOffset || document.documentElement.scrollTop; var element = document.querySelector(".converter"); if (scroll >= 400) { element.classList.add("atcbottomactive"); } else { element.classList.remove("atcbottomactive"); } }); 
 .converter { position: fixed; height: 60px; width: 100%; bottom: -200px; background: red; transition: 1s; z-index: 10000; } .ccontent { display: inline-flex; width: 100%; padding: 10px 5%; } .atcbottomactive{ bottom:0; transition: 1s; } .page { background: green; height: 1500px; width: 100%; } 
 <div class="page">Scroll me: 400px</div> <div class="converter"> <div class="ccontent"></div> </div> 

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

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