您好,欢迎访问一九零五行业门户网

关于Vue.JS实现垂直方向展开和收缩不定高度模块的JS组件的方法

这篇文章主要介绍了vue.js实现垂直方向展开、收缩不定高度模块的js组件,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
需求分析:
如图,有很多高度不固定的模块(图中只显示两个,本人项目有十三个),点击模块标题展开相应的模块,再次点击此模块匿藏,如何实现此需求并实现复用?
点击红框前:
点击后:
难点分析:
模块高度不固定。比如,本人一开始想到的方法如下:
<!doctype html><html lang="en"><head> <meta charset="utf-8"> <title>title</title> <style> .box{ height:500px; background-color:black; overflow: hidden; } .mybox-leave-active,.mybox-enter-active{ transition: all 1s ease; } .mybox-leave-active,.mybox-enter{ height:0px !important; } .mybox-leave,.mybox-enter-active{ height: 500px; } </style></head><body><p id="box"> <transition name="mybox"> <p class="box" v-show="boxshow"></p> </transition> <button @click="togglebox">按钮</button></p></body><script src="../bower_components/vue/dist/vue.js"></script><script> new vue({ el:'#box', data:{ boxshow:false }, methods:{ togglebox:function(){ this.boxshow = !this.boxshow; } } });</script></html>
这种方法确实可以实现点击展开,再次点击收缩的需求,但是有一个明显的缺点:限定了容器的高度,也就是每个模块都需要固定高度,并不适用于需求场景。
解决方案:
1、实现一个函数式组件
本人命名为vertical-toggle.js// created by xiaoqiang on 17/04/2018.const eltransition = '0.3s height ease-in-out, 0.3s padding-top ease-in-out, 0.3s padding-bottom ease-in-out'const transition = { 'before-enter' (el) { el.style.transition = eltransition if (!el.dataset) el.dataset = {} el.dataset.oldpaddingtop = el.style.paddingtop el.dataset.oldpaddingbottom = el.style.paddingbottom el.style.height = 0 el.style.paddingtop = 0 el.style.paddingbottom = 0 }, 'enter' (el) { el.dataset.oldoverflow = el.style.overflow if (el.scrollheight !== 0) { el.style.height = el.scrollheight + 'px' el.style.paddingtop = el.dataset.oldpaddingtop el.style.paddingbottom = el.dataset.oldpaddingbottom } else { el.style.height = '' el.style.paddingtop = el.dataset.oldpaddingtop el.style.paddingbottom = el.dataset.oldpaddingbottom } el.style.overflow = 'hidden' }, 'after-enter' (el) { el.style.transition = '' el.style.height = '' el.style.overflow = el.dataset.oldoverflow }, 'before-leave' (el) { if (!el.dataset) el.dataset = {} el.dataset.oldpaddingtop = el.style.paddingtop el.dataset.oldpaddingbottom = el.style.paddingbottom el.dataset.oldoverflow = el.style.overflow el.style.height = el.scrollheight + 'px' el.style.overflow = 'hidden' }, 'leave' (el) { if (el.scrollheight !== 0) { el.style.transition = eltransition el.style.height = 0 el.style.paddingtop = 0 el.style.paddingbottom = 0 } }, 'after-leave' (el) { el.style.transition = '' el.style.height = '' el.style.overflow = el.dataset.oldoverflow el.style.paddingtop = el.dataset.oldpaddingtop el.style.paddingbottom = el.dataset.oldpaddingbottom }}export default { name: 'verticaltoggle', functional: true, render (h, { children }) { const data = { on: transition } return h('transition', data, children) }}
2、引用此组件
在components中注册了此组件:
即可在teamplate中引用,请留意红框文字说明部分。
至此,vue.js实现垂直展开、收缩不定高度模块组件实现完成及应用均已完成。
实现效果:
以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注!
相关推荐:
javascript如何对图片进行黑白化_javascript技巧
vue单个组件实现无限层级多选菜单功能
以上就是关于vue.js实现垂直方向展开和收缩不定高度模块的js组件的方法的详细内容。
其它类似信息

推荐信息