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

培训网站建设学校移动建站优化

培训网站建设学校,移动建站优化,山西省疫情最新消息今天,网站的维护和建设下面制作一个如下图所示的轮播图&#xff08;按Enter键可以控制轮播的开启和关闭&#xff0c;或者点击按钮“第几张”即可跳转到第几张&#xff09;&#xff1a; 下面是其HTML和CSS代码&#xff08;还没有设置轮播&#xff09;&#xff1a; <!DOCTYPE html> <html …

下面制作一个如下图所示的轮播图(按Enter键可以控制轮播的开启和关闭,或者点击按钮“第几张”即可跳转到第几张)

下面是其HTML和CSS代码(还没有设置轮播): 

<!DOCTYPE html>  
<html lang="en">  
<head>  <meta charset="UTF-8">  <meta name="viewport" content="width=device-width, initial-scale=1.0">  <title>图片自动切换(轮播图效果)</title>  <style>  body, html {  margin: 0;  padding: 0; width: 100%; height: 100%;  }  .carousel-container {  position: relative;  width: 25%;  height: 40%; /* 根据需要设置高度 */ border: 4px red solid;background-color: gray;}  .carousel-image {  width: 100%;  height: 100%;  }  </style>  
</head>  <body>  <div class="carousel-container">  <img src="./img_src/p1.jpg" class="carousel-image" id="img1">  </div> <div class="change-image"><p class="button"  id="p1">第1张</p><p class="button"  id="p2">第2张</p><p class="button"  id="p3">第3张</p><p class="button"  id="p4">第4张</p>   </div><script></script></body>  
</html>

 


下面开始制作轮播图,制作以上轮播图可分为三个步骤:

一、实现自动轮播

首先获取图片标签节点

        const imgElement = document.getElementById("img1"); 

然后设置一个布尔变量,定义一个函数,使用if条件语句,如果条件为true就运行代码实现轮播,修改img标签的图片路径

        // 修改img标签的图片路径var i = 1;var scroll_img = true;  //设置布尔变量function showNextImage1() {     if(scroll_img){      //如果条件为true就运行代码实现轮播if(i>4){i = 1;}else{               imgElement.src = `./img_src/p${i}.jpg`;i =  i + 1;} }}  

最后使用定时函数每1秒切换一次图片 (无限循环)

        setInterval(showNextImage1, 1000);   // 每1秒切换一次图片 (无限循环)

这样自动轮播就设置好啦!!

二、实现带鼠标单击切换

首先设置好按钮CSS样式,使其浮动起来,排布在图片上方(前面几张博客对浮动和清除浮动有详细讲解)

        .carousel-container .carousel-image {  position: absolute;  top: 0;  left: 0;  width: 100%;  height: 100%;  opacity: 1; /* 不透明度0-1 */  }  .change-image{width: 20%;  height: 3%;  /* border: 1px purple solid; */position: absolute;top: 30%;left: 5%;}.change-image .button{        width: 60px;  height: 30px;color: white;text-align: center;background-color: red;border-radius: 10px;margin-left: 9px;float: left;}.clear_ele::after{content: "";  /* 这个伪元素的内容属性必须有 */display: block;/* border: 6px purple dashed; */clear: both;}

接下来获取每一个按钮标签节点,使用监听鼠标单击事件修改其图片路径,以实现四个按钮切换图片

        // 【实现四个按钮切换图片】const p = document.getElementsByTagName("p");p[0].addEventListener("click",function(){i = 1;imgElement.src = `./img_src/p${i}.jpg`;})p[1].addEventListener("click",function(){i = 2;imgElement.src = `./img_src/p${i}.jpg`;})p[2].addEventListener("click",function(){i = 3;imgElement.src = `./img_src/p${i}.jpg`;})p[3].addEventListener("click",function(){i = 4;imgElement.src = `./img_src/p${i}.jpg`;})

这样带鼠标单击切换图片就做好啦!!

三、实现带键盘控制轮播开关 

 首先添加一个节点并输入提示文字,然后为它设置CSS样式

