简体   繁体   English

Vue.js 在元素进入视口时出现/出现的过渡

[英]Vue.js transition to appear/occur on element entering viewport

Firstly please no jQuery .首先请不要使用 jQuery I could do this in jQuery etc. , the point of the question is to do it without unnecessary dependencies.我可以在 jQuery 等中做到这一点,问题的关键是在没有不必要的依赖的情况下做到这一点。

The scenario here is I'm creating a single page website with several sections to scroll through.这里的场景是我正在创建一个单页网站,其中包含几个要滚动的部分。 I want to use Vue.js's transitions to simply fade in once the browser has scrolled to that section.一旦浏览器滚动到该部分,我想使用Vue.js 的转换来简单地淡入。 I've managed to make the transitions work with the appear attribute but the problem is this initial render trigger works on elements that are off screen and I want to defer that trigger until when the browser has scroll the element on screen.我已经设法使转换与出现属性一起工作,但问题是这个初始渲染触发器适用于屏幕外的元素,我想将该触发器推迟到浏览器滚动屏幕上的元素时。

I've found a library like vue-observe-visibility which would kind of works for what I need but honestly I don't want to create a load of data properties simply for the trigger to change it to true for a v-if statement to trigger the fade in effect.我找到了一个像vue-observe-visibility这样的库,它可以满足我的需要,但老实说,我不想仅仅为了触发器将其更改为 true 以用于 v-if 语句而创建数据属性负载触发淡入效果。 Hopefully that makes some sense.希望这有点道理。

one way, using directives, would be have an on-scroll listener.使用指令的一种方法是有一个滚动监听器。 when the element is in view, then add a class that transitions opacity to 1 (or x-offset).当元素在视图中时,然后添加一个将不透明度转换为 1(或 x-offset)的类。 Then once it's in view, destroy the listener.然后,一旦它出现在视野中,就摧毁听众。

