【详细】手把手教你实现JavaScript轮播图功能

news/2024/7/19 15:35:40 标签: 轮播图, js, html
htmledit_views">

        html" title=轮播图>轮播图见下图是移动端,网站上一种非常常见的展示效果,我们在各大app,网站都经常可以看到这种效果。用户可以用手来回选择展示的图片,无点击事件时,控件会依次循环展示图片。

那么本文今天就探究一下如何实现这类效果。

展示效果

技术流程

对于这个功能,作者梳理了一下实现流程
1. 需要有一个图片切换功能

2. 能够自动切换图片

3. 鼠标移动到html" title=轮播图>轮播图上,不进行轮回播放,鼠标移动走进行轮回播放

 

图片切换函数的实现

function changePic (curIndex) {
       for (var i = 0; i < pic.length; ++i) {
             pic[i].style.display = "none";
             list[i].className = "";
           }
           pic[curIndex].style.display = "block";
           list[curIndex].className = "on";
       }
   };

遍历所有数组中所有图片,然后通过遍历展示切换图片,进而实现效果

 

自动切换图片

自动切换图片就需要让他能够在一定时间内自动调用切换函数

function autoPlay () {
                if (++index >= pic.length) index = 0;
                changePic(index);
            }

同时为了实现过一段时间切换下一张,需要进行时间限制

html">timer = setInterval(autoPlay, 5000);

 

鼠标悬停控制

鼠标划过整个容器时停止自动播放

wrap.onmouseover = function () 
{      
    clearInterval(timer);
}
html">鼠标离开整个容器时继续播放至下一张,设置轮播间隔为5000毫秒(5s)
wrap.onmouseout = function () 
{
     timer = setInterval(autoPlay, 5000);
}

html文件引入需要轮播的图片

html"><body>
<div class="wrap" id='wrap'>
    <ul id="pic">
        <li><img src="res/card1.png" alt=""></li>
        <li><img src="res/card2.png" alt=""></li>
        <li><img src="res/card3.png" alt=""></li>
        <li><img src="res/card4.png" alt=""></li>
        <li><img src="res/card5.png" alt=""></li>
    </ul>
    <ol id="list">
        <li class="on">1</li>
        <li>2</li>
        <li>3</li>
        <li>4</li>
        <li>5</li>
    </ol>
</div>

 css决定html" title=轮播图>轮播图的样式

