【前端】手写轮播图·教程2

效果图:

在这里插入图片描述
在这里插入图片描述

需求分析:

  1. 根据图片数量生成对应的小圆点
  2. 克隆第一个li,放到ul的最后面
  3. 最开始显示第一个li,点亮第一个小圆点
  4. 点击left按钮,看上一张,点亮小圆点
  5. 点击right按钮,看下一张,点亮小圆点
  6. 点击小圆点,看相应图片,点亮小圆点
  7. 可以自动轮播
  8. 鼠标移入,停止轮播
  9. 鼠标移出,开始轮播

源代码:

  • HTML部分
<div class="all" id='all'>
    <div class="screen" id="screen">
        <ul id="ul">
            <li><img src="images/1.jpg" width="500" height="200" /></li>
            <li><img src="images/2.jpg" width="500" height="200" /></li>
            <li><img src="images/3.jpg" width="500" height="200" /></li>
            <li><img src="images/4.jpg" width="500" height="200" /></li>
            <li><img src="images/5.jpg" width="500" height="200" /></li>
        </ul>
        <ol>
        </ol>
        <div id="arr">
            <span id="left"><</span>
            <span id="right">></span>
        </div>
    </div>
</div>
  • css部分
<style>
 * {
    padding: 0;
    margin: 0;
    list-style: none;
    border: 0;
}
.all {
    width: 500px;
    height: 200px;
    padding: 7px;
    border: 1px solid #ccc;
    margin: 100px auto;
    position: relative;
}
.screen {
    width: 500px;
    height: 200px;
    overflow: hidden;
    position: relative;
}
.screen li {
    width: 500px;
    height: 200px;
    overflow: hidden;
    float: left;
}
.screen ul {
    position: absolute;
    left: 0;
    top: 0px;
    width: 3000px;
}
.all ol {
    position: absolute;
    right: 10px;
    bottom: 10px;
    line-height: 20px;
    text-align: center;
}
.all ol li {
    float: left;
    width: 20px;
    height: 20px;
    background: #fff;
    border: 1px solid #ccc;
    margin-left: 10px;
    cursor: pointer;
}
.all ol li.current {
    background: yellow;
}
/*#arr {display: none;}*/
#arr span {
    width: 40px;
    height: 40px;
    position: absolute;
    left: 5px;
    top: 50%;
    margin-top: -20px;
    background: #000;
    cursor: pointer;
    line-height: 40px;
    text-align: center;
    font-weight: bold;
    font-family: '黑体';
    font-size: 30px;
    color: #fff;
    opacity: 0.3;
    border: 1px solid #fff;
}
#arr #right {
    right: 5px;
    left: auto;
}   
</style>
  • JavaScript部分
  1. 为了简化代码,先封装一些常用的自定义构造函数
<script>
// 返回指定dom节点的attr样式值
function getStyle(dom,attr){
	if(window.getComputedStyle){// 是否有getComputedStyle方法
		//非IE低版
		return window.getComputedStyle(dom,null)[attr];
	}else{
		//IE低版
		return dom.currentStyle[attr];
	}
}
// 封装一个缓动的动画函数,需要时调用即可
function animate(dom,json,callback){
	/* 
        dom:要运动的节点
        json:要运动的样式名
        callback:运动完成的回调函数
    */
    clearInterval(dom.timer);//要用定时器,先清定时器
    //要运动的定时器
    dom.timer = setInterval(function(){
		var flag = true;
		//json有几个属性,就要运动几次
		for(var attr in json){
			//获取当前位置
			var current = parseInt(getStyle(dom,attr));
			// 计算速度
			var speed = json[attr] - current > 0 ? Math.ceil((json[attr]-current)/10):Math.floor((json[attr]-current)/10);
			//计算下一个位置
			var next = current + speed;
			//定位元素
			dom.style[attr] = next+'px';
			//判断是否达到目标
			if(next != json[attr]){
				flag = false;
			}
		}
		if(flag){
			clearInterval(dom.timer);
		}
	},20)// 每20毫秒
}
// 封装light,点亮第num个小圆点
function light() {
    // 干掉所有人
    for (var i = 0; i < pointArr.length; i++) {
        pointArr[i].className = "";
    }
    // 留下我一个
    pointArr[point].className = "current";
}
// 下一张
function autoplay() {
    // li的索引要增加1
    // point的索引要增加1
    num++;
    point++;
    // li的索引的临界点
    if (num > liArr.length - 1) {
        num = 1;
        ul.style.left = 0;
    }
    // point的索引的临界点
    point = point > pointArr.length - 1 ? 0 : point
    // ul进行移动
    animate(ul, { left: -num * width })
    // 点亮小圆点
    light()
}
</script>
  1. js主体部分,需要用到封装的函数,调用即可
