js 滚动到指定位置显示或隐藏元素

  1. $(function(){  
  2.     $(window).scroll(function(){  
  3.         var scroll_top=$(window).scrollTop();  
  4.         console.log(scroll_top)  
  5.         if(scroll_top>=200){  
  6.             $("#lianxi").fadeIn();  
  7.         }else{  
  8.             $("#lianxi").fadeOut();  
  9.         }  
  10.     })  
  11. })
可以使用Vue3的`Intersection Observer` API来实现这个效果。首先,在需要显示元素上添加`ref`引用,以及一个`v-if`指令来控制它的显示隐藏: ```html <div v-if="showElement" ref="myElement">要显示元素</div> ``` 然后,在Vue3组件的`setup()`函数中,使用`ref`来定义一个`Intersection Observer`对象,用来观察指定元素是否进入或离开了视口。在回调函数中,可以根据元素的可见性来设置`showElement`的值,从而控制元素显示隐藏: ```javascript import { ref, onMounted } from 'vue' export default { setup() { const observer = ref(null) const showElement = ref(false) const options = { rootMargin: '0px', threshold: 0.5 } const callback = (entries, observer) => { const [entry] = entries if (entry.isIntersecting) { showElement.value = true observer.unobserve(entry.target) } } onMounted(() => { observer.value = new IntersectionObserver(callback, options) observer.value.observe(myElement.value) }) return { showElement, myElement } } } ``` 最后,为了实现动画效果,可以在元素上添加一个`transition`属性,以及一个`watch`监听`showElement`的变化,来触发动画: ```html <template> <div> <div v-if="showElement" ref="myElement" class="animate__animated animate__fadeInDown">要显示元素</div> </div> </template> <script> import { ref, onMounted, watch } from 'vue' export default { setup() { const observer = ref(null) const showElement = ref(false) const options = { rootMargin: '0px', threshold: 0.5 } const callback = (entries, observer) => { const [entry] = entries if (entry.isIntersecting) { showElement.value = true observer.unobserve(entry.target) } } const myElement = ref(null) onMounted(() => { observer.value = new IntersectionObserver(callback, options) observer.value.observe(myElement.value) }) watch(showElement, (newValue, oldValue) => { if (oldValue === true && newValue === false) { myElement.value.classList.remove('animate__fadeInDown') myElement.value.classList.add('animate__fadeOutUp') } else if (oldValue === false && newValue === true) { myElement.value.classList.remove('animate__fadeOutUp') myElement.value.classList.add('animate__fadeInDown') } }) return { showElement, myElement } } } </script> <style> .animate__animated { animation-duration: 1s; animation-fill-mode: both; } .animate__fadeInDown { animation-name: fadeInDown; } .animate__fadeOutUp { animation-name: fadeOutUp; } @keyframes fadeInDown { from { opacity: 0; transform: translate3d(0, -100%, 0); } to { opacity: 1; transform: none; } } @keyframes fadeOutUp { from { opacity: 1; transform: none; } to { opacity: 0; transform: translate3d(0, -100%, 0); } } </style> ``` 这样,当元素进入视口时,它就会以`fadeInDown`动画显示出来;当元素离开视口时,它就会以`fadeOutUp`动画隐藏起来。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值