vue3写垂直轮播效果(translateY)

news/2024/7/24 8:38:58 标签: vue.js, javascript, 前端

实现思路:卡片移动使用css的translateY属性实现,每个卡片从最下面移动到最上面,然后直接移动到最下面,每次改变的位移是固定的,假设每次移动50px,当移动到最小时,就让translataY为0,但是一定要进行初始化每个div的初始位置;

代码:

<template>
  <div id="demo">
    <div class="box">
      <!-- 左下角预警 -->
      <div
        class="warning"
        :ref="setItemRef"
        :style="{
          transform: `translateY(${-index * 50}px)`,
          transition: 'transform 0.9s ease',
          zIndex: `${-index + 100}`
        }"
        v-for="(item, index) in warningInfoList"
      >
        <div class="warning-title">{{ item.warning_type }}</div>
        <div class="warning-content" :style="{display: index != 0 ?'none':'block'}">
          <div class="warning-time">{{ item.time }}</div>
          <div class="warning-word">{{ item.warning_content }}</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { reactive, ref, computed, onMounted, onUnmounted, nextTick } from 'vue'

// let warningInfoList = ref([
//   { warning_type: '台风', warning_content: '大风,注意安全', time: '12日-13' },
//   { warning_type: '暴雨', warning_content: '暴雨,注意安全', time: '12日-13' },
//   { warning_type: '冰雹', warning_content: '冰雹,注意安全', time: '12日-13' }
// ])
import {warningInfo} from '@renderer/api/index.ts';
//预警数据
let warningInfoList = ref([])
const getWarningInfo = async () => {
  let response = await warningInfo({})
  warningInfoList.value = response.data.ret.results
  let maxLength = warningInfoList.value.length
  nextTick(() => {
    setInterval(() => {
      warningInfoList.value.forEach((item, index) => {
        let aTransformY = parseInt(itemRefs[index].style.transform.substring(11))
        let bIndex = itemRefs[index].style.zIndex
        
        // console.log("aTransformY",aTransformY);
        if (aTransformY == -((maxLength - 1) * 50)) {
          aTransformY = 0
          itemRefs[index].children[1].style.display = 'block'
        } else {
          aTransformY -= 50
          itemRefs[index].children[1].style.display = 'none'
        }
        if(bIndex == (100 - maxLength+1)){
          bIndex = 100
        }else {
          bIndex -= 1
        }
        itemRefs[index].style.transform = `translateY(${aTransformY}px)`
        itemRefs[index].style.zIndex = bIndex
        // console.log('itemRefs[index]', itemRefs[index].style)
      })
    }, 3000)
  })
}

const itemRefs = []
const setItemRef = (el) => {
  if (el) {
    itemRefs.push(el)
  }
}
// console.log("itemRefs",itemRefs);
onMounted(async () => {
  await getWarningInfo()
  //初始化
  // nextTick(() => {
  //   setInterval(() => {
  //     warningInfoList.value.forEach((item, index) => {
  //       let aTransformY = parseInt(itemRefs[index].style.transform.substring(11))
  //       let bIndex = itemRefs[index].style.zIndex
        
  //       console.log("aTransformY",aTransformY);
  //       // debugger
  //       if (aTransformY == -((maxLength - 1) * 50)) {
  //         aTransformY = 0
  //         itemRefs[index].children[1].style.display = 'block'
  //       } else {
  //         aTransformY -= 50
  //         itemRefs[index].children[1].style.display = 'none'
  //       }
  //       if(bIndex == (100 - maxLength+1)){
  //         bIndex = 100
  //       }else {
  //         bIndex -= 1
  //       }
  //       itemRefs[index].style.transform = `translateY(${aTransformY}px)`
  //       itemRefs[index].style.zIndex = bIndex
  //       // console.log('itemRefs[index]', itemRefs[index].style)
  //     })
  //   }, 3000)
  // })
})
</script>

