首页 > 其他分享 >uniapp中swiper设置自适应高度

uniapp中swiper设置自适应高度

时间:2023-05-03 09:23:55浏览次数:45  
标签:uniapp res 高度 设置 query swiper

原文链接: https://www.jianshu.com/p/6a2fa0f1f86e

uniapp中如何设置swiper的高度自适应的问题解决

uniapp中的swiper组件可以用来做滑动切屏的,但是有个不好的地方,就是必须设置一个固定的高度,对于在每一个swiper-item里的内容可能不一定的情况,就会造成内部的内容不能自动撑开,就被截取了,这个就很头疼,网上找了很多资料,终于解决了这个问题。

一、解决思路

  1. 在每次滑动切换的时候,动态地获取swiper-item内部的DOM的元素的高度。
  2. 将获取的高度动态设置给swiper元素。

二、代码解析

<template>
  <view>
    <swiper
      :autoplay="false"
      @change="changeSwiper"
      :current="currentIndex"
      :style="{ height: swiperHeight + 'px' }"
    >
      <swiper-item v-for="(item, index) in dataList" :key="item.id">
        <view :id="'content-wrap' + index">
            每一个swiper-item的内容区域
            ....
        </view>
      </swiper-item>
    </swiper>   
  </view>
</template>

<script>
export default {
  data() {
    return {
      //滑块的高度(单位px)
      swiperHeight: 0,
      //当前索引
      currentIndex: 0,
      //列表数据
      dataList: [],
    };
  },
  onl oad(args) {
    //动态设置swiper的高度
    this.$nextTick(() => {
      this.setSwiperHeight();
    });
  },
  methods: {
    //手动切换题目
    changeSwiper(e) {
      this.currentIndex = e.detail.current;
      //动态设置swiper的高度,使用nextTick延时设置
      this.$nextTick(() => {
        this.setSwiperHeight();
      });
    },
    //动态设置swiper的高度
    setSwiperHeight() {
      let element = "#content-wrap" + this.currentIndex;
      let query = uni.createSelectorQuery().in(this);
      query.select(element).boundingClientRect();
      query.exec((res) => {
        if (res && res[0]) {
          this.swiperHeight = res[0].height;
        }
      });
    },
  },
};
</script>

<style lang="scss">

</style>

标签:uniapp,res,高度,设置,query,swiper
From: https://www.cnblogs.com/zx529/p/17368672.html

相关文章

  • vivado ILA更改设置
    更改检测端口双击ila_0,可以修改检测端口数与位数。更改检测模块直接代码中修改,子模块也可以调用设置的ilaip硬件检测界面添加信号更改检测端口后,编译过后界面并不会自动添加检测信号,可以手动添加。......
  • 【C++】设立一组状态,在程序运行过程中设置对象的某个状态,检查对象是否满足所有的状态
    `#include//定义状态枚举enumState{STATE_A=1<<0,//0001STATE_B=1<<1,//0010STATE_C=1<<2//0100};classMyClass{private:intcurrentState;public:MyClass():currentState(0){}//设置状态voidsetState(Statestate){......
  • python设置环境变量在代码中
    以Linux平台为例:>>>importsys>>>sys.path'''['',#当前目录'/usr/local/python3/lib/python37.zip',#python标准库目录'/usr/local/python3/lib/python3.7','/usr/local/python3/lib/python3.7/lib-dynl......
  • 【转】Ubuntu系统Gnome桌面设置允许root登录
    转自https://www.cnblogs.com/tonyc/p/10432871.html设置方法如下:vi/etc/pam.d/gdm-autologin找到下面这一行authrequiredpam_succeed_if.souser!=rootquiet_success在最前面加上#注释掉。对/etc/pam.d/gdm-password进行同样的操作,重启。......
  • echarts 5.x 如果legend设置selected时,legend需要单击两次才能切换状态
    在第一组的selected里面先进行声明,在当前组再进行一次声明就可以了。 legend:[    {     show:true,     x:'center',     y:'0',     data:['日平均气温(℃)','日平均室温(℃)'],     textStyle:{......
  • pyqt5-快捷键设置
    1、介绍pyqt5中有三种方式可以设置快捷键,所谓快捷键,本质上就是以单个或多个组合键盘按键的方式,触发组件的事件。2、三种方式2.1setShortcut方法"""直接为组件设置快捷键,不区分事件类型,会调用该组件各种事件绑定的所有方法"""self.ui.pushButton_2.setShortcut('A')self.u......
  • Visual Studio 项目的设置
    1、File->New->Project...展开截图2、File->New->Project...点击查看代码......
  • 可设置圆角背景边框的按钮, 通过调节色彩明度自动计算按下(pressed)状态颜色
    可设置圆角背景边框的的按钮,通过调节色彩明度自动计算按下(pressed)状态颜色使用:xml<?xmlversion="1.0"encoding="UTF-8"?><LinearLayoutandroid:paddingTop="20dp"android:orientation="vertical"android:layout_height=&quo......
  • C# 调用 C dll char* callback 设置回调函数不定参数
    1:C#调用返回字符串C++nativedll函数的注意事项:a:C++DLL的返回值,安全的做法是分配一个全局char数组,把要返回的char*复制到这个char数组中, charbuff[255];constchar*__stdcallReturnString(){strcpy(buff,"xxxxxxxxxxxxxxx");returnbuff;}......
  • 设置ImageView的图片资源是直接来自SD卡
    在设置ImageView资源的时候,这时的图片是来自SD卡,查看API很容易就会看到view.setImageUri(Uriu)这个函数。所以一般会这样写:ImageViewview=(ImageView)findViewById(...);Filefile=newFile(path);Uriuri=Uri.from(file);view.setImageUr......