uni-app文章列表制作⑧

文章目录

十二、文章列表制作-容器组件

一、定义 articleList 组件
  1. 使用 swiper 组件实现滚动效果

https://uniapp.dcloud.io/component/swiper

  1. swiperItem 数量动态话,当前的 swiper 数量应该与选项卡的数量相同

    复制代码
    获取选项卡的数量值,根据选项卡数量进行 swiperItem 渲染,index 界面进行 labelList 传递
    react 复制代码
        <ArticleList :labelList="labelLIst" class="list-container"></ArticleList>
    1. ArticleList 内根据 swiper 数量进行 swiperItem 渲染
    react 复制代码
       <swiper class="swiper-container">
       <swiper-item v-for="(item,index) in labelList" :key="index">
           <view class="swiper-item uni-bg-red">
           <list-item></list-item>
           </view>
       </swiper-item>
       </swiper>
  2. 选项卡与 swiper 组件联动效果实现

    1. 选项卡点击事件绑定

      发送事件,调整 activeIndex 值,将 activeIdnex 值调整为父组件传递值

      swiper 制定 current 属性,值为 activeIndex

    2. swiper change 事件监听,实现改变 activeIndex 属性

      react 复制代码
       <swiper class="swiper-container" :current="activeIndex" @change="changCurrentIndex">
          <swiper-item v-for="(item,index) in labelList" :key="index">
            <view class="swiper-item uni-bg-red">
              <list-item></list-item>
            </view>
          </swiper-item>
        </swiper>
      
      <script>
        	 methods:{
            changeCurrentIndex(e) {
                const {current}  = e.detail;
                this.$emit('changeCurrentIndex',current)
           	 }
        		}
      </script>
二、选项卡自动进行位置调整
  1. scroll-view 组件添加属性 scroll-with-animation 及 scroll-left 属性 https://uniapp.dcloud.io/component/scroll-view

  2. 动态设置 scrollintoview 属性,为每一项添加 ID 属性进行跳转切换

react 复制代码
<scroll-view class="tab-scroll" scroll-x="true" :scroll-into-view="currentIndex" :scroll-with-animation="true">
        <view class="tab-scroll-box">
        <view :id="`item${index}`" @click="navClickFn(index)" :class="{active:activeIndex === index}" v-for="(item, index) in labelList" :key="index" class="tab-scroll-item">{{ item.name}}</view>
        </view>
    </scroll-view>

<script>
    watch:{
    activeIndex(index){
    this.currentIndex = `item${index}`
    }
    },
    data() {
    return {
    currentIndex:''
    }
    }
</script>
  1. 通过 watch 属性监听 currIndex 值改变,进行 currentIndex 设定
相关推荐
aidingni8882 分钟前
保护你的前端:打造数字堡垒的艺术
前端·javascript
快乐是一切3 分钟前
PDF底层结构之字体与编码系统
前端
llq_3503 分钟前
解决端口被占用问题的 Webpack 启动脚本
前端
沢田纲吉4 分钟前
《LLVM IR 学习手记(六):break 语句与 continue 语句的实现与解析》
前端·c++·llvm
桜吹雪4 分钟前
15 个可替代流行 npm 包的 Node.js 新特性
javascript·后端
火锅小王子4 分钟前
目标筑基:从0到1学习GoLang (入门 Go语言+GoFrame开发服务端+ langchain接入)
前端·后端·openai
温宇飞5 分钟前
CSS 属性分类
前端
鹏多多7 分钟前
使用React-OAuth进行Google/GitHub登录的教程和案例
前端·javascript·react.js
苏打水com34 分钟前
JavaScript 企业面试与学习难度拆解:从0到中高级的阶梯式路线图
javascript
晓得迷路了36 分钟前
栗子前端技术周刊第 101 期 - React 19.2、Next.js 16 Beta、pnpm 10.18...
前端·javascript·react.js