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 设定
相关推荐
天蓝色的鱼鱼1 分钟前
React Router v8 来了:react-router-dom 没了,老项目该怎么迁移?
前端·react.js
Bolt37 分钟前
TypeScript 7.0 来了:当 tsc 用 Go 重写之后
javascript·typescript·go
闪闪发光得欧1 小时前
前端提效新思路:Gemini 3.5 自动化定位 CSS 异常
前端·css
yingyima1 小时前
掌握正则表达式的核心:贪婪与非贪婪匹配的底层机制
前端
奇奇怪怪的1 小时前
文档摄入与 Chunking 策略全对决
前端
阳火锅2 小时前
😭测试小姐姐终于不骂我了!这个提BUG神器太香了...
前端·javascript·面试
道友可好3 小时前
AI 是最好的混乱放大器:代码熵管理实战
前端·人工智能·后端
猩猩程序员3 小时前
前端学习 AI Agent 开发
前端
Younglina4 小时前
打了3年羽毛球球才发现:我对自己的装备和胜率一无所知
前端·后端
风骏时光牛马4 小时前
Bash脚本高阶实战与常见报错完整代码案例详解
前端