亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Vue.JS實現(xiàn)垂直方向展開、收縮不定高度模塊的JS組件

 更新時間:2018年06月19日 16:42:56   作者:ECMAScripter  
這篇文章主要介紹了Vue.JS實現(xiàn)垂直方向展開、收縮不定高度模塊的JS組件,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下

需求分析:

如圖,有很多高度不固定的模塊(圖中只顯示兩個,本人項目有十三個),點擊模塊標題展開相應的模塊,再次點擊此模塊匿藏,如何實現(xiàn)此需求并實現(xiàn)復用? 

 點擊紅框前:

這里寫圖片描述 

 點擊后:

這里寫圖片描述

難點分析:

模塊高度不固定。比如,本人一開始想到的方法如下:

<!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>

這種方法確實可以實現(xiàn)點擊展開,再次點擊收縮的需求,但是有一個明顯的缺點:限定了容器的高度,也就是每個模塊都需要固定高度,并不適用于需求場景。

解決方案:

1、實現(xiàn)一個函數(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中注冊了此組件:

這里寫圖片描述

即可在teamplate中引用,請留意紅框文字說明部分。

這里寫圖片描述 

至此,Vue.js實現(xiàn)垂直展開、收縮不定高度模塊組件實現(xiàn)完成及應用均已完成。 

 實現(xiàn)效果:

這里寫圖片描述

總結(jié)

以上所述是小編給大家介紹的Vue.JS實現(xiàn)垂直方向展開、收縮不定高度模塊的JS組件,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評論