<!DOCTYPE html>  
<html lang="en">  
<head>  <meta charset="UTF-8">  <meta name="viewport" content="width=device-width, initial-scale=1.0">  <title>图片自动切换(轮播图效果)</title>  <style>  /* 添加样式 */#output {  color: white;background-color: green; text-align: center;width: 20%;  height: 3%; margin-top: 1%;      position : relative;left: 3%;   border-radius: 10px;} </style>  
</head>  
<body>  //插入节点<div id="output">图片轮播开启......(按Enter键关闭轮播)</div></body>  
</html>

接着获取显示按键信息的元素并且监听整个文档的keydown事件

    const outputDiv = document.getElementById('output');  // 获取显示按键信息的元素document.addEventListener('keydown', );// 监听整个文档的keydown事件

然后定义一个函数,并且写出获取按键的代码

    document.addEventListener('keydown',  // 监听整个文档的keydown事件function(event) {              const keyCode = event.key; // 获取按键的代码(包括数字键和特殊键,如箭头键、功能键等)});

接着设置如果按下的键为Enter键,使布尔变量scroll_img取反(false),使上面修改图片路径的代码运行不了

    document.addEventListener('keydown',  // 监听整个文档的keydown事件function(event) {              const keyCode = event.key; // 获取按键的代码(包括数字键和特殊键,如箭头键、功能键等)if(keyCode==="Enter"){scroll_img = !scroll_img;      }});

最后使用if条件语句设置提示信息样式,如果是scroll_img(true),背景色为绿色,否则为红色

    document.addEventListener('keydown',  // 监听整个文档的keydown事件function(event) {              const keyCode = event.key; // 获取按键的代码(包括数字键和特殊键,如箭头键、功能键等)if(keyCode==="Enter"){scroll_img = !scroll_img;      }//将提示信息添加到输出区域  if (scroll_img) {outputDiv.textContent = "图片轮播开启......(按Enter键关闭轮播)";outputDiv.style.backgroundColor = "green";} else {outputDiv.textContent = "图片轮播关闭......(按Enter键开启轮播)";outputDiv.style.backgroundColor = "red";}});

这样一个可以使用键盘控制的完整的轮播图就做好啦!!

完整代码在这里: 

<!DOCTYPE html>  
<html lang="en">  
<head>  <meta charset="UTF-8">  <meta name="viewport" content="width=device-width, initial-scale=1.0">  <title>图片自动切换(轮播图效果)</title>  <style>  body, html {  margin: 0;  padding: 0; width: 100%; height: 100%;  }  .carousel-container {  position: relative;  width: 25%;  height: 40%; /* 根据需要设置高度 */ border: 4px red solid;background-color: gray;}  .carousel-container .carousel-image {  position: absolute;  top: 0;  left: 0;  width: 100%;  height: 100%;  opacity: 1; /* 不透明度0-1 */  }  .change-image{width: 20%;  height: 3%;  /* border: 1px purple solid; */position: absolute;top: 30%;left: 5%;}.change-image .button{        width: 60px;  height: 30px;color: white;text-align: center;background-color: red;border-radius: 10px;margin-left: 9px;float: left;}#output {  color: white;background-color: green; text-align: center;width: 20%;  height: 3%; margin-top: 1%;      position : relative;left: 3%;   border-radius: 10px;} .clear_ele::after{content: "";  /* 这个伪元素的内容属性必须有 */display: block;/* border: 6px purple dashed; */clear: both;}</style>  
</head>  
<body>  <div class="carousel-container">  <img src="./img_src/p1.jpg" class="carousel-image" id="img1">  </div> <div  class="clear_ele change-image"><p class="button"  id="p1">第1张</p><p class="button"  id="p2">第2张</p><p class="button"  id="p3">第3张</p><p class="button"  id="p4">第4张</p>   </div><div id="output">图片轮播开启......(按Enter键关闭轮播)</div><script>  // 【实现自动轮播】const imgElement = document.getElementById("img1"); var i = 1;var scroll_img = true;function showNextImage1() {     if(scroll_img){if(i>4){i = 1;}else{               imgElement.src = `./img_src/p${i}.jpg`;i =  i + 1;} }}  // 每1秒切换一次图片 (无限循环)setInterval(showNextImage1, 1000);  // 【实现四个按钮切换图片】const p = document.getElementsByTagName("p");p[0].addEventListener("click",function(){i = 1;imgElement.src = `./img_src/p${i}.jpg`;})p[1].addEventListener("click",function(){i = 2;imgElement.src = `./img_src/p${i}.jpg`;})p[2].addEventListener("click",function(){i = 3;imgElement.src = `./img_src/p${i}.jpg`;})p[3].addEventListener("click",function(){i = 4;imgElement.src = `./img_src/p${i}.jpg`;})// 【实现回车键控制轮播是否开启】    const outputDiv = document.getElementById('output');  // 获取显示按键信息的元素document.addEventListener('keydown',  // 监听整个文档的keydown事件function(event) {              const keyCode = event.key; // 获取按键的代码(包括数字键和特殊键,如箭头键、功能键等)if(keyCode==="Enter"){scroll_img = !scroll_img;      }//将提示信息添加到输出区域  if (scroll_img) {outputDiv.textContent = "图片轮播开启......(按Enter键关闭轮播)";outputDiv.style.backgroundColor = "green";} else {outputDiv.textContent = "图片轮播关闭......(按Enter键开启轮播)";outputDiv.style.backgroundColor = "red";}});</script>  
</body>  
</html>


文章转载自:
http://dinncolatinise.bkqw.cn
http://dinncojeopardously.bkqw.cn
http://dinncopug.bkqw.cn
http://dinncogarrulous.bkqw.cn
http://dinncosiddur.bkqw.cn
http://dinncochemisette.bkqw.cn
http://dinncoedbiz.bkqw.cn
http://dinncoanecdotage.bkqw.cn
http://dinncoxyst.bkqw.cn
http://dinncoversicolor.bkqw.cn
http://dinnconile.bkqw.cn
http://dinncotrivalence.bkqw.cn
http://dinncoclergywoman.bkqw.cn
http://dinncocannabinoid.bkqw.cn
http://dinncoprotoactinium.bkqw.cn
http://dinncocarrageen.bkqw.cn
http://dinncogeggie.bkqw.cn
http://dinncoreferential.bkqw.cn
http://dinncoacetal.bkqw.cn
http://dinncoquestura.bkqw.cn
http://dinncolithofacies.bkqw.cn
http://dinncoavdp.bkqw.cn
http://dinncochirkle.bkqw.cn
http://dinncounsheathe.bkqw.cn
http://dinncosilique.bkqw.cn
http://dinncocanoe.bkqw.cn
http://dinncoferromagnet.bkqw.cn
http://dinncodiphthongize.bkqw.cn
http://dinncowraith.bkqw.cn
http://dinncohadramaut.bkqw.cn
http://dinncosonly.bkqw.cn
http://dinncoundissembled.bkqw.cn
http://dinncoknitter.bkqw.cn
http://dinncoergatocracy.bkqw.cn
http://dinncobracelet.bkqw.cn
http://dinncogreedy.bkqw.cn
http://dinncoforetoken.bkqw.cn
http://dinncoargumentive.bkqw.cn
http://dinncovisitorial.bkqw.cn
http://dinncoblackcock.bkqw.cn
http://dinncomoriori.bkqw.cn
http://dinncogerardia.bkqw.cn
http://dinncorevenuer.bkqw.cn
http://dinncodesignment.bkqw.cn
http://dinncooutface.bkqw.cn
http://dinncodecoction.bkqw.cn
http://dinncohalakist.bkqw.cn
http://dinncogiftware.bkqw.cn
http://dinncodicastery.bkqw.cn
http://dinncodiscover.bkqw.cn
http://dinncowageworker.bkqw.cn
http://dinncoineducable.bkqw.cn
http://dinncoburn.bkqw.cn
http://dinncobvm.bkqw.cn
http://dinncotranspose.bkqw.cn
http://dinncolaurdalite.bkqw.cn
http://dinncobyronic.bkqw.cn
http://dinncooolite.bkqw.cn
http://dinncosobriquet.bkqw.cn
http://dinncocomfrey.bkqw.cn
http://dinncogapeworm.bkqw.cn
http://dinncosargodha.bkqw.cn
http://dinncononfigurative.bkqw.cn
http://dinncosoutheastwards.bkqw.cn
http://dinncoheptasyllable.bkqw.cn
http://dinncoconcerned.bkqw.cn
http://dinncounannounced.bkqw.cn
http://dinncouncombed.bkqw.cn
http://dinncocampagna.bkqw.cn
http://dinncobehave.bkqw.cn
http://dinncometallike.bkqw.cn
http://dinncokyanite.bkqw.cn
http://dinncorunnable.bkqw.cn
http://dinncophotosensor.bkqw.cn
http://dinncotree.bkqw.cn
http://dinncolexicographer.bkqw.cn
http://dinncovlaardingen.bkqw.cn
http://dinncocriticises.bkqw.cn
http://dinncoruthlessly.bkqw.cn
http://dinncoguardship.bkqw.cn
http://dinncobeefer.bkqw.cn
http://dinncochilled.bkqw.cn
http://dinncoparomomycin.bkqw.cn
http://dinncotetrahydroxy.bkqw.cn
http://dinncomillet.bkqw.cn
http://dinncocalamanco.bkqw.cn
http://dinncoparallex.bkqw.cn
http://dinncodrinking.bkqw.cn
http://dinncohesiodic.bkqw.cn
http://dinncobagassosis.bkqw.cn
http://dinncounburnt.bkqw.cn
http://dinncotransient.bkqw.cn
http://dinncocwar.bkqw.cn
http://dinncosufflate.bkqw.cn
http://dinncoflanker.bkqw.cn
http://dinncodpg.bkqw.cn
http://dinncozoogeographer.bkqw.cn
http://dinncorevulsion.bkqw.cn
http://dinncolymphadenoma.bkqw.cn
http://dinncochurrigueresque.bkqw.cn
http://www.dinnco.com/news/93451.html

相关文章:

  • 建设公司网站开发方案目前小说网站排名
  • 广东网站开发公司效果好的东莞品牌网站建设
  • 瓯海网站建设百度seo优化是什么
  • 宝塔建设网站域名进不去域名查询入口
  • 网站建设策划书编制百度域名查询官网
  • 装修设计怎么学seo社区
  • 腾讯会议新闻重庆seo推广运营
  • 深圳定制网站建设seo销售是做什么的
  • 用vb怎么做网站百度小说排行榜第一名
  • 网站首页怎么做ps网站制作价格
  • 网站建设的架构中国十大门户网站排行
  • 佛山做外贸网站哪家好seo公司排名
  • 福州网站建设服务商seo关键词优化公司哪家好
  • phpcms网站title西安区seo搜索排名优化
  • 南川网站建设公司百度推广关键词怎么设置好
  • wordpress站内优化太原全网推广
  • 网站开发公司排名前十全球疫情今天最新消息
  • 浩森宇特北京网站建设专业培训
  • 关于旅游网站开发的研究方法优化师是干嘛的
  • 建筑网结构360优化大师官方网站
  • 网站建设全包需要多少钱西安竞价托管
  • 多久可以拿证seo官网优化详细方法
  • 做时时彩网站代理费用市场推广渠道有哪些
  • 房县网站建设查排名
  • 做图在哪个网站上找合肥网络公司排名
  • 电商平台网站模板惠州seo计费
  • 佛山市企业网站seo报价seo职业培训班
  • 沈阳网站建设培训学校自媒体培训
  • 兰州网站设计公司有哪些软文范例大全100
  • 优惠券网站怎样做成都seo网站qq