<script>
var box = document.getElementById('screen');//总容器
var ul = document.getElementById('ul');//图片li的容器
var ol = box.children[1];//小圆点容器
var prev = box.children[2].children[0];//上一张按钮
var next = box.children[2].children[1];//下一张按钮
var num = 0;//num记录当前li的索引
var point = 0;//point记录小圆点的索引
var width = ul.children[0].offsetWidth;//记录一个li的宽度
// 1 根据图片数量生成对应的下圆点
for(var i=0; i<ul.children.length; i++){
	var newPoint = document.createElement('li');
	newPoint.index = i;
	newPoint.innerHTML = i + 1;
	ol.appendChild(newPoint);
}
// 2 克隆第一个li,放到ul的最后面
var newLi = ul.children[0].cloneNode(true);
ul.appendChild(newLi);

// 3 点亮第一个小圆点
var pointArr = ol.children;//所有小圆点的集合
var liArr = ul.children;//所有的li的集合
light();

// 4 点击left按钮,看上一张,点亮小圆点
prev.onclick = function(){
	num--;
	point--;
	// li的索引的临界点
	if(num < 0){
		ul.style.left = -(liArr.length-1)*width+'px';
		num = liArr.length - 2;
	}
	// point的索引的临界点
	point = point<0?pointArr.length-1:point;
	// ul进行移动
	animate(ul,{left:-num*width});
	// 点亮小圆点
    light();
}
// 5 点击right按钮,看下一张,点亮小圆点
next.onclick = autoplay;
// 6 点击小圆点,看相应图片,点亮小圆点
for(var i=0; i<pointArr.length; i++){
	pointArr[i].onclick = function(){
		//可以获取当前被点击的小圆点的索引
		var next = this.index;
		animate(ul, { left: -next * width });
		// 更新当前索引,并点亮小圆点
		num = next;
		point = next;
		light();
	}
}
// 7 可以自动轮播
var timer = setInterval(autoplay,3000);
// 8 鼠标移入,停止轮播
box.onmouseenter = function(){
	clearInterval(timer);
}
// 9 鼠标移出,开始轮播 
box.onmouseleave = function(){
	clearInterval(timer);
	timer = setInterval(autoplay, 3000);
}
</script>
  • 总代码
<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title>
    <style type="text/css">
        * {
            padding: 0;
            margin: 0;
            list-style: none;
            border: 0;
        }

        .all {
            width: 500px;
            height: 200px;
            padding: 7px;
            border: 1px solid #ccc;
            margin: 100px auto;
            position: relative;
        }

        .screen {
            width: 500px;
            height: 200px;
            overflow: hidden;
            position: relative;
        }

        .screen li {
            width: 500px;
            height: 200px;
            overflow: hidden;
            float: left;
        }

        .screen ul {
            position: absolute;
            left: 0;
            top: 0px;
            width: 3000px;
        }

        .all ol {
            position: absolute;
            right: 10px;
            bottom: 10px;
            line-height: 20px;
            text-align: center;
        }

        .all ol li {
            float: left;
            width: 20px;
            height: 20px;
            background: #fff;
            border: 1px solid #ccc;
            margin-left: 10px;
            cursor: pointer;
        }

        .all ol li.current {
            background: yellow;
        }

        /*#arr {display: none;}*/
        #arr span {
            width: 40px;
            height: 40px;
            position: absolute;
            left: 5px;
            top: 50%;
            margin-top: -20px;
            background: #000;
            cursor: pointer;
            line-height: 40px;
            text-align: center;
            font-weight: bold;
            font-family: '黑体';
            font-size: 30px;
            color: #fff;
            opacity: 0.3;
            border: 1px solid #fff;
        }

        #arr #right {
            right: 5px;
            left: auto;
        }
    </style>

    <style>

    </style>
</head>

