热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

开发笔记:原生js实现轮播图

篇首语:本文由编程笔记#小编为大家整理,主要介绍了原生js实现轮播图相关的知识,希望对你有一定的参考价值。如何使用原生js实现轮播图效果呢,

篇首语:本文由编程笔记#小编为大家整理,主要介绍了原生js实现轮播图相关的知识,希望对你有一定的参考价值。


如何使用原生js实现轮播图效果呢,现在带着大家做一个小小的例子

  先说一下这次的轮播图需要实现的功能点: 

    1.3s自动切换图片,图片切换时提示点跟随切换
    2.鼠标划到图片上,自动切换轮播图停止
    3.指示点划过切换对应的图片,图片切换时提示点跟随切换
    4.点击上一页下一页按钮切换图片


css代码部分



1 /*初始化浏览器默认样式*/
2 *{
3 margin: 0;
4 padding: 0;
5 }
6 /*sowingMap*/
7 .sowingMap{
8 width: 800px;
9 height: 500px;
10 margin: 0 auto;
11 position: relative;
12 overflow: hidden;
13 }
14 /*imgPart*/
15 .imgPart{
16 width: 800px;
17 height: 500px;
18 position: absolute;
19 }
20 /*imgSwitch*/
21 .imgSwitch{
22 width: 800px;
23 height: 500px;
24 position: absolute;
25 list-style: none;
26 display: none;
27 cursor: pointer;
28 }
29 .imgSwitch img{
30 width: 100%;
31 height: 500px;
32 }
33 .imgShow{
34 display: block;
35 }
36 /*spotPart*/
37 .spotPart{
38 position: absolute;
39 bottom: 0;
40 height: 40px;
41 left: 36%;
42 }
43 .spotPart p{
44 width: 20px;
45 height: 20px;
46 border-radius: 100%;
47 background-color: #fff;
48 float: left;
49 margin-left: 20px;
50 cursor: pointer;
51 }
52 /*提示点的选中颜色*/
53 .spotPart .spotColor{
54 background-color: #f00;
55 }
56 /*上一张下一张切换部分*/
57 .preImg , .nextImg{
58 width: 70px;
59 height: 70px;
60 border-radius: 100%;
61 background-color: rgba(255,255,255,0.5);
62 text-align: center;
63 line-height: 70px;
64 font-size: 30px;
65 color: #f5f5f5;
66 position: absolute;
67 top: 190px;
68 cursor: pointer;
69 display: none;
70 }
71 .preImg{
72 left: -35px;
73 text-indent: 25px;
74 }
75 .nextImg{
76 right: -35px;
77 text-indent: -25px;
78 }


css代码部分

html代码部分


1 <div class="sowingMap">
2 <ul class="imgPart">
3 <li class="imgSwitch imgShow"><img src="img/1.jpg"/>li>
4 <li class="imgSwitch"><img src="img/2.jpg"/>li>
5 <li class="imgSwitch"><img src="img/3.jpg"/>li>
6 <li class="imgSwitch"><img src="img/4.jpg"/>li>
7 <li class="imgSwitch"><img src="img/5.jpg"/>li>
8 ul>
9 <div class="spotPart">
10 <p class="spotColor">p>
11 <p>p>
12 <p>p>
13 <p>p>
14 <p>p>
15 div>
16 <div class="loopChange">
17 <p class="preImg"><p>
18 <p class="nextImg">>p>
19 div>
20 div>


js代码部分


