实列:
<div :class="['jx_box',isFixed&&'fixed-nav']"> //添加动态class
<div class="jx_bx">精选</div>
</div>
js部分:
const isFixed = ref(false);
const handleScroll = () => {
const scrollPosition = window.scrollY;
isFixed.value = scrollPosition > 300;
console.log(scrollPosition,isFixed.value);
};
onMounted(() => {
window.addEventListener('scroll', handleScroll);
});
onUnmounted(() => {
window.removeEventListener('scroll', handleScroll);
});
样式部分省略