Javascript实现图片轮播效果。

news/2024/7/19 14:32:37 标签: javascritp, H5, c3, js

js做一个简单的图片轮播效果。
思路如下:用JavaScript来控制轮播的图片的样式(margin-left)。用计时器来控制图片的自动播放。鼠标点击控制图片的翻页。
效果图如下。具有以下功能:1.自动图片轮播 2.左右切换图片 3.底下小按钮切换图片。在这里插入图片描述
话不多说,代码如下。
首先肯定是HTML代码`这个没什么说的,先插入需要轮播的图片。底下切换图片的按钮,还有左右按键。

<div class="block">
    <div class="box_big ">
        <div class="box"><img src="img/7.jpg" alt=""/></div>
        <div class="box"><img src="img/5.jpg" alt=""/></div>
        <div class="box"><img src="img/6.jpg" alt=""/></div>
        <div class="box"><img src="img/8.jpg" alt=""/></div>
        <div class="box"><img src="img/9.jpg" alt=""/></div>
        <div class="box"><img src="img/7.jpg" alt=""/></div>
    </div>
    <div class="spot">
        <div class="spot_list">1</div>
        <div class="spot_list">2</div>
        <div class="spot_list">3</div>
        <div class="spot_list">4</div>
        <div class="spot_list">5</div>
    </div>
    <div class="btn">
        <div class="left_btn"><span> < </span></div>
        <div class="right_btn"><span> > </span></div>
    </div>
</div>

然后是给代码添加样式。添加好后基本样式就已经完成了。让图片横排列。超出父容器部分隐藏。记得给图片的margin-left添加过渡效果(如图)接下来就是让他动起来!!!在这里插入图片描述

  <style>
        * {
            margin: 0;
            padding: 0;
        }

        .block {
            width: 700px;
            height: 400px;
            border: 1px solid black;
            margin: 100px auto;
            position: relative;
            overflow: hidden;
            box-sizing: border-box;
        }

        .box_big {
            position: absolute;
            width: 4200px;
            height: 400px;

        }

        .nav {
            transition: margin-left 0.5s linear;
        }

        .box {
            width: 700px;
            height: 400px;
            float: left;
        }

        img {
            width: 100%;
            height: 100%;
        }

        .spot {
            position: absolute;
            width: 100%;
            text-align: right;
            bottom: 5px;
        }

        .spot_list {

            width: 30px;
            height: 30px;
            font-size: 20px;
            text-align: center;
            border: 1px solid gray;
            border-radius: 5px;
            line-height: 30px;
            font-weight: bolder;
            margin: 10px;
            display: inline-block;
            background: rgba(255, 255, 255, 0.3);
        }

        .btn {
            width: 100%;
            height: 80px;
            position: absolute;
            margin: 0 auto;
            top: 160px;
        }

        .left_btn, .right_btn {
            width: 30px;
            height: 80px;
            z-index: 15;
            background: rgba(255, 255, 255, 0.3);
            text-align: center;
            line-height: 80px;
            border-radius: 8px;

        }

        .left_btn {
            float: left;
        }

        .right_btn {
            float: right;
        }
    </style>

这块是最重要的的部分,js代码
思路如下,先获取元素,让图片切换下一张,这里我使用count++*元素的marginLeft值。count加一次图片就会切换下一张。然后把这段代码放在计时器中,图片就可以自动切换下一张了。切换的时候底下的小图标的背景色也跟着切换。

ipt>
    var box = document.getElementsByClassName("box_big")[0];
    var spot = document.getElementsByClassName("spot_list");
    var block = document.getElementsByClassName("block")[0];
    var left_btn = document.getElementsByClassName("left_btn")[0];
    var right_btn = document.getElementsByClassName("right_btn")[0];
    var time = null;
    var count = 0;
    
 //计时器
    function showtime() {
        time = setInterval(function () {
            mate();
        }, 1000);
    }
    //正常滚动
    function mate() {
        box.className = "box_big nav";
        spot[count].style.background = "rgba(255, 255, 255, 0.3)";
        count++;
        spot[count > box.children.length - 2 ? 0 : count].style.background = "rgba(91,91,91,0.8)";
        box.style.marginLeft = (count * -700) + "px";
        //添加一次性计时器
        setTimeout(function () {
        //判断count的值。如果大于图片长度,就重0开始+
            if (count > box.children.length - 2) {
                count = 0;
                box.className = "box_big";
                box.style.marginLeft = "0px"
            }
        }, 500)
    }

然后设置鼠标进入计时器关闭,鼠标离开计时器打开。

 block.onmouseenter = function () {
        clearInterval(time);
    };
    //鼠标出来计时器打开
    block.onmouseleave = function () {
        showtime();
    };

接下来,来做底下鼠标放在地下图标的时候切换对应的图片。这里我用的是for循环。

 for (var i = 0; i < spot.length; i++) {
        spot[i].index = i;
        spot[i].onmouseenter = function () {
            spot[count].style.background = "rgba(255, 255, 255, 0.3)";
            this.style.background = "rgba(91,91,91,0.8)";
            count = this.index;
            box.style.marginLeft = (count * -700) + "px";
        }

    }

上面大部分功能已经完成,现在剩下最后一步,左右键切换图片的上一张和下一张。用鼠标点击事件。左边很简单,直接执行开始时候的方法即可(切换方向和自动切换方向相同)。右边用count–

 //图片左边划
    left_btn.onclick = function () {
        mate();
    };
        //图片右边划
    right_btn.onclick = function () {
        spot[count].style.backgroundColor = "rgba(255,255,255,0.3)";
        count--;
        if (count < 0) {
            box.className = "box_big";
            count = box.children.length - 2;
            box.style.marginLeft = "-3500px";
        }
        //添加一次性计时器
        setTimeout(function () {
            box.className = "box_big nav";
            box.style.marginLeft = (-700 * count) + "px";
            spot[count].style.backgroundColor = "rgba(91,91,91,0.8)";
        }, 1);
    };

到这里就已经写完了。

完整js代码如下:

<script>
//首先获取元素,这里获取了所有需要的元素。
    var box = document.getElementsByClassName("box_big")[0];
    var spot = document.getElementsByClassName("spot_list");
    var block = document.getElementsByClassName("block")[0];
    var left_btn = document.getElementsByClassName("left_btn")[0];
    var right_btn = document.getElementsByClassName("right_btn")[0];
    var time = null;
    spot[0].style.background = "rgba(91,91,91,0.8)";
    var count = 0;
    showtime();
    //鼠标进入计时器停止
    block.onmouseenter = function () {
        clearInterval(time);
    };
    //鼠标出来计时器打开
    block.onmouseleave = function () {
        showtime();
    };
    //点的事件
    for (var i = 0; i < spot.length; i++) {
        spot[i].index = i;
        spot[i].onmouseenter = function () {
            spot[count].style.background = "rgba(255, 255, 255, 0.3)";
            this.style.background = "rgba(91,91,91,0.8)";
            count = this.index;
            box.style.marginLeft = (count * -700) + "px";
        }

    }
    //图片左边划
    left_btn.onclick = function () {
        mate();
    };
    //图片右边划
    right_btn.onclick = function () {
        spot[count].style.backgroundColor = "rgba(255,255,255,0.3)";
        count--;
        if (count < 0) {
            box.className = "box_big";
            count = box.children.length - 2;
            box.style.marginLeft = "-3500px";
        }
        setTimeout(function () {
            box.className = "box_big nav";
            box.style.marginLeft = (-700 * count) + "px";
            spot[count].style.backgroundColor = "rgba(91,91,91,0.8)";
        }, 1);
    };
    //计时器
    function showtime() {
        time = setInterval(function () {
            mate();
        }, 1000);
    }
    //正常滚动
    function mate() {
        box.className = "box_big nav";
        spot[count].style.background = "rgba(255, 255, 255, 0.3)";
        count++;
        spot[count > box.children.length - 2 ? 0 : count].style.background = "rgba(91,91,91,0.8)";
        box.style.marginLeft = (count * -700) + "px";
        setTimeout(function () {
            if (count > box.children.length - 2) {
                count = 0;
                box.className = "box_big";
                box.style.marginLeft = "0px"
            }
        }, 500)
    }
</script>
</body>

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

相关文章

IDEA中MyBatiX插件的使用教程

MybatisX插件功能介绍 主要功能如下&#xff1a; 生成mapper xml文件 快速从代码跳转到mapper及从mapper返回代码 mybatis自动补全及语法错误提示 集成mybatis generator gui界面 根据数据库注解&#xff0c;生成swagger model注解 首先下载MybatisX插件 配置数据源 在idea中连…

【java】BlockingQueue解析【硬】

什么是BlockingQueue BlockingQueue即阻塞队列&#xff0c;它算是一种将ReentrantLock用得非常精彩的一种方式&#xff0c;很好的解决了多线程中&#xff0c;如何高效安全“传输”数据的问题。依据它的基本原理&#xff0c;我们可以实现Web中的长连接聊天功能&#xff0c;当然其…

【推荐】Windows安装的几种工具

这里介绍的几种方式是比较干净的安装方式&#xff0c;网上很多傻瓜式的软件可以一键装机&#xff0c;但是会携带一些默认安装的软件&#xff0c;这里介绍的几款都是轻量级只安装系统的软件。 1 软碟通(UltraISO) 这个是比较经典的装机软件&#xff0c;可以自行搜索其使用技巧…

浅谈JavaScript设计模式.

什么是设计模式&#xff1f; 百度百科: 设计模式&#xff08;Design Pattern&#xff09;是一套被反复使用、多数人知晓的、经过分类的、代码设计经验的总结。 使用设计模式的目的&#xff1a;为了代码可重用性、让代码更容易被他人理解、保证代码可靠性。 设计模式使代码编写…

win10可用空间新建卷提示磁盘上没有足够的空间完成此操作如何解决

解决方式&#xff1a; 1.Windows键R&#xff0c;打开运行对话框&#xff0c;键入&#xff1a;diskpart 进入duWindows 自带的磁盘管理程序进入DOS窗口&#xff1a;DISKPART> 2.键入&#xff1a; list disk 选择查看该电脑下已连接的硬盘&#xff0c;dao会显示如下&#x…

hdu - 4974 - A simple water problem(贪心 + 反证)

题意&#xff1a;N个队(N < 100000)&#xff0c;每一个队有个总分ai(ai < 1000000)&#xff0c;每场比赛比赛两方最多各可获得1分&#xff0c;问最少经过了多少场比赛。题目链接&#xff1a;http://acm.hdu.edu.cn/showproblem.php?pid4974 ——>>我们应该尽量使每…

torch中nn.ReLU和F.ReLU的不同

可以看下面的代码 其实TestNet_1和TestNet_2执行的结果是一样的&#xff0c;但是明显 Relu的位置是不一样的&#xff0c;一个是nn库内的&#xff0c;一个是nn.functional内的 import torch.nn as nn import torch.nn.functional as F import torch.nn as nnclass TestNet_1(n…

MfcStrFile

/* ******* MfcStrFile.h ********** ********* 字符串、文件、目录操作函数声明 ********** *//* author: autumoon */#pragma once#ifdef _X86_ #pragma comment(linker,"/manifestdependency:\"typewin32 nameMicrosoft.Windows.Common-Controls version6.0.0.0 p…