This makes is easy to add to elements, as you don't need to manage state for each item, just change <div> to <div class="hidden hidden-left" v-infocus="'showElement'"> , for every object you want to do this for.这使得添加到元素很容易,因为您不需要管理每个项目的状态,只需将<div>更改为<div class="hidden hidden-left" v-infocus="'showElement'"> ,对于您要为其执行此操作的每个对象。

 new Vue ({ el: '#app', data() {}, methods: {}, directives: { infocus: { isLiteral: true, inserted: (el, binding, vnode) => { let f = () => { let rect = el.getBoundingClientRect() let inView = ( rect.width > 0 && rect.height > 0 && rect.top >= 0 && rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) ) if (inView) { el.classList.add(binding.value) window.removeEventListener('scroll', f) } } window.addEventListener('scroll', f) f() } } } })
 #app { font-family: 'Avenir', Helvetica, Arial, sans-serif; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; text-align: center; color: #2c3e50; margin-top: 60px; } div { min-height: 120px; } h1, h2 { font-weight: normal; } ul { list-style-type: none; padding: 0; } li { display: inline-block; margin: 0 10px; } a { color: #42b983; } /* the classes */ .hidden { opacity: 0; } .hidden-right { transform: translate(50px, 0); } .hidden-left { transform: translate(-50px, 0); } .showElement { opacity: 1; transform: translate(0, 0); -webkit-transition: all 0.5s ease-out; -moz-transition: all 0.5s ease-out; transition: all 0.5s ease-out; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.8/vue.min.js"></script> <div id="app"> <div class="hidden" v-infocus="'showElement'"> <h2>Ecosystem</h2> <ul> <li><a href="http://router.vuejs.org/" target="_blank">vue-router</a></li> <li><a href="http://vuex.vuejs.org/" target="_blank">vuex</a></li> <li><a href="http://vue-loader.vuejs.org/" target="_blank">vue-loader</a></li> <li><a href="https://github.com/vuejs/awesome-vue" target="_blank">awesome-vue</a></li> </ul> </div> <div class="hidden hidden-left" v-infocus="'showElement'"> <h2>Ecosystem</h2> <ul> <li><a href="http://router.vuejs.org/" target="_blank">vue-router</a></li> <li><a href="http://vuex.vuejs.org/" target="_blank">vuex</a></li> <li><a href="http://vue-loader.vuejs.org/" target="_blank">vue-loader</a></li> <li><a href="https://github.com/vuejs/awesome-vue" target="_blank">awesome-vue</a></li> </ul> </div> <div class="hidden hidden-right" v-infocus="'showElement'"> <h2>Ecosystem</h2> <ul> <li><a href="http://router.vuejs.org/" target="_blank">vue-router</a></li> <li><a href="http://vuex.vuejs.org/" target="_blank">vuex</a></li> <li><a href="http://vue-loader.vuejs.org/" target="_blank">vue-loader</a></li> <li><a href="https://github.com/vuejs/awesome-vue" target="_blank">awesome-vue</a></li> </ul> </div> <div class="hidden" v-infocus="'showElement'"> <h2>Ecosystem</h2> <ul> <li><a href="http://router.vuejs.org/" target="_blank">vue-router</a></li> <li><a href="http://vuex.vuejs.org/" target="_blank">vuex</a></li> <li><a href="http://vue-loader.vuejs.org/" target="_blank">vue-loader</a></li> <li><a href="https://github.com/vuejs/awesome-vue" target="_blank">awesome-vue</a></li> </ul> </div> <div class="hidden hidden-left" v-infocus="'showElement'"> <h2>Ecosystem</h2> <ul> <li><a href="http://router.vuejs.org/" target="_blank">vue-router</a></li> <li><a href="http://vuex.vuejs.org/" target="_blank">vuex</a></li> <li><a href="http://vue-loader.vuejs.org/" target="_blank">vue-loader</a></li> <li><a href="https://github.com/vuejs/awesome-vue" target="_blank">awesome-vue</a></li> </ul> </div> <div class="hidden hidden-right" v-infocus="'showElement'"> <h2>Ecosystem</h2> <ul> <li><a href="http://router.vuejs.org/" target="_blank">vue-router</a></li> <li><a href="http://vuex.vuejs.org/" target="_blank">vuex</a></li> <li><a href="http://vue-loader.vuejs.org/" target="_blank">vue-loader</a></li> <li><a href="https://github.com/vuejs/awesome-vue" target="_blank">awesome-vue</a></li> </ul> </div> <div class="hidden" v-infocus="'showElement'"> <h2>Ecosystem</h2> <ul> <li><a href="http://router.vuejs.org/" target="_blank">vue-router</a></li> <li><a href="http://vuex.vuejs.org/" target="_blank">vuex</a></li> <li><a href="http://vue-loader.vuejs.org/" target="_blank">vue-loader</a></li> <li><a href="https://github.com/vuejs/awesome-vue" target="_blank">awesome-vue</a></li> </ul> </div> <div class="hidden hidden-left" v-infocus="'showElement'"> <h2>Ecosystem</h2> <ul> <li><a href="http://router.vuejs.org/" target="_blank">vue-router</a></li> <li><a href="http://vuex.vuejs.org/" target="_blank">vuex</a></li> <li><a href="http://vue-loader.vuejs.org/" target="_blank">vue-loader</a></li> <li><a href="https://github.com/vuejs/awesome-vue" target="_blank">awesome-vue</a></li> </ul> </div> <div class="hidden hidden-right" v-infocus="'showElement'"> <h2>Ecosystem</h2> <ul> <li><a href="http://router.vuejs.org/" target="_blank">vue-router</a></li> <li><a href="http://vuex.vuejs.org/" target="_blank">vuex</a></li> <li><a href="http://vue-loader.vuejs.org/" target="_blank">vue-loader</a></li> <li><a href="https://github.com/vuejs/awesome-vue" target="_blank">awesome-vue</a></li> </ul> </div> <div class="hidden" v-infocus="'showElement'"> <h2>Ecosystem</h2> <ul> <li><a href="http://router.vuejs.org/" target="_blank">vue-router</a></li> <li><a href="http://vuex.vuejs.org/" target="_blank">vuex</a></li> <li><a href="http://vue-loader.vuejs.org/" target="_blank">vue-loader</a></li> <li><a href="https://github.com/vuejs/awesome-vue" target="_blank">awesome-vue</a></li> </ul> </div> <div class="hidden hidden-left" v-infocus="'showElement'"> <h2>Ecosystem</h2> <ul> <li><a href="http://router.vuejs.org/" target="_blank">vue-router</a></li> <li><a href="http://vuex.vuejs.org/" target="_blank">vuex</a></li> <li><a href="http://vue-loader.vuejs.org/" target="_blank">vue-loader</a></li> <li><a href="https://github.com/vuejs/awesome-vue" target="_blank">awesome-vue</a></li> </ul> </div> <div class="hidden hidden-right" v-infocus="'showElement'"> <h2>Ecosystem</h2> <ul> <li><a href="http://router.vuejs.org/" target="_blank">vue-router</a></li> <li><a href="http://vuex.vuejs.org/" target="_blank">vuex</a></li> <li><a href="http://vue-loader.vuejs.org/" target="_blank">vue-loader</a></li> <li><a href="https://github.com/vuejs/awesome-vue" target="_blank">awesome-vue</a></li> </ul> </div> <div class="hidden" v-infocus="'showElement'"> <h2>Ecosystem</h2> <ul> <li><a href="http://router.vuejs.org/" target="_blank">vue-router</a></li> <li><a href="http://vuex.vuejs.org/" target="_blank">vuex</a></li> <li><a href="http://vue-loader.vuejs.org/" target="_blank">vue-loader</a></li> <li><a href="https://github.com/vuejs/awesome-vue" target="_blank">awesome-vue</a></li> </ul> </div> <div class="hidden hidden-left" v-infocus="'showElement'"> <h2>Ecosystem</h2> <ul> <li><a href="http://router.vuejs.org/" target="_blank">vue-router</a></li> <li><a href="http://vuex.vuejs.org/" target="_blank">vuex</a></li> <li><a href="http://vue-loader.vuejs.org/" target="_blank">vue-loader</a></li> <li><a href="https://github.com/vuejs/awesome-vue" target="_blank">awesome-vue</a></li> </ul> </div> <div class="hidden hidden-right" v-infocus="'showElement'"> <h2>Ecosystem</h2> <ul> <li><a href="http://router.vuejs.org/" target="_blank">vue-router</a></li> <li><a href="http://vuex.vuejs.org/" target="_blank">vuex</a></li> <li><a href="http://vue-loader.vuejs.org/" target="_blank">vue-loader</a></li> <li><a href="https://github.com/vuejs/awesome-vue" target="_blank">awesome-vue</a></li> </ul> </div> <div class="hidden" v-infocus="'showElement'"> <h2>Ecosystem</h2> <ul> <li><a href="http://router.vuejs.org/" target="_blank">vue-router</a></li> <li><a href="http://vuex.vuejs.org/" target="_blank">vuex</a></li> <li><a href="http://vue-loader.vuejs.org/" target="_blank">vue-loader</a></li> <li><a href="https://github.com/vuejs/awesome-vue" target="_blank">awesome-vue</a></li> </ul> </div> <div class="hidden hidden-left" v-infocus="'showElement'"> <h2>Ecosystem</h2> <ul> <li><a href="http://router.vuejs.org/" target="_blank">vue-router</a></li> <li><a href="http://vuex.vuejs.org/" target="_blank">vuex</a></li> <li><a href="http://vue-loader.vuejs.org/" target="_blank">vue-loader</a></li> <li><a href="https://github.com/vuejs/awesome-vue" target="_blank">awesome-vue</a></li> </ul> </div> <div class="hidden hidden-right" v-infocus="'showElement'"> <h2>Ecosystem</h2> <ul> <li><a href="http://router.vuejs.org/" target="_blank">vue-router</a></li> <li><a href="http://vuex.vuejs.org/" target="_blank">vuex</a></li> <li><a href="http://vue-loader.vuejs.org/" target="_blank">vue-loader</a></li> <li><a href="https://github.com/vuejs/awesome-vue" target="_blank">awesome-vue</a></li> </ul> </div> </div>

... only, I wish I could remove the listener when the component is unmounted. ...仅,我希望在卸载组件时可以删除侦听器。 I think in a SPA this could cause some listeners to stick around.我认为在 SPA 中,这可能会导致一些听众留下来。

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

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