[英]Adding Class on Scroll Vanilla JS
我正在使用Vue.js,想在用戶滾動到特定點時有條件地顯示返回頂部的按鈕。 我在做什么錯(沒有JQuery)?
在我的模板中:
<div class="scroll">
<span class="scroll_button">Top</span>
</div>
在我的mount()函數中
const toTop = document.getElementsByClassName('scroll').addEventListener('scroll', function() {
if (window.scrollY > 0) {
this.classList.add('shown')
}
});
toTop();
data () {
return {
scrolled: false
};
},
methods: {
handleScroll () {
this.scrolled = window.scrollY > 0;
}
},
created () {
window.addEventListener('scroll', this.handleScroll);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.