自定义展开收起面板

自定义展开收起面板

<template>
  <div class="jy-custom-collapse" :style="getRootStyle">
    <!-- 左侧区域 -->
    <div class="jy-custom-collapse-main" :style="horizontal ? getWidthStyle : getHeightStyle">
      <!-- 内容自定义 -->
      <div :style="horizontal ? {} : isOverflow">
        <span class="targetElement" ref="targetElement">
          <slot></slot>
        </span>
      </div>

      <div class="handle-horizontal" :style="getBtnImg" @click="handleClick" v-if="horizontal">
        <em :class="`${value ? 'jy-icon-arrow-right' : 'jy-icon-arrow-left'}`"></em>
      </div>
    </div>
    <!-- 右侧区域 -->
    <div
      :class="['jy-custom-collapse-container', { 'jy-custom-collapse-container-close': !value }]"
      :style="getContainerStyle"
      v-if="horizontal"
    >
      <!-- 内容自定义 -->
      <slot name="container"></slot>
    </div>
    <!-- 按钮 -->
    <div class="handle">
      <span class="handle-vertical" @click="handleClick" v-if="!horizontal && showBtn">
        <span>
          <span :class="['iconfont icon-xiangxiajiantou1', { 'open-more-icon': !value }]"></span>
          <span>{{ getbtnName }}</span>
        </span>
      </span>
    </div>
  </div>
</template>

<script>
import collapse from './collapse.png'
import { mapState } from 'vuex'
const defaultOptions = {
  // 主内容区默认高度
  maxHeight: 200,
  // 内容宽度
  containerWidth: 476,
  // 内容最大宽度
  containerMaxwidth: 476,
  // 最大显示多少行
  showLineClamp: 3,
  // 展开收起
  handerText: ['展开全部', '收起']
}
export default {
  props: {
    options: {
      type: Object,
      default() {
        return {}
      }
    },
    // 展示收起控制
    value: {
      type: Boolean
    },
    // 水平还是垂直
    horizontal: {
      type: Boolean,
      default: true
    }
  },
  computed: {
    ...mapState('d2admin/menu', ['asideCollapse']),
    // 根模块样式
    getRootStyle() {
      return {
        'flex-direction': this.horizontal ? 'row' : 'column'
      }
    },
    // 主内容水平区域 样式
    getWidthStyle() {
      const { containerWidth } = this.config
      return {
        width: this.value ? `calc(100% - 16px - ${containerWidth}px)` : '100%'
      }
    },
    // 右侧内容区域样式
    getContainerStyle() {
      const { containerWidth } = this.config
      return {
        width: this.value ? containerWidth + 'px' : '0px'
      }
    },
    // 主内容竖向区域 样式
    getHeightStyle() {
      const { maxHeight } = this.config
      const heightObj = this.value ? { 'max-height': maxHeight + 'px' } : { height: 'auto' }
      return {
        overflow: this.value ? 'hidden' : 'auto',
        transition: 'all 0.05s',
        boxSizing: 'border-box',
        ...heightObj
      }
    },
    isOverflow() {
      const { showLineClamp } = this.config
      const str = this.value
        ? {
            WebkitLineClamp: showLineClamp, // 限制为3行
            overflow: 'hidden',
            display: '-webkit-box', // 使用 WebKit 的弹性盒子模型显示
            WebkitBoxOrient: 'vertical' // 设置或检索伸缩盒对象的子元素的排列方式(垂直)
          }
        : {}
      return {
        height: '100%',
        boxSizing: 'border-box',
        padding: '10px 20px 0px',
        lineHeight: '30px',
        transition: 'all 0.05s',
        ...(str || {})
      }
    },

    getBtnImg() {
      const { btnStyle } = this.config
      return { backgroundImage: `url(${collapse})`, ...(btnStyle || {}) }
    },
    // 获取按钮名称
    getbtnName() {
      const btn = ['展开全部', '收起']
      const { handerText = btn } = this.config
      const [open, stop] = handerText || btn
      return this.value ? open : stop
    }
  },
  data() {
    return {
      config: {},
      showBtn: true
    }
  },
  watch: {
    options: {
      deep: true,
      immediate: true,
      handler(newValue) {
        /* 初始化合并参数 */
        this.config = Object.assign({}, defaultOptions, newValue)
      }
    },
    asideCollapse() {
      if (!this.horizontal) {
        this.isShowBtn()
      }
    }
  },
  methods: {
    handleClick() {
      this.$emit('input', !this.value)
    },
    isShowBtn() {
      this.$nextTick(() => {
        const element = this.$refs.targetElement
        const { maxHeight } = this.config
        if (element) {
          this.showBtn = Boolean((element.offsetHeight || 0) > maxHeight)
        }
      })
    }
  },
  mounted() {
    if (!this.horizontal) {
      this.isShowBtn()
      window.addEventListener('resize', () => {
        this.isShowBtn()
        this.util.throttle(this.isShowBtn, 700)()
      })
    }
  }
}
</script>

