无缝轮播图 React+Ts类 React+Ts+hook vue3.0 +hook

React类

index.tsx

import React, { Component, createRef } from 'react';
import './index.less';
export default class index extends Component {
    x: number = 0;
    dix: number = 0;
    index: number = 1;
    list = [3, 0, 1, 2, 3, 0];
    box = createRef<HTMLDivElement>();
    Getbox = () => this.box.current as HTMLDivElement;
    start = (ev: React.TouchEvent) => {
        this.dix = ev.changedTouches[0].pageX;
        if (this.index === this.list.length - 1) this.index = 1;
        if (this.index === 0) this.index = this.list.length - 2;
        document.ontouchmove = this.move.bind(this);
        document.ontouchend = this.end.bind(this);
        this.Getbox().style.transition = 'none';
    };
    move = (ev: TouchEvent) => {
        this.x = ev.targetTouches[0].pageX - this.dix;
        this.Getbox().style.left = this.index * -100 + 'vw';
    };
    end = (ev: TouchEvent) => {
        if (this.x < -50) this.index++;
        if (this.x > 50) this.index--;
        this.Getbox().style.left = this.index * -100 + 'vw'; this.Getbox().style.transition = '.3s all';
    };
    render() {
        return (
            <div className='Swiper' onTouchStart={this.start.bind(this)}>
                <div className="swipers" ref={this.box}>
                    {this.list.map((item, index) => (<div key={index} className="swiper-item">{item}</div>))}
                </div>
            </div>
        )
    }
}

index.less

.Swiper {
  width: 750px;
  height: 100vh;
  overflow: hidden;
  position: relative;
  .swipers {
    width: calc(100vw * 6);
    display: flex;
    position: absolute;
    left: -100vw;
  }
  .swiper-item {
    transition: 0.3s all;
    width: 100vw;
    line-height: 300px;
    color: #fff;
    font-size: 50px;
    text-align: center;
  }
  .swiper-item:nth-child(1) {
    background-color: pink;
  }
  .swiper-item:nth-child(2) {
    background-color: red;
  }
  .swiper-item:nth-child(3) {
    background-color: yellow;
  }
  .swiper-item:nth-child(4) {
    background-color: greenyellow;
  }
  .swiper-item:nth-child(5) {
    background-color: pink;
  }
  .swiper-item:nth-child(6) {
    background-color: red;
  }
}

React+Ts+hook

index.tsx

import React, { useRef } from 'react'
import './index.less'
export default function Wflbt() {
    const arr = [3, 0, 1, 2, 3, 0]
    let x: number = 0
    let dix: number = 0
    let num: number = 1
    const box = useRef<HTMLDivElement | null>(null)
    const Getbox = () => box.current as HTMLDivElement
    const Fnstart = (e: React.TouchEvent) => {
        dix = e.changedTouches[0].pageX
        if (num === arr.length - 1) num = 1
        if (num === 0) num = arr.length - 2
        Getbox().style.left = num * -100 + 'vw'
        document.ontouchmove = Fnmove
        Getbox().style.transition = 'none'
    }
    const Fnmove = (e: TouchEvent) => {
        x = e.changedTouches[0].pageX - dix
        document.ontouchend = Fnend
    }
    const Fnend = () => {
        if (x < -50) num++
        if (x > 50) num--
        Getbox().style.left = num * -100 + 'vw'
        Getbox().style.transition = '.3s all'
    }
    return (
        <div className="Swiper" onTouchStart={Fnstart}>
            <div className="Swipers" ref={box}>
                {
                    arr.map((item, index) => (
                        <div className="item" key={index}>{item}</div>
                    ))
                }
            </div>
        </div>
    )
}

index.less

.Swiper {
  overflow: hidden;
  position: relative;
  width: 100vw;
  height: 100vh;
  .Swipers {
    display: flex;
    width: calc(6 * 100vw);
    position: absolute;
    left: -100vw;
    .item {
      line-height: 20vh;
      width: 100vw;
      text-align: center;
      background-color: red;
    }
    .item:nth-child(1){
        background-color: red;
    }
    .item:nth-child(2){
        background-color: pink;
    }
    .item:nth-child(3){
        background-color: skyblue;
    }
    .item:nth-child(4){
        background-color: yellow;
    }
    .item:nth-child(5){
        background-color: red;
    }
    .item:nth-child(6){
        background-color: pink;
    }
  }
}

