当前位置: 首页 > news >正文

网站建设网址导航网游戏网站开发试验报告

网站建设网址导航网,游戏网站开发试验报告,彩票网站开发的,分析网站的优势和不足一、场景描述 我想写一个轮播图的程序,只是把bootstrap里面的轮播图拉过来就用上感觉不是很合适,然后我就想自己写自动轮播,因此,这篇文章里面只是自动轮播的部分,没有按键跟自动轮播的衔接部分。 Ps: 本文用的是函数…

一、场景描述

我想写一个轮播图的程序,只是把bootstrap里面的轮播图拉过来就用上感觉不是很合适,然后我就想自己写自动轮播,因此,这篇文章里面只是自动轮播的部分,没有按键跟自动轮播的衔接部分。
Ps: 本文用的是函数式组件,因为新版本的react函数式组件用的比较多。

二、相关知识

2.1 怎么在react.js中使用bootstrap

首先,在react中引入bootstrap。

npm install bootstrap

然后在index.js中引入bootstrap,在后续组件中使用bootstrap的时候就不需要再引入。
需要同时引入css文件和js文件。

import "bootstrap/dist/css/bootstrap.min.css";
import "bootstrap/dist/js/bootstrap";

2.2 useEffect钩子

useEffect钩子是在渲染之后运行的程序。有一个作用是:根据React state控制非React组件。
在部分的内容中有三种写的方式:

  useEffect(() => {//代码// eslint-disable-next-line react-hooks/exhaustive-deps}, []);   //后面的依赖数据是空的,就是只在第一次渲染的时候运行useEffect(() => {//代码// eslint-disable-next-line react-hooks/exhaustive-deps});    //后面没有依赖,每次渲染的时候都会运行useEffect(() => {//代码// eslint-disable-next-line react-hooks/exhaustive-deps},[img]);    //后面的依赖是img就只在img改变的时候再运行

2.3useRef钩子

ref和state之间的区别在于state变化之后组件会重新渲染,ref变化之后组件不会重新渲染。ref在整个组件生命周期内保持不变。

import { useRef } from "react”;let currentIndex1 = useRef(0); //定义ref
//html中代码
<button onClick={() => {console.log(currentIndex1.current);currentIndex1.current = currentIndex1.current + 1;
}}>+1</button>

三、轮播图的自动播放功能实现

这部分分为两个问题,一个是自动播放的index,自动播放就是定时器interval;另一个是通过class类定义当前显示的图片,然后设置动画。
这里定义index只能是state,因为ref变化不会渲染,这样根据index改变的class类可能就改变不了。
思路如下图所示。
在这里插入图片描述
整体代码如下:
Recommend.tsx

import { useRef } from "react";
import React, { useEffect, useState } from "react";
import hyRequest from "../../service/index";
const Recommend = function () {const [img, setImg] = useState<any[]>([]);let currentIndex1 = useRef(0);const [currentIndex, setCurrentIndex] = useState(0);useEffect(() => {hyRequest.get({ url: "/banner" }).then((res) => {console.log(res);setImg(res.banners);console.log(img);});// eslint-disable-next-line react-hooks/exhaustive-deps}, []);useEffect(() => {console.log("设置定时器");const interval = setInterval(() => {//console.log(img, img.length);//currentIndex.current = (currentIndex.current + 1) % img.length;//console.log(currentIndex.current);setCurrentIndex((preCurrentIndex) => (preCurrentIndex + 1) % img.length);}, 3000);return () => clearInterval(interval); // 清除定时器// eslint-disable-next-line react-hooks/exhaustive-deps}, [img]);return (<div><h2>推荐</h2><div id="carouselExample" className="carousel slide"><div className="carousel-inner">{img.map((item, index) => {//console.log(currentIndex, item, index);return (<divkey={index}className={`carousel-item ${index === currentIndex ? "active" : ""} ${index === currentIndex - 1 ? "preActive" : ""} ${index === currentIndex + 1 ? "nextActive" : ""}`}><img src={item.imageUrl} className="d-block w-100" alt="..." /></div>);})}</div><buttonclassName="carousel-control-prev"type="button"data-bs-target="#carouselExample"data-bs-slide="prev"><spanclassName="carousel-control-prev-icon"aria-hidden="true"></span><span className="visually-hidden">Previous</span></button><buttonclassName="carousel-control-next"type="button"data-bs-target="#carouselExample"data-bs-slide="next"><spanclassName="carousel-control-next-icon"aria-hidden="true"></span><span className="visually-hidden">Next</span></button></div><div>{currentIndex1.current}</div><buttononClick={() => {console.log(currentIndex1.current);currentIndex1.current = currentIndex1.current + 1;}}>+1</button></div>);
};export default Recommend;

index.less

.nextActive {display: block !important;transform: translateX(100%);opacity: 0;
}
.active {transform: none;opacity: 1;
}
.preActive {display: block !important;transform: translateX(-100%);opacity: 0;
}
http://www.yayakq.cn/news/773918/

相关文章:

  • 建设网站的心得体会新网站做外链
  • 自己如何高效有力的维护一个网站视觉设计工资一般多少
  • 二手车 东莞网站建设排名优化关键词公司
  • 网页作业班级网站怎么做怎么看网站有没有做地图
  • 网站广告联盟怎么做的网站开发需要大学吗
  • 时尚flash网站企业做网站有发展么
  • 做传销网站后果严重吗成立一个网站
  • html5门户网站模版二级建造师注册查询官网入口
  • 常州网站设计湛江公司电话写微信小程序需要什么软件
  • 做网页设计的步骤asp系统网站怎么做优化
  • 北京模板建站代理纪检监察门户网站建设论证报告
  • 做网站的一般要多钱数据表和网站建设的关系
  • 定制网站开发蒙特厦门专业网站建设公司
  • 优秀设计网站大全网站建设需要注意的问题
  • 查询关键词密度网站的网址有哪些攻击静态网站
  • 成都网站建设制作百度高级搜索功能
  • 手机app网站模板下载桐柏县建设局网站
  • 天津网站建设兼职wordpress 标签云页面
  • 长尾网站搜索引擎网站安全的必要性
  • 做国外搞笑网站企业网站推广17
  • dede 网站打开慢唐山企业做网站
  • 网站开发yuanmus怎么给wordpress加背景图
  • 赤峰专业网站建设橙色网站设计
  • 网站建立有哪些功能温州做网站价格
  • 网站存在风险什么意思网站建设 该如何选好域名
  • 网站建立计划书江苏有哪些做网站建设的公司
  • 网站建设策划书附录erp软件培训
  • 网站开发怎么谈欧美做受网站视频播放
  • 公司注册网上核名入口如何做网站内部优化
  • 陕西网站制作公司厦门最快seo