<body>
    <script></script>
    <div class="all" id='all'>
        <div class="screen" id="screen">
            <ul id="ul">
                <li><img src="images/1.jpg" width="500" height="200" /></li>
                <li><img src="images/2.jpg" width="500" height="200" /></li>
                <li><img src="images/3.jpg" width="500" height="200" /></li>
                <li><img src="images/4.jpg" width="500" height="200" /></li>
                <li><img src="images/5.jpg" width="500" height="200" /></li>
            </ul>
            <ol>

            </ol>
            <div id="arr">
                <span id="left"><</span>
                <span id="right">></span>
            </div>
        </div>
    </div>
    <script>
        // 返回指定dom节点的attr样式值
        function getStyle(dom, attr) {
            if (window.getComputedStyle) {// 是否有getComputedStyle方法
                //非IE低版
                return window.getComputedStyle(dom, null)[attr];
            } else {
                //IE低版
                return dom.currentStyle[attr];
            }
        }
        // 封装一个缓动的动画函数,需要时调用即可
        function animate(dom, json, callback) {
            /* 
                dom:要运动的节点
                json:要运动的样式名
                callback:运动完成的回调函数
            */
            clearInterval(dom.timer);//要用定时器,先清定时器
            //要运动的定时器
            dom.timer = setInterval(function () {
                var flag = true;
                //json有几个属性,就要运动几次
                for (var attr in json) {
                    //获取当前位置
                    var current = parseInt(getStyle(dom, attr));
                    // 计算速度
                    var speed = json[attr] - current > 0 ? Math.ceil((json[attr] - current) / 10) : Math.floor((json[attr] - current) / 10);
                    //计算下一个位置
                    var next = current + speed;
                    //定位元素
                    dom.style[attr] = next + 'px';
                    //判断是否达到目标
                    if (next != json[attr]) {
                        flag = false;
                    }
                }
                if (flag) {
                    clearInterval(dom.timer);
                }
            }, 20)// 每20毫秒
        }
        // 封装light,点亮第num个小圆点
        function light() {
            // 干掉所有人
            for (var i = 0; i < pointArr.length; i++) {
                pointArr[i].className = "";
            }
            // 留下我一个
            pointArr[point].className = "current";
        }
        // 下一张
        function autoplay() {
            // li的索引要增加1
            // point的索引要增加1
            num++;
            point++;
            // li的索引的临界点
            if (num > liArr.length - 1) {
                num = 1;
                ul.style.left = 0;
            }
            // point的索引的临界点
            point = point > pointArr.length - 1 ? 0 : point
            // ul进行移动
            animate(ul, { left: -num * width })
            // 点亮小圆点
            light()
        }

        var box = document.getElementById('screen');//总容器
        var ul = document.getElementById('ul');//图片li的容器
        var ol = box.children[1];//小圆点容器
        var prev = box.children[2].children[0];//上一张按钮
        var next = box.children[2].children[1];//下一张按钮
        var num = 0;//num记录当前li的索引
        var point = 0;//point记录小圆点的索引
        var width = ul.children[0].offsetWidth;//记录一个li的宽度
        // 1 根据图片数量生成对应的下圆点
        for (var i = 0; i < ul.children.length; i++) {
            var newPoint = document.createElement('li');
            newPoint.index = i;
            newPoint.innerHTML = i + 1;
            ol.appendChild(newPoint);
        }
        // 2 克隆第一个li,放到ul的最后面
        var newLi = ul.children[0].cloneNode(true);
        ul.appendChild(newLi);

        // 3 点亮第一个小圆点
        var pointArr = ol.children;//所有小圆点的集合
        var liArr = ul.children;//所有的li的集合
        light();

        // 4 点击left按钮,看上一张,点亮小圆点
        prev.onclick = function () {
            num--;
            point--;
            // li的索引的临界点
            if (num < 0) {
                ul.style.left = -(liArr.length - 1) * width + 'px';
                num = liArr.length - 2;
            }
            // point的索引的临界点
            point = point < 0 ? pointArr.length - 1 : point;
            // ul进行移动
            animate(ul, { left: -num * width });
            // 点亮小圆点
            light();
        }
        // 5 点击right按钮,看下一张,点亮小圆点
        next.onclick = autoplay;
        // 6 点击小圆点,看相应图片,点亮小圆点
        for (var i = 0; i < pointArr.length; i++) {
            pointArr[i].onclick = function () {
                //可以获取当前被点击的小圆点的索引
                var next = this.index;
                animate(ul, { left: -next * width });
                // 更新当前索引,并点亮小圆点
                num = next;
                point = next;
                light();
            }
        }
        // 7 可以自动轮播
        var timer = setInterval(autoplay, 3000);
        // 8 鼠标移入,停止轮播
        box.onmouseenter = function () {
            clearInterval(timer);
        }
        // 9 鼠标移出,开始轮播 
        box.onmouseleave = function () {
            clearInterval(timer);
            timer = setInterval(autoplay, 3000);
        }
    </script>
