vuejs实现折叠面板展开收缩动画效果
2018-09-06 11:31
976 查看
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' }
总结
以上所述是小编给大家介绍的vuejs实现折叠面板展开收缩动画效果,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!
您可能感兴趣的文章:
相关文章推荐
- Jquery实现的一个DIV层面板的折叠/展开效果
- JS实现的简单折叠展开动画效果示例
- 在iframe里的页面编写js,实现在父窗口上创建动画效果展开和收缩的div(不变动iframe父窗口代码)
- Vue2(三)实现子菜单展开收缩,带动画效果
- 在iframe里的页面编写js,实现在父窗口上创建动画效果展开和收缩的div(不变动iframe父窗口代码)
- TextView的折叠展开(动画效果)
- css实现会折叠、展开的菜单导航栏效果
- JS实现鼠标滑过折叠与展开菜单效果代码
- 折叠展开收缩效果的栏目分类导航
- jQuery实现有动画淡出效果的二级折叠菜单代码
- vue.js 实现点击展开收起动画效果
- Android 自定义View修炼-自定义可动画展开收缩View的实现
- Extjs展开和收缩动画效果造成问题
- JS+CSS实现DIV层的展开、收缩效果
- 使用CollapsiblePanelExtender实现展开和收缩效果以及Accordion控件
- jquery简单实现网页层的展开与收缩效果
- ExpandableListView展开收缩加动画效果
- jquery实现的Accordion折叠面板效果代码
- jquery实现的Accordion折叠面板效果代码