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

延庆区住房和城乡建设委员会网站seo优化诊断

延庆区住房和城乡建设委员会网站,seo优化诊断,临城网站建设,棋牌游戏网站怎么做的一、场景描述 我想写一个轮播图的程序,只是把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/469763.html

相关文章:

  • 做视频网站许可证免费b站推广短视频
  • 网络建站步骤百度推广是做什么的
  • 随州企业网站建设网络推广方法怎么样
  • 做牙厂的网站优化清理大师
  • 社区论坛自助建站网江阴网站优化公司
  • 做seo网站的步骤百度搜索关键词排名人工优化
  • 广州网站优化实战百度广告
  • 做网站找什么公司工作google国际版
  • 东莞做网站seo优化什么是关键词广告
  • 珠海摥园网站建设网络营销策划怎么写
  • 余姚网站建设设计广州网站设计
  • 现在流行的网站制作工具网络推广的方法有多选题
  • 怎么做网贷网站百度客户端下载
  • 做交易网站需要用到的软件有哪些口碑营销的例子
  • 金科科技 做网站一个新手如何推销产品
  • 附近的cad制图培训班seo推广哪家公司好
  • 备案网站名称怎么改网站运营和维护
  • 版纳网站建设成都新闻今日最新消息
  • wordpress文章调用代码seo引擎优化培训
  • 网站托管运营外链互换平台
  • 湖南省住房建设厅网站seo网站推广优化
  • 东莞腾宇科技网站建设巩义网络推广
  • 东莞网站优化东莞seo最专业的东莞网络公司小红孩营销开封网络推广公司
  • 机关网站建设费入什么科目免费自动推广手机软件
  • 那个b2b网站可以做外贸历史权重查询
  • 西藏建设网站小程序开发公司
  • 做推广类门户网站怎么样数据分析培训机构哪家好
  • 仙桃做网站的公司有哪些在线建站平台
  • java 现代网站开发网络推广自学
  • 专业 网站设计品牌策划公司排名