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

罗湖网站建设的公司哪家好百度网站名称和网址

罗湖网站建设的公司哪家好,百度网站名称和网址,怎么拥有自己的网站,免费看电视剧的网站有哪些一、场景描述 我想写一个轮播图的程序,只是把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.dtcms.com/wzjs/353349.html

相关文章:

  • 学做的网站基础蛋糕自建网站平台
  • 如何用easyui做网站seo评测论坛
  • wordpress外贸网站好用的模板西安今日头条新闻
  • 长沙市宁乡县建设局网站优化网站首页
  • 视频制作培训机构推荐优化教程网站推广排名
  • 展示网站全网网站推广
  • 教育培训网站建设正规app推广
  • 平顶山做网站公司在线外链工具
  • 杭州网站设计予尚搜索引擎优化的定义是什么
  • 网站设计宣传广告方案新闻头条最新消息30字
  • 客服外包公司怎么创立网站排名优化培训
  • 深圳城乡和住房建设局网站首页百度的seo关键词优化怎么弄
  • 十大免费跨境app西安网络推广优化培训
  • 电子商务网站建设与管理课程的目的中小企业管理培训课程
  • 深圳网站建设公盐城seo优化
  • 深圳网站建设服务哪一个便宜长沙seo优化排名推广
  • 荣成网站制作公司b站推广是什么意思
  • 成都网络优化网站建设企业宣传方式
  • 吉林省住房和城乡建设部网站软文
  • h5素材免费下载网优化设计单元测试卷答案
  • 济南大型网站建设宁波seo网络推广定制多少钱
  • 安贞网站建设公司百度公司排名多少
  • 各大高校的校园网站建设免费网站推广产品
  • 石家庄微网站建设小红书seo关键词优化多少钱
  • 网站基础建设和管理暂行办法沈阳百度seo关键词排名优化软件
  • 网站设计及建设合同邹平县seo网页优化外包
  • 网站建设青岛杭州seo论坛
  • 重庆做网站建设公司排名seo职业规划
  • 河南手机网站建设爱站关键词挖掘软件
  • 免费商城版网站百度竞价排名收费标准