<style lang="less" scoped>
#demo {
  position: absolute;
  left: 40px;
  bottom: 30px;
  .box {
    width: 301px;
    height: 360px;
    // overflow: scroll;
    position: relative;
    .warning {
      width: 301px;
      height: 280px;
      position: absolute;
      .warning-title {
        height: 40px;
        background-image: url('../assets/img/title_bg.png');
        font-size: 18px;
        font-family:
          PingFang SC,
          PingFang SC-700;
        font-weight: 700;
        color: #ff6838;
        line-height: 40px;
        padding-left: 60px;
      }
      .warning-content {
        height: 240px;
        border-radius: 0px 0px 8px 8px;
        padding-top: 20px;
        box-sizing: border-box;
        background: rgba(255, 245, 209, 0.9);
        > .warning-time {
          height: 30px;
          font-size: 20px;
          font-family:
            PingFang SC,
            PingFang SC-400;
          font-weight: 500;
          color: #009f80;
          line-height: 30px;
          padding-left: 20px;
          box-sizing: border-box;
          position: relative;
          &::before {
            display: block;
            content: '';
            width: 3px;
            height: 3px;
            border-radius: 50%;
            background-color: #009f80;
            position: absolute;
            top: 50%;
            left: 3%;
          }
        }
        .warning-word {
          padding: 20px 30px;
          box-sizing: border-box;
          font-size: 20px;
          font-family:
            PingFang SC,
            PingFang SC-400;
          font-weight: 400;
          color: #313737;
          line-height: 30px;
        }
      }
    }
  }
}
</style>

如何没有接口,可以使用里面的假数据先测试


http://www.niftyadmin.cn/n/5035528.html

相关文章

蓝桥杯嵌入式创建第一个工程(点亮led灯)

蓝桥杯嵌入式创建第一个工程&#xff08;点亮led灯&#xff09; 一.keil导入stm32G431RX板级芯片包1.1 下载板级芯片包1.2 导入芯片包1.2.1 我们首先打开keil 点击Pack installer,如图**1.2.2 然后选中file→import 如图**1.2.3 选择我们下好的芯片包即可1.2.4 确定安装成功 二…

无涯教程-JavaScript - RAND函数

描述 RAND函数返回大于或等于0且小于1的均匀分布的随机实数。每次计算工作表时,都会返回一个新的随机实数。 语法 RAND ()争论 RAND函数没有参数。 Notes 您可以在a和b之间生成一个随机实数,其中 RAND ()*(b-a)a 如果要使用RAND生成随机数,但又不想每次计算单元格时都更改…

爬虫 — Scrapy 框架(二)

目录 一、Scrapy 核心文件1、spider2、Request3、构造 post 请求4、response 二、案例三、Scrapy 下载中间件1、执行顺序2、使用方法3、Download Middlewares 默认方法4、代理 IP4.1、工作原理4.2、分类4.3、查看 IP 地址4.4、常用代理 四、Scrapy 爬虫中间件五、Scrapy 下载图…

Linux 用户注意!GNOME 45 将影响所有扩展!

GNOME 45 是一次重要的升级&#xff0c;但对扩展的影响并不令人满意&#xff01; 每当 GNOME 升级&#xff0c;总会有一些扩展遭遇问题&#xff0c;这点并不新鲜。但如今&#xff0c;到了 GNOME 45&#xff0c;每个扩展都将面临问题&#xff01; 那么&#xff0c;究竟是什么原…

项目部署流程,域名解析,备案

每日鸡汤&#xff1a;每个你想要学习的瞬间都是未来的你像自己求救 假如你在部署一个网站域名是https://www.test.com 除了你要打包部署到一个服务器上之后&#xff0c;还需要有以下几个准备 购买域名www.test.com购买www.test.com的ssl证书&#xff0c;https协议用让架构组的…

华清远见第六课程day10作业

保纯 //保存按钮对应的槽函数 void Widget::on_saveBtn_clicked() {QString fileName QFileDialog::getSaveFileName(this, "保存文件", "./", "All(*.*);;Images (*.png *.xpm *.jpg);;Text files(*.txt);;XML …

C# 静态类和sealed类(密封类)的区别

网上看到很多文章写静态类&#xff0c;和密封类&#xff0c;但是鲜有它们的对比总结&#xff0c;在此简单总结一下&#xff1a; 静态类&#xff08;Static Class&#xff09;&#xff1a; 静态类不能被实例化&#xff0c;其成员都是静态的&#xff0c;可以通过类名直接访问。静…

React+Node——next.js 构建前后端项目

一、安装全局依赖 npm i -g create-next-app二、创建next项目 create-next-app react-next-demo //或 create-next-app react-next-demo --typescript三、加载mysql依赖 npm i -S mysql2四、运行项目 npm run dev五、创建db文件目录&#xff0c;目录下创建index.ts import…