zoukankan      html  css  js  c++  java
  • vuejs实现折叠面板展开收缩动画

    vuejs通过css3实现元素固定高度到auto高度的动画和auto高度到固定高度的动画。

    循环列表,html:

    
    <template>
      <div class="newslist">
        <ul>
          <li v-for="(item,index) in newslist" :key="index">
            <p class="p" ref="liCon">{{item.content}}</p>
            <div class="open" @click="open(item,index)">
              <div v-if="!item.openFlag">【展开】</div>
              <div v-else>【收缩】</div>
            </div>
          </li>
        </ul>
      </div>
    </template>
    

    在css上加上动画transition

    
      .newslist ul li p {
        font-size: 14px;
        color: #555;
        line-height: 25px;
        height: 50px;
        overflow: hidden;
        transition: height .3s;
      }
    

    重点是下面js的实现:

    分为两种情况:

    (一)初始状态是收缩时:

    
    <script type="text/ecmascript-6">
      import Vue from 'vue'
    
      export default {
        props: ['newslist'],
        data() {
          return {
            liConHeight: 50 // 两行文字的高度
          }
        },
        methods: {
          open(item, i) {
            const liCon = this.$refs.liCon[i]
            var height = liCon.offsetHeight
            if (height === this.liConHeight) { // 展开
              liCon.style.height = 'auto'
              height = liCon.offsetHeight
              liCon.style.height = this.liConHeight + 'px'
              var f = document.body.offsetHeight  // 必加
              liCon.style.height = height + 'px'
            } else { // 收缩
              liCon.style.height = this.liConHeight + 'px'
            }
            if (!item.openFlag) {
              Vue.set(item, 'openFlag', true)
            } else {
              Vue.set(item, 'openFlag', false)
            }
          }
        }
      }
    </script>
    

    (二)初始状态是展开时:

    稍微改动:

    
    var height = liCon.offsetHeight  // 也可以是liCon.getBoundingClientRect().height
    if (height === this.liConHeight) { // 展开
        liCon.style.height = 'auto'
        height = liCon.offsetHeight
        liCon.style.height = this.liConHeight + 'px'
        liCon.style.height = height + 'px'
    } else { // 收缩
        liCon.style.height = height + 'px'
        var f = document.body.offsetHeight
        liCon.style.height = this.liConHeight + 'px'
    }
    

    来源:https://segmentfault.com/a/1190000016280626

  • 相关阅读:
    archlinux安装nvidia-1050ti闭源驱动教程,亲测
    键盘键码对照
    解决manjaro无法外接显示器
    windows下的上帝模式,很好用,细想,很恐怖啊
    高效率编辑器 Vim——操作篇,非常适合 Vim 新手
    linux rand application
    数组和对象常用API
    JsBridge的异步不执行的处理--promise异步变同步
    js根据数组对象中某个元素合并数组
    js/vue 高德地图绘制驾车路线图
  • 原文地址:https://www.cnblogs.com/qixidi/p/10144160.html
Copyright © 2011-2022 走看看