Vue3.0+hook

<template>
  <div class="Swiper" v-on:touchstart="Fnstart">
    <div class="Swipers" ref="box">
      <div class="item" v-for="(item, index) in arr" :key="index">
        {{ item }}
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from "@vue/reactivity";
const arr = [3, 0, 1, 2, 3, 0];
let x: number = 0;
let dix: number = 0;
let num: number = 1;
const box = ref<HTMLDivElement | null>(null);
const Getbox = () => box.value as HTMLDivElement;
const Fnstart = (e: TouchEvent) => {
  dix = e.changedTouches[0].pageX;
  if (num === arr.length - 1) num = 1;
  if (num === 0) num = arr.length - 2;
  Getbox().style.left = num * -100 + "vw";
  document.ontouchmove = Fnmove;
  Getbox().style.transition = "none";
};
const Fnmove = (e: TouchEvent) => {
  x = e.changedTouches[0].pageX - dix;
  document.ontouchend = Fnend;
};
const Fnend = () => {
  if (x < -50) num++;
  if (x > 50) num--;
  Getbox().style.left = num * -100 + "vw";
  Getbox().style.transition = ".3s all";
};
</script>

<style lang="less" scoped>
.Swiper {
  overflow: hidden;
  position: relative;
  width: 100vw;
  height: 100vh;
  .Swipers {
    display: flex;
    width: calc(6 * 100vw);
    position: absolute;
    left: -100vw;
    .item {
      line-height: 20vh;
      width: 100vw;
      text-align: center;
      background-color: red;
    }
    .item:nth-child(1) {
      background-color: red;
    }
    .item:nth-child(2) {
      background-color: pink;
    }
    .item:nth-child(3) {
      background-color: skyblue;
    }
    .item:nth-child(4) {
      background-color: yellow;
    }
    .item:nth-child(5) {
      background-color: red;
    }
    .item:nth-child(6) {
      background-color: pink;
    }
  }
}
</style>

### 比较 VueReact Hooks #### 使用方式差异 Vue 提供了一种声明式的模板语法,允许开发者通过HTML扩展来描述UI[^3]。而React Hooks则提供了一系列函数,用于在不编写的情况下使用state和其他React特性。Hooks使得逻辑可以被抽取出来并重用,在组件之间共享业务逻辑变得更为容易。 #### 数据绑定机制的不同 Vue内置了双向数据流的支持,这意呸着当模型改变时视图会自动更新;反之亦然。这种设计简化了许多场景下的开发工作量。相比之下, React采用单向数据流动的方式,即父级传递props给子级,并且任何状态的变化都需要显式地触发重新渲染过程[^2]。 #### 组件定义上的区别 对于Vue而言,一个完整的Web应用程序通常由多个小型独立的.vue文件组成,这些文件包含了<template>、<script>以及<style>标签部分。而在React项目里,则更多依赖于JSX语法混合JavaScript表达式与标记结构一起书写,配合上Hooks之后可以让无状态功能性组件也拥有生命周期方法等功能[^1]。 ```jsx // React Hook 示例 import React, { useState } from &#39;react&#39;; function Example() { const [count, setCount] = useState(0); return ( <div> <p>You clicked {count} times</p> <button onClick={() => setCount(count + 1)}> Click me </button> </div> ); } ``` ```html <!-- Vue 单文件组件 --> <template> <div id="app"> <p>{{ message }}</p> <button v-on:click="incrementCounter">增加计数器</button> </div> </template> <script> export default { data () { return { counter: 0, message: `点击次数 ${this.counter}` } }, methods: { incrementCounter() { this.counter += 1; this.message = `点击次数 ${this.counter}`; } } }; </script> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

红烧四喜丸子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值