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

asp网站架设教程google play下载安装

asp网站架设教程,google play下载安装,找人做网站怎么找,郑州荥阳疫情最新消息一、场景描述 在很多网站的页面中都有轮播图,所以我想利用react.js和ts实现一个轮播图。自动轮播图已经在前面实现过了,如:https://blog.csdn.net/weixin_43872912/article/details/145622444?sharetypeblogdetail&sharerId145622444&a…

一、场景描述

在很多网站的页面中都有轮播图,所以我想利用react.js和ts实现一个轮播图。自动轮播图已经在前面实现过了,如:https://blog.csdn.net/weixin_43872912/article/details/145622444?sharetype=blogdetail&sharerId=145622444&sharerefer=PC&sharesource=weixin_43872912&spm=1011.2480.3001.8118。
思维导图可以在网站:https://download.csdn.net/download/weixin_43872912/90429355?spm=1001.2014.3001.5503下载高清原图。
在这里插入图片描述

二、问题拆解

轮播图(如下图轮播图所示)的实现可以分为三个:
第一个是自动轮播,就是每过一定的时间自动变成下一张图;
第二个是前后按钮的轮播,就是按左右的按钮,按左边的按钮时,跳转到前一张图,按右边的按钮时,跳转到后一张图。
第三个就是按底部的按钮切换轮播图。
在这里插入图片描述
在这里插入图片描述

三、相关知识

3.1 setTimeout与setInterval的用法与详解

setTimeout是指过了多久之后执行内部代码,一次性的;setInterval是每过多久就要执行一次代码。setTimeout里面开启计时器的话,就需要先过setTimeout的时间,然后过setInterval的一次时间才会运行第一次。

    var time = 0;setInterval(() => {time += 1;console.log("当前时间为" + time + "秒");}, 1000);let count = 0;//test setTimeout & setIntervalvar testTimeFunction = function () {setTimeout(() => {setInterval(() => {count += 1;console.log("count输出了" + count + "次");}, 3000);}, 3000);};testTimeFunction();

运行结果如下图所示:
在这里插入图片描述

3.2 react中的ref,利用ref获取dom元素,并对dom元素的class进行操作

