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

永兴县网站建设蒙牛网站是谁做的

永兴县网站建设,蒙牛网站是谁做的,WordPress分享到笔记,传媒公司简介模板一、场景描述 我想写一个轮播图的程序,只是把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/a/451154.html

相关文章:

  • react学习——react-redux
  • 国内ui设计公司seo入门视频
  • 提供网站建设工具的品牌江安网站建设
  • 建筑设计找工作的网站上海装修公司排名有哪些
  • 正定seo绍兴seo计费
  • 面试经典150题[046]:存在重复元素 II(LeetCode 219)
  • dede网站如何换logo网站开发需要哪些证书
  • 静态企业网站模板下载网上书店网站建设毕业设计
  • 网站推广策划书 精品中信建设有限责任公司客户
  • cv领域接地气的方向
  • 快速网站开发介绍浙江建设网查询
  • 整站优化快速排名广州手机网站建设公司哪家好
  • 在线动画手机网站模板360推广登录入口官网
  • 设计网站推荐语长治企业网站建设
  • 网上做调查网站做短袖的网站
  • 想推网站目录源码宛城区网站推广
  • php手机网站后台源码电子商务有限公司有哪些
  • 工厂外贸网站建设ps怎么做网站logo
  • 恩平市网站建设国外设计网站网址
  • 广州网站服务涟源seo快速排名
  • IEEE/ASME Transactions on Mechatronics投稿流程和注意事项
  • 太原做企业网站现在去横琴会变黄码吗
  • TensorFlow深度学习实战(39)——机器学习实践指南
  • Python函数参数设计的实战逻辑
  • 网站设计字体最好用外网服务器租用
  • 关于药品网站建设策划书淘宝分销平台
  • 网站dw建设凯里门户网
  • Flash擦写均衡攻略:让嵌入式设备的时间记录更长寿
  • 快速了解并使用Matplotlib库
  • 潍坊模板建站平台销售人员培训课程有哪些