vuejs實(shí)現(xiàn)折疊面板展開收縮動(dòng)畫效果
vuejs通過css3實(shí)現(xiàn)元素固定高度到auto高度的動(dòng)畫和auto高度到固定高度的動(dòng)畫。
循環(huán)列表,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上加上動(dòng)畫transition
.newslist ul li p {
font-size: 14px;
color: #555;
line-height: 25px;
height: 50px;
overflow: hidden;
transition: height .3s;
}
重點(diǎn)是下面js的實(shí)現(xiàn):
分為兩種情況:
(一)初始狀態(tài)是收縮時(shí):
<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>
(二)初始狀態(tài)是展開時(shí):
稍微改動(dòng):
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'
}
總結(jié)
以上所述是小編給大家介紹的vuejs實(shí)現(xiàn)折疊面板展開收縮動(dòng)畫效果,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- vue頂部菜單欄實(shí)現(xiàn)小結(jié)
- vue實(shí)現(xiàn)右鍵菜單欄
- vue+el-menu實(shí)現(xiàn)菜單欄無限多層級(jí)分類
- 如何利用Vue3管理系統(tǒng)實(shí)現(xiàn)動(dòng)態(tài)路由和動(dòng)態(tài)側(cè)邊菜單欄
- vue如何實(shí)現(xiàn)自定義底部菜單欄
- vue列表單項(xiàng)展開收縮功能之this.$refs的詳解
- Vue2(三)實(shí)現(xiàn)子菜單展開收縮,帶動(dòng)畫效果實(shí)現(xiàn)方法
- vue2左側(cè)菜單欄收縮展開功能實(shí)現(xiàn)
相關(guān)文章
Vue實(shí)現(xiàn)動(dòng)態(tài)路由的示例詳解
這篇文章主要為大家詳細(xì)介紹了Vue實(shí)現(xiàn)動(dòng)態(tài)路由的相關(guān)知識(shí),主要涉及到兩個(gè)方面:一是路由的動(dòng)態(tài)添加,二是基于路由的參數(shù)變化來動(dòng)態(tài)渲染組件,下面就跟隨小編一起深入學(xué)習(xí)一下動(dòng)態(tài)路由的實(shí)現(xiàn)吧2024-02-02
vue基礎(chǔ)知識(shí)--axios合并請(qǐng)求和slot
這篇文章主要介紹了vue中的axios和slot,文中代碼非常詳細(xì),對(duì)大家的工作學(xué)習(xí)有所幫助,感興趣的朋友可以參考下2020-06-06
Nuxt如何實(shí)現(xiàn)將服務(wù)測(cè)數(shù)據(jù)存儲(chǔ)到Vuex中
這篇文章主要介紹了Nuxt如何實(shí)現(xiàn)將服務(wù)測(cè)數(shù)據(jù)存儲(chǔ)到Vuex中的方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10
Vue.js實(shí)現(xiàn)開發(fā)購物車功能的方法詳解
這篇文章主要介紹了Vue.js實(shí)現(xiàn)開發(fā)購物車功能的方法,結(jié)合實(shí)例形式分析了基于vue.js開發(fā)的購物車功能相關(guān)操作步驟與實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-02-02
Vue3獲取響應(yīng)式數(shù)據(jù)的四種方法
Vue 3 引入了一個(gè)全新的響應(yīng)式系統(tǒng),其中最核心的就是 reactive 和 ref,它們是實(shí)現(xiàn)響應(yīng)式數(shù)據(jù)的基礎(chǔ),用于創(chuàng)建可以自動(dòng)跟蹤變化并更新視圖的對(duì)象和變量,本文介紹了Vue3獲取響應(yīng)式數(shù)據(jù)的四種方法,需要的朋友可以參考下2024-08-08