1 //定义自动轮播的定时器
2 var startLoop = null;
3 //获取所有的li 和 p标签
4 var li_v = document.querySelectorAll("li");
5 var p_v = document.querySelectorAll(".spotPart p");
6 var sowingMap = document.querySelector(\'.sowingMap\');
7 var p_change = document.querySelectorAll(\'.loopChange p\');
8 //业务1:实现3s钟自动循环切换图片,图片切换时提示点跟随切换
9 var num = 0;
10 function loopSetInterval() {
11 clearInterval(startLoop);
12 startLoop = setInterval(function() {
13 for(var i = 0; i ) {
14 li_v[i].setAttribute("class", "imgSwitch");
15 p_v[i].setAttribute("class", " ");
16 }
17 if(num >= li_v.length - 1) {
18 num = 0;
19 } else {
20 num++;
21 }
22 li_v[num].setAttribute("class", "imgSwitch imgShow");
23 p_v[num].setAttribute("class", "spotColor");
24 }, 3000);
25 }
26 loopSetInterval();
27
28 //业务2:鼠标划到图片上,轮播图停止自动切换,划出后继续播放
29 for(var i = 0; i ) {
30 li_v[i].Onmouseover= function() {
31 clearInterval(startLoop);
32 }
33 li_v[i].Onmouseout= function() {
34 loopSetInterval();
35 }
36 }
37
38 //业务三:指示点划过切换对应的图片,图片切换时提示点跟随切换
39 for(var i = 0; i ) {
40 p_v[i].index = i;
41 p_v[i].Onmouseover= function() {
42 clearInterval(startLoop);
43 for(var i = 0; i ) {
44 li_v[i].setAttribute("class", "imgSwitch");
45 p_v[i].setAttribute("class", " ");
46 }
47 this.setAttribute("class", "spotColor");
48 li_v[this.index].setAttribute("class", "imgSwitch imgShow");
49 }
50 p_v[i].Onmouseout= function() {
51 loopSetInterval();
52 }
53 }
54
55 //业务四:点击上一页下一页切换
56 sowingMap.Onmouseover= function () {
57 for (var i=0;i) {
58 p_change[i].style.display = "block";
59 }
60 }
61 sowingMap.Onmouseout= function () {
62 for (var i=0;i) {
63 p_change[i].style.display = "none";
64 }
65 }
66 //点击切换上一张
67 p_change[0].Onclick= function () {
68 console.log(num)
69 for(var i = 0; i ) {
70 li_v[i].setAttribute("class", "imgSwitch");
71 p_v[i].setAttribute("class", " ");
72 }
73 if (num <= 0) {
74 num = 4;
75 li_v[num].setAttribute("class", "imgSwitch imgShow");
76 p_v[num].setAttribute("class", "spotColor");
77 } else if(num <= 4){
78 li_v[num-1].setAttribute("class", "imgSwitch imgShow");
79 p_v[num-1].setAttribute("class", "spotColor");
80 num--;
81 }
82 }
83 //点击切换下一张
84 p_change[1].Onclick= function () {
85 console.log(num)
86 for(var i = 0; i ) {
87 li_v[i].setAttribute("class", "imgSwitch");
88 p_v[i].setAttribute("class", " ");
89 }
90 if (num >= 4) {
91 num = 0;
92 li_v[num].setAttribute("class", "imgSwitch imgShow");
93 p_v[num].setAttribute("class", "spotColor");
94 } else if(num >= 0){
95 li_v[num+1].setAttribute("class", "imgSwitch imgShow");
96 p_v[num+1].setAttribute("class", "spotColor");
97 num++;
98 }
99 }

 

 

 

好了,一个原生的js代码实现轮播图效果就大功告成了,如果你想使用更简单的办法,可以参考我使用jQuery实现的轮播图效果:

https://www.cnblogs.com/qdclub/p/9782921.html

 



推荐阅读
  • CSS3选择器的使用方法详解,提高Web开发效率和精准度
    本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
  • 本文介绍了C#中数据集DataSet对象的使用及相关方法详解,包括DataSet对象的概述、与数据关系对象的互联、Rows集合和Columns集合的组成,以及DataSet对象常用的方法之一——Merge方法的使用。通过本文的阅读,读者可以了解到DataSet对象在C#中的重要性和使用方法。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • 本文详细介绍了Linux中进程控制块PCBtask_struct结构体的结构和作用,包括进程状态、进程号、待处理信号、进程地址空间、调度标志、锁深度、基本时间片、调度策略以及内存管理信息等方面的内容。阅读本文可以更加深入地了解Linux进程管理的原理和机制。 ... [详细]
  • 1,关于死锁的理解死锁,我们可以简单的理解为是两个线程同时使用同一资源,两个线程又得不到相应的资源而造成永无相互等待的情况。 2,模拟死锁背景介绍:我们创建一个朋友 ... [详细]
  • 《数据结构》学习笔记3——串匹配算法性能评估
    本文主要讨论串匹配算法的性能评估,包括模式匹配、字符种类数量、算法复杂度等内容。通过借助C++中的头文件和库,可以实现对串的匹配操作。其中蛮力算法的复杂度为O(m*n),通过随机取出长度为m的子串作为模式P,在文本T中进行匹配,统计平均复杂度。对于成功和失败的匹配分别进行测试,分析其平均复杂度。详情请参考相关学习资源。 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • 本文介绍了使用PHP实现断点续传乱序合并文件的方法和源码。由于网络原因,文件需要分割成多个部分发送,因此无法按顺序接收。文章中提供了merge2.php的源码,通过使用shuffle函数打乱文件读取顺序,实现了乱序合并文件的功能。同时,还介绍了filesize、glob、unlink、fopen等相关函数的使用。阅读本文可以了解如何使用PHP实现断点续传乱序合并文件的具体步骤。 ... [详细]
  • Voicewo在线语音识别转换jQuery插件的特点和示例
    本文介绍了一款名为Voicewo的在线语音识别转换jQuery插件,该插件具有快速、架构、风格、扩展和兼容等特点,适合在互联网应用中使用。同时还提供了一个快速示例供开发人员参考。 ... [详细]
  • 后台获取视图对应的字符串
    1.帮助类后台获取视图对应的字符串publicclassViewHelper{将View输出为字符串(注:不会执行对应的ac ... [详细]
  • 动态规划算法的基本步骤及最长递增子序列问题详解
    本文详细介绍了动态规划算法的基本步骤,包括划分阶段、选择状态、决策和状态转移方程,并以最长递增子序列问题为例进行了详细解析。动态规划算法的有效性依赖于问题本身所具有的最优子结构性质和子问题重叠性质。通过将子问题的解保存在一个表中,在以后尽可能多地利用这些子问题的解,从而提高算法的效率。 ... [详细]
  • 高质量SQL书写的30条建议
    本文提供了30条关于优化SQL的建议,包括避免使用select *,使用具体字段,以及使用limit 1等。这些建议是基于实际开发经验总结出来的,旨在帮助读者优化SQL查询。 ... [详细]
  • 本文介绍了指针的概念以及在函数调用时使用指针作为参数的情况。指针存放的是变量的地址,通过指针可以修改指针所指的变量的值。然而,如果想要修改指针的指向,就需要使用指针的引用。文章还通过一个简单的示例代码解释了指针的引用的使用方法,并思考了在修改指针的指向后,取指针的输出结果。 ... [详细]
  • 猜字母游戏
    猜字母游戏猜字母游戏——设计数据结构猜字母游戏——设计程序结构猜字母游戏——实现字母生成方法猜字母游戏——实现字母检测方法猜字母游戏——实现主方法1猜字母游戏——设计数据结构1.1 ... [详细]
  • 本文介绍了解决IE678伪类不兼容问题的方法,包括少用CSS3和HTML5独有的属性,使用CSS hacker,使用last-child清除浮动、批量添加标签、去掉list item最后一个的border-right等技巧。同时还介绍了使用after清除浮动时加上IE独有属性zoom:1的处理方法。另外,本文还提到可以使用jQuery代替批量添加标签的功能,以及使用负边距和CSS2选择器element+element去掉list item最后一个的border-right的方法。 ... [详细]
author-avatar
一粒小小无名砂_741
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有