当前位置:   article > 正文

转载:vue实现高度不固定元素的展开收起_vue3 实现折叠功能 不固定高度

vue3 实现折叠功能 不固定高度
  1. const elTransition = '0.3s height ease-in-out, 0.3s padding-top ease-in-out, 0.3s padding-bottom ease-in-out'
  2. const Transition = {
  3. 'before-enter' (el) {
  4. el.style.transition = elTransition
  5. if (!el.dataset) el.dataset = {}
  6. el.dataset.oldPaddingTop = el.style.paddingTop
  7. el.dataset.oldPaddingBottom = el.style.paddingBottom
  8. el.style.height = 0
  9. el.style.paddingTop = 0
  10. el.style.paddingBottom = 0
  11. },
  12. 'enter' (el) {
  13. el.dataset.oldOverflow = el.style.overflow
  14. if (el.scrollHeight !== 0) {
  15. el.style.height = el.scrollHeight + 'px'
  16. el.style.paddingTop = el.dataset.oldPaddingTop
  17. el.style.paddingBottom = el.dataset.oldPaddingBottom
  18. } else {
  19. el.style.height = ''
  20. el.style.paddingTop = el.dataset.oldPaddingTop
  21. el.style.paddingBottom = el.dataset.oldPaddingBottom
  22. }
  23. el.style.overflow = 'hidden'
  24. },
  25. 'after-enter' (el) {
  26. el.style.transition = ''
  27. el.style.height = ''
  28. el.style.overflow = el.dataset.oldOverflow
  29. },
  30. 'before-leave' (el) {
  31. if (!el.dataset) el.dataset = {}
  32. el.dataset.oldPaddingTop = el.style.paddingTop
  33. el.dataset.oldPaddingBottom = el.style.paddingBottom
  34. el.dataset.oldOverflow = el.style.overflow
  35. el.style.height = el.scrollHeight + 'px'
  36. el.style.overflow = 'hidden'
  37. },
  38. 'leave' (el) {
  39. if (el.scrollHeight !== 0) {
  40. el.style.transition = elTransition
  41. el.style.height = 0
  42. el.style.paddingTop = 0
  43. el.style.paddingBottom = 0
  44. }
  45. },
  46. 'after-leave' (el) {
  47. el.style.transition = ''
  48. el.style.height = ''
  49. el.style.overflow = el.dataset.oldOverflow
  50. el.style.paddingTop = el.dataset.oldPaddingTop
  51. el.style.paddingBottom = el.dataset.oldPaddingBottom
  52. }
  53. }
  54. export default {
  55. name: 'collapseTransition',
  56. functional: true,
  57. render (h, { children }) {
  58. const data = {
  59. on: Transition
  60. }
  61. return h('transition', data, children)
  62. }
  63. }

建个JS文件随便取个名字,把上面的js复制进去,然后在你要用的页面 import 组件名字(随便取) from '你建的JS文件路径',export default{components:{组件名字}},data添加isActive=true;

 <标签 @click="isActive=!isActive">打开或者收缩</标签>
<组件名字>

  1. <div class="collapse-wrap"
  2. v-show="isActive">
  3. <!-- @slot default -->
  4. <slot></slot>
  5. </div>

</组件名字>

https://segmentfault.com/q/1010000011359250 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/91757
推荐阅读
相关标签
  

闽ICP备14008679号