JS实战——轮播图

2024-06-04 9504阅读

目录

一、轮播图介绍

 二、原理

三、轮播图基本htm布局

四、轮播图CSS布局

五、轮播图JS布局

六、轮播图效果


一、轮播图介绍

       现在我们在很多网站上都能看到轮播图,像某东、某宝、某猫等等大小型网站上都有应用。下面就是某宝上的轮播图样式。

JS实战——轮播图 第1张

 二、原理

  将一些图片在一行中平铺,然后计算偏移量再利用定时器实现定时轮播。

三、轮播图基本htm布局

       主要就是创建一个大盒子里面存放轮播图的图片标签,cicle标签下的div主要是为后期存放实现图片轮播下面的小圆点。

     
		

四、轮播图CSS布局

        定义全局,消除页面对我们创建CSS布局时的影响。布局全局样式,消除li标签自带小黑点使用定位消除高度塌陷。

      * {
			margin: 0px;
			padding: 0px;
		}
		.banner {
			width: 600px;
			margin: auto;
			border: 10px solid greenyellow;
			height: 350px;
			position: relative;
			overflow: hidden;
		}
		.imgList {
			list-style: none;
			/* width: 2480px; */
			position: absolute;
			/* left:-620px; */
		}

           设置图片大小,让图片在我们之前设置的边框里。

        .imgList img {
			width: 600px;
			height: 350px;
		}
		.imgList li {
			float: left;
			margin-right: 20px;
		}
		

        定义a标签,生成点击小圆点,产生点击前后效果对比样式。

		.circle {
			position: absolute;
			bottom: 15px;
			left: 50%;
			transform: translateX(-50%);
		}
        .circle a {
			width: 15px;
			height: 15px;
			background: yellow;
			display: block;
			border-radius: 50%;
			opacity: .5;
			float: left;
			margin-right: 5px;
			cursor: pointer;
		}
		.circle a.hover {
			background: black;
			opacity: .8;
		}

五、轮播图JS布局

         首先先获取元素,给父类设置整个图片的宽度,并利用设置好的CSS样式创建底部小圆点。

window.onload = function () {
			var imgList = document.querySelector('.imgList');
			var circle = document.querySelector('.circle');
			var thisIndex = 0;
			var imgListLi = imgList.children;
			var circleA = circle.children;
			var flag = true;
			imgList.style.width = imgList.children.length * 620 + 'px';
			for (var i = 0; i  

         设置监听器为点击按钮实现图片的移动,并用获取元素的方法解决点击圆点附近区域跳转的bug事件。

circle.addEventListener('click', function (e) {
				if (flag) {
					flag = false;
					// console.log(e.target);
					if (e.target.nodeName != 'A') {
						return false;
					}
					thisIndex = e.target.getAttribute('index');
					// imgList.style.left = -thisIndex * 620 + 'px';
					slow(imgList, -thisIndex * 620, function () {
						flag = true;
					});
					circleChange();
				}
			})

         利用函数和节流阀解决图片在跳转到最后一张图片无法返回第一张图片的问题,节流阀用来解决图片移动出现左右抖动横跳现象,并设置了图片自动播放功能。

function antoChange() {
				setInterval(function () {
					if (flag) {
						flag = false;
						if (thisIndex >= circleA.length) {
							thisIndex = 0;
						}
						slow(imgList, -thisIndex * 620, function () {
							flag = true;
						});
						circleChange();
						thisIndex++;
					}
				}, 3000);
			}

        剩下主要用来解决图片左右移动速度和图片移动同时图片下方小圆点同步移动,以及移动过程中小圆点样式的变化。

function circleChange() {
				for (var i = 0; i  0 ? num = Math.ceil(num) : num = Math.floor(num);
					if (offsetLeft == target) {
						clearInterval(obj.myInter);
						callback && callback();
					} else {
						obj.style.left = offsetLeft + num + 'px';
					}
				}, 10)
			}
			antoChange();
		}

六、轮播图效果

JS实战——轮播图 第2张

七、完整代码



	
	轮播图
	
	
		* {
			margin: 0px;
			padding: 0px;
		}
		.banner {
			width: 600px;
			margin: auto;
			border: 10px solid greenyellow;
			height: 350px;
			position: relative;
			overflow: hidden;
		}
		.imgList {
			list-style: none;
			/* width: 2480px; */
			position: absolute;
			/* left:-620px; */
		}
		.imgList img {
			width: 600px;
			height: 350px;
		}
		.imgList li {
			float: left;
			margin-right: 20px;
		}
		.circle {
			position: absolute;
			bottom: 15px;
			left: 50%;
			transform: translateX(-50%);
		}
		.circle a {
			width: 15px;
			height: 15px;
			background: yellow;
			display: block;
			border-radius: 50%;
			opacity: .5;
			float: left;
			margin-right: 5px;
			cursor: pointer;
		}
		.circle a.hover {
			background: black;
			opacity: .8;
		}
	


	
		
window.onload = function () { var imgList = document.querySelector('.imgList'); var circle = document.querySelector('.circle'); var thisIndex = 0; var imgListLi = imgList.children; var circleA = circle.children; var flag = true; imgList.style.width = imgList.children.length * 620 + 'px'; for (var i = 0; i = circleA.length) { thisIndex = 0; } slow(imgList, -thisIndex * 620, function () { flag = true; }); circleChange(); thisIndex++; } }, 3000); } function circleChange() { for (var i = 0; i 0 ? num = Math.ceil(num) : num = Math.floor(num); if (offsetLeft == target) { clearInterval(obj.myInter); callback && callback(); } else { obj.style.left = offsetLeft + num + 'px'; } }, 10) } antoChange(); }
    免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理! 图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们,邮箱:ciyunidc@ciyunshuju.com。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

    目录[+]