uniapp 内容展开组件

uni-collapse折叠面板并不符合需求,需要自己写一个。

效果展示:

代码: (vue3版本)

<template>
  <view class="collapse-view">
    <view class="collapse-content">
      <swiper
        :autoplay="false"
        :circular="true"
        :indicator-dots="false"
        :style="{
          width: '100%',
          height: calcHeight(),
        }"
        class="collapse-swiper"
        @change="changeSwp"
      >
        内容区域
      </swiper>
      <view class="mode-change" @click="changeMode">
        <!-- 上下展开的小图标-->

        <uni-icons
          v-if="isCollapse"
          type="bottom"
          size="18"
          color="#165dff"
        ></uni-icons>
        <uni-icons v-else type="top" size="18" color="#165dff"></uni-icons>
      </view>
    </view>
  </view>
</template>

<script setup>
import {ref,reactive} from 'vue'
let isCollapse =ref(true) //是否展开控件
// 计算组件内容区域的高度
const calcHeight = () =>{
  //默认高度
  let h = "70rpx";
  if (!isCollapse.value) {
    //展开后高度
    h = 190 + "rpx";
  }
  return h;
}
//切换展开与否
const changeMode =()=>{
  isCollapse.value =!isCollapse.value
}
</script>
<style lang="scss" scoped>
.collapse-view {
  width: 100%;
  height: auto;
  background-color: #fff;
  margin-top: 20rpx;

  swiper {
    width: 100%;
    height: 60upx;
  }
  .collapse-content {
    padding-bottom: 26rpx;
    border-bottom: 1upx solid #f7f7f7;
    border-bottom-left-radius: 37upx;
    border-bottom-right-radius: 37upx;
  }
  .collapse-swiper {
    min-height: 70upx;
    transition: height ease-out 0.3s;
  }
  .mode-change {
    display: flex;
    justify-content: center;
    margin-top: 10upx;
    margin-bottom: 22upx;

    :deep(.uni-icons) {
      position: absolute;
    }
  }
}
</style>

vue2版本的 逻辑片段


<script>
export default {
  props: {},

  computed: {
//结构部分直接写 calcHeight来调用。不要()
    calcHeight() {
      //默认高度
      let h = "70rpx";
      if (!this.isCollapse) {
       //展开后高度
        h = 190 + "rpx";
      }
      return h;
    },
  },
  data() {
    return {
      isCollapse: true, //展开与折叠控件
    };
  },
  methods: {
    changeMode() {
      this.isCollapse = !this.isCollapse;
    },
  },
};
    
</script>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以帮你写一个基于uniapp的折叠面板组件。 1. 创建一个名为`FoldPanel`的组件。 ```vue <template> <div class="fold-panel"> <div class="fold-panel-header" @click="toggleFold"> <div class="fold-panel-title">{{ title }}</div> <div class="fold-panel-arrow" :class="{fold: folded}"></div> </div> <div class="fold-panel-content" :style="{height: contentHeight}"> <slot></slot> </div> </div> </template> ``` 2. 在组件中定义`title`、`folded`和`contentHeight`三个属性以及`toggleFold`方法。 ```vue <script> export default { name: 'FoldPanel', props: { title: String, folded: { type: Boolean, default: true } }, data () { return { contentHeight: this.folded ? '0px' : 'auto' } }, methods: { toggleFold () { this.folded = !this.folded this.contentHeight = this.folded ? '0px' : 'auto' } } } </script> ``` 3. 在CSS中定义组件的样式。 ```css .fold-panel { border: 1px solid #ccc; border-radius: 4px; overflow: hidden; margin: 10px; } .fold-panel-header { display: flex; align-items: center; justify-content: space-between; background-color: #f5f5f5; padding: 10px; cursor: pointer; } .fold-panel-title { font-size: 16px; font-weight: bold; } .fold-panel-arrow { width: 0; height: 0; margin-left: 5px; border-top: 5px solid transparent; border-bottom: 5px solid transparent; border-left: 5px solid #000; transition: transform 0.2s ease; } .fold-panel-arrow.fold { transform: rotate(-90deg); } .fold-panel-content { transition: height 0.2s ease; overflow: hidden; padding: 10px; } ``` 现在你可以在uniapp中使用这个折叠面板组件了。例如: ```vue <template> <div> <FoldPanel title="面板1"> 面板1的内容 </FoldPanel> <FoldPanel title="面板2" :folded="false"> 面板2的内容 </FoldPanel> </div> </template> <script> import FoldPanel from '@/components/FoldPanel.vue' export default { components: { FoldPanel } } </script> ``` 这样就可以在页面中显示两个折叠面板了。第一个面板默认是折叠状态,而第二个面板默认是展开状态。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值