JavaScript轮播图

JavaScript轮播图

JavaScript

window.onload = function () {
	//自定义获取元素样式方法
	function getStyle(obj, name) {
		if(window.getComputedStyle) {
			return getComputedStyle(obj, null)[name];
		}else {
			return  obj.currentStyle[name];
		}
	}
	/*动画方法
   * 参数:
   *   obj:要执行动画的对象
   *   attr:动画执行样式
   *   target:执行动画目标位置
   *   speed:移动的速度(正数向右运动, 负数向左运动)
   *   callback:回调函数,在动画执行完毕后执行
   * */
	function move(obj, attr, target, speed, callback){

		// 关闭之前定时器
		clearInterval(obj.timer);

		//开启定时器
		obj.timer = setInterval(function () {

			//获取原left值
			let oldValue = parseInt(getStyle(obj, attr));

			//设置新left值,不断递增
			let newValue = oldValue + speed;

			/*
			 * 向左移动时,需要判断newValue是否小于target
			 * 向右移动时,需要判断newValue是否大于target
			 * 递增最大值为target值
			 * */
			if ((speed < 0 && newValue < target) || (speed > 0 &&  newValue > target)) {
				newValue = target;
			}

			//设置left不断递增
			obj.style[attr] = newValue + "px";

			// 判断目标位置
			if(newValue === target) {
				// 到达目标关闭定时器
				clearInterval(obj.timer);

				// 动画执行完毕执行函数
				callback();
			}
		},20);
	}

	// 获取box1
	const box1 = document.getElementById("box1");
	//获取按钮id
	const btn1 = document.getElementById("btn1");
	const btn2 = document.getElementById("btn2");
	// 获取navUl
	const navUl = document.getElementById("navUl");
	let navLi = navUl.getElementsByTagName("li");

	// 获取导航拦li
	const imgUl = document.getElementById("imgUl");
	let imgLi = imgUl.getElementsByTagName("li");

	//index存储下标
	let index = 0;

	//鼠标经过图片样式
	box1.addEventListener('mouseenter', function () {
		btn1.style.display = "block";
		btn2.style.display = "block";
		box1.style.cursor = "pointer";
		// 关闭定时器
		clearInterval(timer);
		timer = null;
	})

	//鼠标离开图片样式
	box1.addEventListener('mouseleave', function () {
		btn1.style.display = "none";
		btn2.style.display = "none";
		// 开启定时器
		timer = setInterval(function () {
			btn2.click();
		},2000);

	})

	//根据图片数量动态生成导航栏
	for (let i = 0; i < imgLi.length; i++){

		//创建li
		let li = document.createElement("li");

		//自定义属性,记录下标
		li.setAttribute('index', i);

		// 添加进navUl
		navUl.appendChild(li);

		//给li添加是点击事件
		li.addEventListener('click', function () {

			// 遍历清除li原有class
			for (let  j = 0; j < navLi.length; j++){
				navLi[j].className = "";
			}
			// 点击这获取选中class样式
			navLi[index].className = 'pitch';

			// 获取当前索引
			index = this.getAttribute('index');

			// 点击圆点切换图片:索引 * div宽
			move(imgUl, "left",  -box1.offsetWidth * index, -10, function (){});
		})
	}

	// 初始化navLi样式样式选中
	navLi[0].className = 'pitch';

	//设置导航拦动态居中:(box1宽 - navUl宽) / 2
	navUl.style.left = (box1.offsetWidth - navUl.offsetWidth) / 2 + "px";

	// 左按钮
	btn1.onclick = function () {
		index--;
		if (index < 0){
			index = imgLi.length - 1;
		}
		move(imgUl, "left",  -box1.offsetWidth * index, 10, function (){
			eliminate();
			clearInterval(timer);
			timer = null;
		})
	}

	// 右按钮
	btn2.onclick = function () {
		index++;
		if (index > imgLi.length - 1){
			index = 0;
		}
		move(imgUl, "left",  -box1.offsetWidth * index, -10, function (){
			eliminate();
			clearInterval();
		})
	}

	//启动轮播
	 let timer = setInterval(function () {
		btn2.click();
	},2000);

	// 重定位小圆点
	function eliminate(){
		// 遍历清除li原有class
		for (let  j = 0; j < navLi.length; j++){
			navLi[j].className = "";
		}
		// 点击这获取选中class样式
		navLi[index].className = 'pitch';
	}
}

 HTML5+CSS3

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>轮播图</title>
  <style>
      *{padding: 0; margin: 0;}

      #box1{
          box-sizing: border-box;
          margin: 20px auto;
          width: 666px;
          height: 666px;
          border: 1px solid;
          position: relative;
          overflow: hidden;
      }

      #imgUl{
          display: flex;
          width: 100%;
          height: 100%;
          position: relative;
          left: 0;
          top: 0;
      }
      img{
          width: 666px;
          height: 666px;
      }

      #btn1, #btn2{
          position: absolute;
          top: 50%;
          font-size: 20px;
          background-color: unset;
          border-radius: 20%;
          padding: 10px;
      }
      #btn1{ left: 0; }
      #btn2{ right: 0; }
      #btn1:hover,
      #btn2:hover{
          background-color: rgba(243, 240, 240, 0.5);
      }

      #navUl{
          display: flex;
          position: absolute;
          bottom: 15px;
      }
      #navUl>li{
          background-color: red;
          width: 15px;
          height: 15px;
          list-style: none;
          margin-right: 20px;
          border-radius: 50%;
          cursor: pointer;
      }

      #navUl>li:hover{ background-color: #41a835;}
      .pitch{background-color: #41a835 !important;}
  </style>
  <script type="text/javascript" src="../js/wzlbt.js"></script>

</head>
<body>

  <div id="box1">
    <ul id="imgUl">
      <li><img src="../img/2.jpg" alt="完整轮播图"></li>
      <li><img src="../img/3.jpg" alt="完整轮播图"></li>
      <li><img src="../img/4.jpg" alt="完整轮播图"></li>
      <li><img src="../img/5.jpg" alt="完整轮播图"></li>
      <li><img src="../img/6.jpg" alt="完整轮播图"></li>
      <li><img src="../img/7.jpg" alt="完整轮播图"></li>
      <li><img src="../img/8.jpg" alt="完整轮播图"></li>
      <li><img src="../img/9.jpg" alt="完整轮播图"></li>
    </ul>

    <button id="btn1"> < </button>
    <button id="btn2"> > </button>

    <ul id="navUl">

    </ul>
  </div>
</body>

</html>

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

ZiWie丶ZHANG

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

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

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

打赏作者

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

抵扣说明:

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

余额充值