Vue.JS實(shí)現(xiàn)垂直方向展開(kāi)、收縮不定高度模塊的JS組件
需求分析:
如圖,有很多高度不固定的模塊(圖中只顯示兩個(gè),本人項(xiàng)目有十三個(gè)),點(diǎn)擊模塊標(biāo)題展開(kāi)相應(yīng)的模塊,再次點(diǎn)擊此模塊匿藏,如何實(shí)現(xiàn)此需求并實(shí)現(xiàn)復(fù)用?
點(diǎn)擊紅框前:
點(diǎn)擊后:
難點(diǎn)分析:
模塊高度不固定。比如,本人一開(kāi)始想到的方法如下:
<!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> <div id="box"> <transition name="mybox"> <div class="box" v-show="boxshow"></div> </transition> <button @click="togglebox">按鈕</button> </div> </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>
這種方法確實(shí)可以實(shí)現(xiàn)點(diǎn)擊展開(kāi),再次點(diǎn)擊收縮的需求,但是有一個(gè)明顯的缺點(diǎn):限定了容器的高度,也就是每個(gè)模塊都需要固定高度,并不適用于需求場(chǎng)景。
解決方案:
1、實(shí)現(xiàn)一個(gè)函數(shù)式組件
本人命名為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中注冊(cè)了此組件:
即可在teamplate中引用,請(qǐng)留意紅框文字說(shuō)明部分。
至此,Vue.js實(shí)現(xiàn)垂直展開(kāi)、收縮不定高度模塊組件實(shí)現(xiàn)完成及應(yīng)用均已完成。
實(shí)現(xiàn)效果:
總結(jié)
以上所述是小編給大家介紹的Vue.JS實(shí)現(xiàn)垂直方向展開(kāi)、收縮不定高度模塊的JS組件,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
vue項(xiàng)目如何使用$router.go(-1)返回時(shí)刷新原來(lái)的界面
這篇文章主要介紹了vue項(xiàng)目如何使用$router.go(-1)返回時(shí)刷新原來(lái)的界面問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09基于el-tree實(shí)現(xiàn)懶加載穿梭條的示例代碼
這篇文章主要介紹了基于el-tree實(shí)現(xiàn)懶加載穿梭條的示例代碼,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-03-03使用vue進(jìn)行Lodop打印的一些常用方法小結(jié)
這篇文章主要給大家介紹了關(guān)于使用vue進(jìn)行Lodop打印的一些常用方法,需要進(jìn)行打印功能,Lodop就是實(shí)現(xiàn)需求的插件,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-07-07Vue.js+express利用切片實(shí)現(xiàn)大文件斷點(diǎn)續(xù)傳
斷點(diǎn)續(xù)傳就是要從文件已經(jīng)下載的地方開(kāi)始繼續(xù)下載,本文主要介紹了Vue.js+express利用切片實(shí)現(xiàn)大文件斷點(diǎn)續(xù)傳,具有一定的參考價(jià)值,感興趣的可以了解下2023-05-05vue實(shí)現(xiàn)導(dǎo)出Word文件(數(shù)據(jù)流方式)
這篇文章主要介紹了vue實(shí)現(xiàn)導(dǎo)出Word文件(數(shù)據(jù)流方式),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06Ant?Design?of?Vue的樹(shù)形控件Tree的使用及說(shuō)明
這篇文章主要介紹了Ant?Design?of?Vue的樹(shù)形控件Tree的使用及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10vue實(shí)現(xiàn)橫向時(shí)間軸組件方式
這篇文章主要介紹了vue實(shí)現(xiàn)橫向時(shí)間軸組件方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12vue限制文本輸入框只允許輸入字母、數(shù)字、禁止輸入特殊字符
這篇文章主要介紹了vue限制文本輸入框只允許輸入字母、數(shù)字、不允許輸入特殊字符,通過(guò)監(jiān)聽(tīng)表單輸入的內(nèi)容,使用方法的缺陷,本文通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友參考下吧2023-10-10