<style>
        *{margin:0;
            padding:0;
            list-style:none;}
        .wrap{height:170px;
            width:490px;
            margin:60px auto;
            overflow: hidden;
            position: relative;
            margin:100px auto;}
        .wrap ul{position:absolute;}
        .wrap ul li{height:170px;}
        .wrap ol{position:absolute;
            right:5px;
            bottom:10px;}
        .wrap ol li{height:20px; width: 20px;
            background:#ccc;
            border:solid 1px #666;
            margin-left:5px;
            color:#000;
            float:left;
            line-height:1;
            text-align:center;
            cursor:pointer;}
        .wrap ol .on{background:#E97305;
            color:#fff;}
 </style>

 完整代码与演示

html"><!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>html" title=轮播图>轮播图demo</title>
    <style>
        *{margin:0;
            padding:0;
            list-style:none;}
        .wrap{height:170px;
            width:490px;
            margin:60px auto;
            overflow: hidden;
            position: relative;
            margin:100px auto;}
        .wrap ul{position:absolute;}
        .wrap ul li{height:170px;}
        .wrap ol{position:absolute;
            right:5px;
            bottom:10px;}
        .wrap ol li{height:20px; width: 20px;
            background:#ccc;
            border:solid 1px #666;
            margin-left:5px;
            color:#000;
            float:left;
            line-height:1;
            text-align:center;
            cursor:pointer;}
        .wrap ol .on{background:#E97305;
            color:#fff;}

    </style>
    <script type="text/javascript">
        window.onload=function(){
            var wrap=document.getElementById('wrap'),
                pic=document.getElementById('pic').getElementsByTagName("li"),
                list=document.getElementById('list').getElementsByTagName('li'),
                index=0,
                timer=null;

            // 定义并调用自动播放函数
            timer = setInterval(autoPlay, 5000);

            // 鼠标划过整个容器时停止自动播放
            wrap.onmouseover = function () {
                clearInterval(timer);
            }

            // 鼠标离开整个容器时继续播放至下一张
            wrap.onmouseout = function () {
                timer = setInterval(autoPlay, 5000);
            }
            // 遍历所有数字导航实现划过切换至对应的图片
            for (var i = 0; i < list.length; i++) {
                list[i].onmouseover = function () {
                    clearInterval(timer);
                    index = this.innerText - 1;
                    changePic(index);
                };
            };

            function autoPlay () {
                if (++index >= pic.length) index = 0;
                changePic(index);
            }

            // 定义图片切换函数
            function changePic (curIndex) {
                for (var i = 0; i < pic.length; ++i) {
                    pic[i].style.display = "none";
                    list[i].className = "";
                }
                pic[curIndex].style.display = "block";
                list[curIndex].className = "on";
            }

        };

    </script>
</head>
<body>
<div class="wrap" id='wrap'>
    <ul id="pic">
        <li><img src="res/card1.png" alt=""></li>
        <li><img src="res/card2.png" alt=""></li>
        <li><img src="res/card3.png" alt=""></li>
        <li><img src="res/card4.png" alt=""></li>
        <li><img src="res/card5.png" alt=""></li>
    </ul>
    <ol id="list">
        <li class="on">1</li>
        <li>2</li>
        <li>3</li>
        <li>4</li>
        <li>5</li>
    </ol>
</div>
</body>
</html>

喜欢的朋友点赞收藏啦

 


http://www.niftyadmin.cn/n/855453.html

相关文章

【详细】三种主流跨平台技术探讨

前言 每当我们评估新技术时要问的第一个问题就是“它会给我们的业务和客户带来哪些价值&#xff1f;”&#xff0c;工程师们很容易对闪闪发光的新事物着迷&#xff0c;却经常会忽略这些新事物其实可能对我们的客户没有任何好处&#xff0c;反而只会让现有的工作流程更加复杂。 …

基于Python和Opencv的目标检测与特征

1.目标检测 (1)什么是目标检测&#xff1f; 判断一副图像或者视频的一帧存不存在目标物体&#xff0c;例如检测一幅图片中有没有花&#xff0c;有 没有人脸&#xff0c;或者检测一段视频中行驶过的车辆、行人等 检测完成后&#xff0c;也可以继续往深做目标识别&#xff0c;例如…

【详细】TeamViewer安装使用教程

今天给大家分享一款可以远程办公&#xff0c;协同办公的软件--TeamViewer&#xff0c;第一次知道它是看到师兄出去玩用手机改BUG&#xff01;被深深地震惊到了,,,渣渣感觉也体验一下这款好的工具。 下载地址&#xff1a;https://www.teamviewer.com/en/ 个人版免费 安装教程 安…

ping tracert netstat 命令解释

PING命令参数详解 -a 将目标的机器标识转换为ip地址 -t 若使用者不人为中断会不断的ping下去 -c count 要求ping命令连续发送数据包&#xff0c;直到发出并接收到count个请求 -d 为使用的套接字打开调试状态 -f 是一种快速方式ping。使得ping输出数据包的速度和数据包从远程主机…

全网最详细的微信小程序开发教程

前几天学习了react这个开发框架&#xff0c;了解了JS语言的基本知识。想到最近3年小程序如火如荼的发展&#xff0c;进而决定入坑学习。 微信小程序简介 微信小程序是以微信为运行环境的一种应用&#xff0c;其实质是 Hybrid 技术的应用&#xff0c;Hybrid App 即混合模式移动…

解读设计模式----策略模式(Strategy Pattern)

一、模式概述 策略模式(Strategy Pattern)在外形上与状态模式很相似&#xff0c;但在意图上有些不同。其意图是使这些算法可以相互替换&#xff0c;并提供一种方法来选择最合适的算法。在我应用OOP的设计过程演化&#xff08;三&#xff09;这篇文章里应用到了策略模式&am…

ASP.NET验证控件

一、前言 在Web应用程序中&#xff0c;大都是对数据的提供或收集&#xff0c;对于数据的有效性验证是非常重要的&#xff0c;验证就是给所收集的数据应用的一系列规则。 为进行有效性验证而收集的数据来自于在应用程序中提供的Web窗体&#xff0c;Web窗体由不同…

《壮志在我心》

[hjp3]hjptypesong&player1&sonhttp://www.godblog.cn/user1/2869/upload/2007916831.mp3&autoplayno&captionfalse&lrc&autoreplay1&bgcolorFFFFFF&width200&height20[/hjp3] 转载于:https://www.cnblogs.com/JemBai/archive/2008/07/23/…