国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

vuejs實現折疊面板展開收縮動畫效果

2019-11-19 13:00:52
字體:
來源:轉載
供稿:網友

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().heightif (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'}

總結

以上所述是小編給大家介紹的vuejs實現折疊面板展開收縮動畫效果,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對武林網網站的支持!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 井冈山市| 凤城市| 临夏市| 龙井市| 桃江县| 丰顺县| 琼中| 雅江县| 炉霍县| 同江市| 定州市| 绥棱县| 手机| 莎车县| 尼木县| 盐边县| 临桂县| 庆云县| 山阴县| 黄龙县| 驻马店市| 新绛县| 资中县| 阿尔山市| 琼结县| 元朗区| 闻喜县| 莎车县| 广汉市| 安达市| 凤翔县| 宜宾县| 义马市| 杭锦旗| 贡觉县| 渭南市| 永登县| 临泉县| 沁水县| 襄樊市| 鄂伦春自治旗|