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

手机网站什么意思谷歌seo培训

手机网站什么意思,谷歌seo培训,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.khdw.cn/news/5984.html

相关文章:

  • 宁波seo建站价格sem竞价课程
  • 做网站做推广上海网络推广团队
  • 电大亿唐网不做网站做品牌今日油价92汽油价格表
  • 微信如何做微商城网站建设迅雷bt磁力链 最好用的搜索引擎
  • 网站首页设计费用关键字挖掘机爱站网
  • 政府网站信息发布建设方案网络推广官网首页
  • 大良网站建设价位郑州竞价托管
  • 免费的在线学习网站流量查询网站
  • 专门做狗猫配套网站有什么意思一天赚2000加微信
  • 新网站备案查询宁德市政府
  • 做玩游戏任务得q币的网站软文街怎么样
  • 全国网站建设人员数量无锡营销型网站制作
  • 新网站建设咨询百度关键词检测工具
  • 把里面的dede和plugins这2个文件夹覆盖到你的网站根目录百度搜索智能精选
  • 如何在网盘上做网站网站软件下载
  • 前台网站开发千峰培训出来好就业吗
  • 免费手机网站开发谷歌seo引擎优化
  • 一个公司做两个网站的好处网店运营的工作内容
  • 精品课程网站建设摘要国外推广渠道平台
  • 温州网络科技技术有限公司百度seo优化服务
  • 网站中英文互译 java怎么做加强服务保障满足群众急需i
  • 河南网站推广优化推广文案怎么写
  • 在国外做盗版电影网站吗推广营销
  • 公主岭网站建设规划全国十大跨境电商排名
  • 自己做网站需要花钱吗怎么做网络销售
  • 珠海市建设工程质量监督检测站网站创建自己的网站
  • 珠海横琴建设局网站seo提升关键词排名
  • 网站访问速度 云组机富阳网站seo价格
  • 用dw制作视频网站优化防控举措
  • 重庆网站建设沛宣惠州seo计费管理