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 设定
相关推荐
旷世奇才李先生3 分钟前
Next.js 安装使用教程
开发语言·javascript·ecmascript
ᥬ 小月亮8 分钟前
webpack基础
前端·webpack
YongGit27 分钟前
探索 AI + MCP 渲染前端 UI
前端·后端·node.js
慧一居士1 小时前
<script setup>中的setup作用以及和不带的区别对比
前端
RainbowSea1 小时前
NVM 切换 Node 版本工具的超详细安装说明
java·前端
读书点滴2 小时前
笨方法学python -练习14
java·前端·python
Mintopia2 小时前
四叉树:二维空间的 “智能分区管理员”
前端·javascript·计算机图形学
慌糖2 小时前
RabbitMQ:消息队列的轻量级王者
开发语言·javascript·ecmascript
Mintopia2 小时前
Three.js 深度冲突:当像素在 Z 轴上玩起 "挤地铁" 游戏
前端·javascript·three.js
Penk是个码农2 小时前
web前端面试-- MVC、MVP、MVVM 架构模式对比
前端·面试·mvc