<style scoped lang="scss">
.jy-custom-collapse {
  box-sizing: border-box;
  overflow: hidden;
  width: 100%;
  height: auto;
  display: flex;
  justify-content: space-between;
  &-main {
    transition: all 0.05s;
    position: relative;
    height: 100%;
    &::-webkit-scrollbar {
      display: none;
    }
  }
  .handle-horizontal {
    position: absolute;
    top: 50%;
    right: 0;
    margin-top: -29px;
    width: 16px;
    height: 58px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    background-repeat: no-repeat;
    background-size: 100% 100%;
  }
  .handle {
    min-height: 10px;
    .handle-vertical {
      width: 95px;
      height: 30px;
      margin: 0 auto;
      box-sizing: border-box;
      font-size: 12px;
      display: flex;
      justify-content: center;
      align-items: center;
      cursor: pointer;
      background-repeat: no-repeat;
      background-size: 100% 100%;
      background-image: url('~@/../src/assets/images/main/see-more.png');
      color: $--jy-color-text-secondary;
      .iconfont {
        font-size: 12px;
        margin-right: 6px;
        font-weight: 600;
        transition: all 0.2s;
        display: inline-block;
      }
      .open-more-icon {
        transform: rotate(-180deg);
      }
    }
  }
  &-container {
    flex: 1;
    margin-left: 16px;
    height: 100%;
    transition: all 0.05s;
  }
  &-container-close {
    width: 0;
    flex: 0;
    margin-left: 0;
  }
}
</style>


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,546评论 6 507
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,224评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,911评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,737评论 1 294
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,753评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,598评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,338评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,249评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,696评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,888评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,013评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,731评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,348评论 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,929评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,048评论 1 270
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,203评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,960评论 2 355

推荐阅读更多精彩内容

  • 本周主要学习了异常处理,知道了异常处理可以提高了代码的容错率(健壮性)。异常处理和程序错误是有着根本上的不同的。异...
    琰懿阅读 110评论 1 1
  • 看女人看她的身材,看男人看他的钱财。 一个女人身材背后隐藏着自律和坚持。 一个男人钱财背后隐藏着智慧和运气。 年龄...
    15399c89197b阅读 538评论 4 2
  • 第一篇留给我最爱的妈妈 亲爱的妈妈: 落笔是念 好久了,离开您的身边已有一月多余,很想你,真的很想您。长大...
    ZL小宝阅读 61评论 0 0
  • 这次犯错后,我深刻的反思了自己,藏手机是非常不自觉的表现,由于自己的手机瘾真的特别大,就有了这一想法,这一想法得到...
    快乐微笑每一天阅读 83评论 0 1
  • 再坚强的人,也有软肋;再厚重的铠甲,也有卸下的那一刻。只愿我需要拥抱的时候,你能毫不犹豫的抱紧我,跟我说,你懂我的...
    星辰0418阅读 343评论 0 0