import React, { useState, useRef } from "react”;const divRef = useRef<HTMLDivElement>(null);
//tsx部分
<div ref={divRef}><buttononClick={() => {console.log(divRef);let target = divRef.current as HTMLElement;  //ts里面要将其定义为htmlElement再操作target.classList.add("preActive");console.log(target);}}>打印ref</button>
</div>

四、轮播图的按钮部分实现

4.1 上一张和下一张的按钮实现

在这里插入图片描述
上一张和下一张的按钮部分其实就是自动轮播部分的手动操作,这部分的内容关注一下自动轮播部分和手动轮播部分的联动就可以了。
按钮事件:
跳转的按钮事件, 可以看到其实就是设置了一下currentIndex,这是一个state,更新之后会引起组件渲染,然后就会更新dom元素的class。

//跳转到上一张图的按钮事件。
<buttonclassName="carousel-control-prev"type="button"data-bs-target="#carouselExample"onClick={() => {//clearCrouselClass();setCurrentIndex((pre) => {let nowCurrentIndex =currentIndex.currentIndex - 1 === -1? img.length - 1: currentIndex.currentIndex - 1;let nowPreIndex =nowCurrentIndex - 1 === -1? img.length - 1: nowCurrentIndex - 1;let nownextIndex =nowCurrentIndex + 1 === img.length ? 0 : nowCurrentIndex + 1;return {preIndex: nowPreIndex,currentIndex: nowCurrentIndex,nextIndex: nownextIndex,};});}}
>
//跳转到下一张图的按钮事件。
<buttonclassName="carousel-control-next"type="button"data-bs-target="#carouselExample"onClick={() => {//clearCrouselClass();setCurrentIndex((pre) => {let nowCurrentIndex =currentIndex.currentIndex + 1 === img.length? 0: currentIndex.currentIndex + 1;let nowPreIndex =nowCurrentIndex - 1 === -1? img.length - 1: nowCurrentIndex - 1;let nownextIndex =nowCurrentIndex + 1 === img.length ? 0 : nowCurrentIndex + 1;return {preIndex: nowPreIndex,currentIndex: nowCurrentIndex,nextIndex: nownextIndex,};});}}
>

手动换图与自动轮播图之间的不和谐在于,手动换图后自动轮播还在执行会导致换两次可能,所以手动换图的时候需要停止自动轮播,结束后开启。
换图每次都会伴随着cunrrentIndex的变动,所以在这里我们用useEffect去检测cunrrentIndex的变化,只要变化就停止计时器,然后重新开启。 — 这里我用了异步

 //开启计时器,设置preIndex是当前index的前一个index(index-1),nextIndex是index+1const startAutoplay = async () => {interval.current = setInterval(() => {setCurrentIndex((preCurrentIndex) => {return {preIndex:((preCurrentIndex.currentIndex + 1) % img.length) - 1 === -1? img.length - 1: ((preCurrentIndex.currentIndex + 1) % img.length) - 1,currentIndex: (preCurrentIndex.currentIndex + 1) % img.length,nextIndex:((preCurrentIndex.currentIndex + 1) % img.length) + 1 === img.length? 0: ((preCurrentIndex.currentIndex + 1) % img.length) + 1,};});}, 3000);};const stopAutoPlay = () => {if (interval.current !== null) {clearInterval(interval.current as NodeJS.Timeout);interval.current = null;}};  useEffect(() => {clearInterval(interval.current as NodeJS.Timeout);interval.current = null;let target = imgRef.current as HTMLElement;console.log(target.childNodes);if (interval.current !== null) {stopAutoPlay();}if (interval.current === null) {   //避免因为定时器什么的缘故导致计时器多开startAutoplay();}//eslint-disable-next-line react-hooks/exhaustive-deps}, [currentIndex]);

4.2 底部小圆点按钮

这块部分就是将点击按钮所对应的图片转到主页。这个要实现用setCurrentIndex()就可以,但是要加动画,首先需要将目标页先移到上一页或者下一页的位置,准备平移至主页位置。所以,需要预先将目标图移到动画的起始位置。
在这里插入图片描述
如上图所示,如果是目标页是当前页的前面几页(目标页的index比currentIndex小)。需要提前把目标页移到上一页的位置,反之,移到下一页的位置。
移完之后设置currentIndex进行重新渲染。

if (index < currentIndex.currentIndex) {target.classList.add("preActive”);        //移到上一页的位置,class设为preActive// target1.classList.add("nextActive");// target1.classList.remove("active");setTimeout(() => {setCurrentIndex((pre) => {console.log(currentIndex);// let nextIndex = index + 1 === img.length ? 0 : index + 1;// if(nextIndex === pre.currentIndex) return;return {preIndex:index - 1 === -1 ? img.length - 1 : index - 1,currentIndex: index,nextIndex: pre.currentIndex,};});}, 10);
} else if (index > currentIndex.currentIndex) {target.classList.add("nextActive”);     //移到下一页的位置,class设为nextActive// target1.classList.add("preActive");// target1.classList.remove("active");console.log(currentIndex);setTimeout(() => {setCurrentIndex((pre) => {return {nextIndex:index + 1 === img.length ? 0 : index + 1,currentIndex: index,preIndex: pre.currentIndex,};});}, 10);
}

到此为止出现的问题:
和“自动轮播和前后按钮换图”之前的联动出现的错误是:当跳到前面页面的时候,下一页的类名没有nextActive所以跳到下一页可能没有动画;相同的跳到前面页的时候,上一页的类名没有preActive,可能没有翻页动画。因此,我们要保证当前页的前一页类名有preActive,后一页类名有nextActive.

<div
key={index}
className={`carousel-item ${index === currentIndex.currentIndex ? "active" : ""
}${index ===(currentIndex.currentIndex - 1 === -1? img.length - 1: currentIndex.currentIndex - 1) ||(index === currentIndex.preIndex )? "preActive": ""
} ${index ===(currentIndex.currentIndex + 1 === img.length? 0: currentIndex.currentIndex + 1) ||index === currentIndex.nextIndex? "nextActive": ""
}`}
>

这样写存在的错误是会造成同一个图片有preActive和nextActive的情况
在这里插入图片描述
解决方案:按键跳转前的主页是目标页的下一页:因此,当preIndex等于currentIndex+1不给preIndex

<div
key={index}
className={`carousel-item ${index === currentIndex.currentIndex ? "active" : ""
}${index ===(currentIndex.currentIndex - 1 === -1? img.length - 1: currentIndex.currentIndex - 1) ||//下面这部分代码就是 当preIndex等于currentIndex+1不给preIndex(index === currentIndex.preIndex &&currentIndex.preIndex !==(currentIndex.currentIndex + 1 === img.length? 0: currentIndex.currentIndex + 1))? "preActive": ""
} ${index ===(currentIndex.currentIndex + 1 === img.length? 0: currentIndex.currentIndex + 1) ||index === currentIndex.nextIndex? "nextActive": ""
}`}
>

在这里插入图片描述
五、遇到的问题

    useEffect(() => {console.log("停止计时器");console.log(currentIndex);clearInterval(interval.current as NodeJS.Timeout);interval.current = null;let target = imgRef.current as HTMLElement;console.log(target.childNodes);if (interval.current !== null) {stopAutoPlay();}//如果这里这样写会出现问题,如果在这三秒内点的话,就会导致setCurrentIndex的值都变成NaNsetTimeout(()=>{if (interval.current === null) {startAutoplay();}},3000)//eslint-disable-next-line react-hooks/exhaustive-deps}, [currentIndex]);//改完之后的版本useEffect(() => {console.log("停止计时器");console.log(currentIndex);clearInterval(interval.current as NodeJS.Timeout);interval.current = null;let target = imgRef.current as HTMLElement;console.log(target.childNodes);if (interval.current !== null) {stopAutoPlay();}
//改成异步函数,或者setTimeout的事件变短一点if (interval.current === null) {startAutoplay();}//eslint-disable-next-line react-hooks/exhaustive-deps}, [currentIndex]);

整个项目可以在下面链接下载:https://download.csdn.net/download/weixin_43872912/90429357?spm=1001.2014.3001.5501


文章转载自:
http://inimitably.pwkq.cn
http://pasteurellosis.pwkq.cn
http://abstainer.pwkq.cn
http://flouncing.pwkq.cn
http://inclinometer.pwkq.cn
http://formulate.pwkq.cn
http://hexylresorcinol.pwkq.cn
http://prig.pwkq.cn
http://confiscate.pwkq.cn
http://indoors.pwkq.cn
http://mongol.pwkq.cn
http://sacerdotal.pwkq.cn
http://shortage.pwkq.cn
http://gynecology.pwkq.cn
http://tintometer.pwkq.cn
http://disassembly.pwkq.cn
http://poplar.pwkq.cn
http://grumpy.pwkq.cn
http://epilation.pwkq.cn
http://modificator.pwkq.cn
http://hindoo.pwkq.cn
http://strass.pwkq.cn
http://lenient.pwkq.cn
http://uptight.pwkq.cn
http://needlecase.pwkq.cn
http://populist.pwkq.cn
http://rostellate.pwkq.cn
http://aminobenzene.pwkq.cn
http://prepreg.pwkq.cn
http://utter.pwkq.cn
http://staggard.pwkq.cn
http://espantoon.pwkq.cn
http://atamasco.pwkq.cn
http://gusla.pwkq.cn
http://chalicosis.pwkq.cn
http://nonterminating.pwkq.cn
http://toedrop.pwkq.cn
http://ossific.pwkq.cn
http://peoplehood.pwkq.cn
http://ampliate.pwkq.cn
http://husk.pwkq.cn
http://khanate.pwkq.cn
http://myoelastic.pwkq.cn
http://electrotypist.pwkq.cn
http://islamic.pwkq.cn
http://panentheism.pwkq.cn
http://cariosity.pwkq.cn
http://sculduddery.pwkq.cn
http://pirogi.pwkq.cn
http://sinarquist.pwkq.cn
http://watcom.pwkq.cn
http://expositor.pwkq.cn
http://cytogenics.pwkq.cn
http://nostoc.pwkq.cn
http://experientialism.pwkq.cn
http://essayist.pwkq.cn
http://babylonian.pwkq.cn
http://cutty.pwkq.cn
http://saury.pwkq.cn
http://neonatology.pwkq.cn
http://aitch.pwkq.cn
http://inconsistent.pwkq.cn
http://ras.pwkq.cn
http://vigia.pwkq.cn
http://anathematically.pwkq.cn
http://unbloody.pwkq.cn
http://begirt.pwkq.cn
http://jiggers.pwkq.cn
http://onchocercosis.pwkq.cn
http://surfboat.pwkq.cn
http://cathexis.pwkq.cn
http://inclasp.pwkq.cn
http://capework.pwkq.cn
http://bsb.pwkq.cn
http://quotable.pwkq.cn
http://gloam.pwkq.cn
http://bata.pwkq.cn
http://confluence.pwkq.cn
http://noble.pwkq.cn
http://larchwood.pwkq.cn
http://outer.pwkq.cn
http://transfinalization.pwkq.cn
http://legantine.pwkq.cn
http://pushful.pwkq.cn
http://amitriptyline.pwkq.cn
http://epitheliomatous.pwkq.cn
http://gasbag.pwkq.cn
http://compassion.pwkq.cn
http://copyfit.pwkq.cn
http://narita.pwkq.cn
http://estelle.pwkq.cn
http://diovular.pwkq.cn
http://bowls.pwkq.cn
http://toilless.pwkq.cn
http://partygoer.pwkq.cn
http://schoolmiss.pwkq.cn
http://rojak.pwkq.cn
http://glace.pwkq.cn
http://ile.pwkq.cn
http://machair.pwkq.cn
http://www.dt0577.cn/news/95341.html

相关文章:

  • 网题 做问卷的网站随机关键词生成器
  • wordpress无法登录后台显示500seo首页关键词优化
  • 分类目录网站怎么做厦门推广平台较好的
  • 网站建设中倒计时模板百度查重免费入口
  • 启航做网站怎么样谷歌搜索引擎免费入口 台湾
  • 毕业设计代做网站java如何让百度收录
  • 沈阳网站建设推广seo描述快速排名
  • wordpress 笔记插件下载优化大师下载安装
  • html网站模版有什么平台可以发广告
  • 新蔡县做网站收多少钱去了外包简历就毁了吗
  • web前端开发培训学校seo千享科技
  • 网站建设现状分析网络营销策划书包括哪些内容
  • 搜索引擎优化分析上海百度seo优化
  • 旅游网站介绍怎么写seo自己怎么做
  • 营销公司网站模板媒体发稿推广
  • 网站建设的知识点有哪些郴州网站建设
  • 网站公司说我们做的网站服务器不够用全网营销是什么意思
  • wordpress 网易云网站seo课程
  • 高端网站定做营销
  • 手机怎么复制网站模板谷歌搜索引擎入口
  • 网页设计分为几个部分搜索引擎优化的内容有哪些
  • cms系统排名北京谷歌seo公司
  • 政府网站模板 下载30条新闻摘抄
  • 泉州企业网站制作定制引擎搜索对人类记忆的影响
  • 互联网工作室暴利项目排名优化培训
  • 专业的个人网站建设哪家电商营销
  • 长春专业做网站公司淘宝定向推广
  • 怀仁网站建设网站网络营销
  • wordpress企业免费主题是什么qq关键词排名优化
  • 做网站图片素材在线编辑百度手机快速排名点击软件