</body>

</html>

所用图片:

在这里插入图片描述

由于你们没有图片,直接复制代码,展示不出效果,大家可以自己找几张图片来代替,稍微修改下即可,只要能看得懂代码,修改起来莫得问题啦

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
提供的源码资源涵盖了Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!
好的,以下是一个简单的前端手写轮播图实现思路: 1. HTML 结构中需要一个容器元素,用来包含轮播图。 2. 在该容器元素中,添加多个轮播项,每个轮播项包含一张图片和一段描述文字。 3. 通过 CSS 设置轮播项的样式,包括宽度、高度、位置等。 4. 在 JavaScript 中获取容器元素和轮播项元素,以及左右切换按钮元素。 5. 通过设置 CSS 样式,将当前显示的轮播项置于最前面。 6. 给左右切换按钮元素添加事件监听器,当点击时,通过 JavaScript 移动轮播项的位置,完成切换。 7. 使用定时器 setInterval() 实现自动轮播,每隔一定时间切换到下一个轮播项。 以下是一个简单的前端手写轮播图代码示例: ```html <div class="carousel-container"> <div class="carousel-item active"> <img src="image1.jpg" alt="image1"> <div class="carousel-caption"> <h3>Image 1</h3> <p>Description 1</p> </div> </div> <div class="carousel-item"> <img src="image2.jpg" alt="image2"> <div class="carousel-caption"> <h3>Image 2</h3> <p>Description 2</p> </div> </div> <div class="carousel-item"> <img src="image3.jpg" alt="image3"> <div class="carousel-caption"> <h3>Image 3</h3> <p>Description 3</p> </div> </div> <div class="carousel-item"> <img src="image4.jpg" alt="image4"> <div class="carousel-caption"> <h3>Image 4</h3> <p>Description 4</p> </div> </div> <div class="carousel-item"> <img src="image5.jpg" alt="image5"> <div class="carousel-caption"> <h3>Image 5</h3> <p>Description 5</p> </div> </div> <div class="carousel-prev"></div> <div class="carousel-next"></div> </div> ``` ```css .carousel-container { position: relative; width: 800px; height: 500px; overflow: hidden; } .carousel-item { position: absolute; top: 0; left: 0; width: 100%; height: 100%; opacity: 0; transition: all 1s ease-in-out; } .carousel-item.active { opacity: 1; } .carousel-item img { width: 100%; height: 100%; object-fit: cover; } .carousel-caption { position: absolute; bottom: 0; left: 0; width: 100%; padding: 20px; background-color: rgba(0, 0, 0, 0.5); color: #fff; font-size: 24px; line-height: 1.5; } .carousel-prev, .carousel-next { position: absolute; top: 50%; transform: translateY(-50%); width: 50px; height: 50px; background-color: rgba(0, 0, 0, 0.5); cursor: pointer; } .carousel-prev { left: 0; } .carousel-next { right: 0; } ``` ```javascript const carouselContainer = document.querySelector('.carousel-container'); const carouselItems = document.querySelectorAll('.carousel-item'); const prevBtn = document.querySelector('.carousel-prev'); const nextBtn = document.querySelector('.carousel-next'); let currentIndex = 0; function showItem(index) { carouselItems.forEach(item => { item.classList.remove('active'); }); carouselItems[index].classList.add('active'); } prevBtn.addEventListener('click', () => { currentIndex--; if (currentIndex < 0) { currentIndex = carouselItems.length - 1; } showItem(currentIndex); }); nextBtn.addEventListener('click', () => { currentIndex++; if (currentIndex > carouselItems.length - 1) { currentIndex = 0; } showItem(currentIndex); }); setInterval(() => { currentIndex++; if (currentIndex > carouselItems.length - 1) { currentIndex = 0; } showItem(currentIndex); }, 5000); ``` 这是一个简单的轮播图实现,具体的实现方式可以根据实际需求进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一颗不甘坠落的流星

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值