JS学习1——PC端轮播图

</!DOCTYPE html>
<html>
<head>
	<title>淘宝轮播图</title>
	<style type="text/css">
		/*清楚整体样式*/
		*{
		    margin:0;
		    padding:0;
		}
		a{
			text-decoration: none;
			color: #ccc;
		}
		li{
			list-style: none;
		}
		.lb{
			width: 533px;
			height: 300px;
			margin:100px auto;
			position: relative;
			overflow: hidden;
		}
		.lb .imgBox{
			position: absolute;
			width: 3000px;
			height: 300px;
		}
		.lb .imgBox li,img{
			position: absolute;
			width: 533px;
			height: 300px;
		}

		.lb .left{
			display: none;
			/* 子绝父相 */
			position: absolute;
			/* 非普通流盒子居中 */
			top:50%;
			left: 0;
			width: 18px;
			height: 30px;
			margin-top: -15px;
			background-color: rgba(0,0,0,.3);
			/* 文本居中 */
			padding-left: 1px;
			line-height: 30px;
			border-radius: 0 15px 15px 0;
			font-weight: bolder;
		}
		.lb .right{
			display: none;
			position: absolute;
			top:50%;
			right: 0;
			width: 18px;
			height: 30px;
			margin-top: -15px;
			background-color: rgba(0,0,0,.3);
			text-align: right;
			padding-right: 1px;
			line-height: 30px;
			border-radius: 15px 0 0 15px;
			font-weight: bolder;
		}
		.left:hover,.right:hover{
			background-color: rgba(0,0,0,.1);
		}
		.circleBox{
			position: absolute;
			left: 50%;
			margin-left: -47px;
			bottom: 20px;
			width: 82px;
			height: 13px;
			background-color: rgba(0,0,0,.1);
			border-radius: 10px;
			padding: 0 7px;
		}
		.circleBox li{
			float: left;
			width: 8px;
			height: 8px;
			border-radius: 4px;
			background-color: white;
			margin-left:7px;  /*外边距为3*/
			margin-top: 2px;
			cursor: pointer;
		}
		.circleBox .current{
			background-color: rgb(241, 71, 100);
		}
	</style>
	<script src="animate.js"></script>
</head>
<body>
	<div class="lb">
		<ul class="imgBox">
			<li><img src="1.jpg"></li>
			<li><img src="2.jpeg"></li>
			<li><img src="3.jpg"></li>
			<li><img src="4.jpeg"></li>
		</ul>
		<!-- <img src="5.jpg" title="5"> -->
		<a href="#" class="left">&lt</a>
		<a href="#" class="right">&gt</a>
		<ul class="circleBox">
		</ul>
	</div>
	<script>
		// num记录当前是第几张图片
		var num=0;
		// 1、图片li的样式
		var lis=document.querySelectorAll('.imgBox li');
		var liWidth=lis[0].offsetWidth;
		for(var i=0;i<lis.length;i++){
			lis[i].style.left=liWidth*i+'px';
		}

		//2、根据图片个数,创建小圆点
		var circleBox=document.querySelector('.circleBox');
		var imgBox=document.querySelector('.imgBox');
		for(var i=0;i<lis.length;i++){
			var li=document.createElement('li');
			// 设置索引号
			li.setAttribute('index',i);
			circleBox.appendChild(li);
			circleBox.style.width=lis.length*15+5+'px';
			circleBox.style.marginLeft=-circleBox.offsetWidth/2+'px';

			//2.1、小圆点的点击事件:排他思想,干掉所有人,留下我自己
			//2.2 点击小圆点,图片ul移动
			li.onclick=function(){
				for(var j=0;j<lis.length;j++){
					circleBox.children[j].className='';
				}
				this.className='current';
				var index=this.getAttribute('index');
				num=index;
				animate(imgBox,-index*liWidth);
			}
		}
		circleBox.children[0].className='current';
		// 2.3 克隆第一个图片,lis的长度依旧不变
		var first=imgBox.children[0].cloneNode(true);
		imgBox.appendChild(first);
		imgBox.children[imgBox.children.length-1].style.left=(imgBox.children.length-1)*liWidth+'px';


		// 3、鼠标经过left 和right显示,离开隐藏
		var lb=document.querySelector('.lb');
		var left=document.querySelector('.left');
		var right=document.querySelector('.right');
		lb.addEventListener('mouseenter',function(){
			left.style.display='block';
			right.style.display='block';
			// 鼠标经过,清除定时器
			clearInterval(timer);
			timer=null;  //清除定时器变量
		})
		lb.addEventListener('mouseleave',function(){
			left.style.display='none';
			right.style.display='none';
			// 鼠标离开,调用定时器自动播放
			timer=setInterval(function(){
				// 手动调用事件
				right.click();
			},2000);

		})

		//4、点击左右键移动图片
		var flag=true;  //节流阀,防止动画播放太快
		right.onclick=function(){
			if(flag){
				flag=false;
				//4.1 无缝隙滚动:在图片最后创建第一个图片的复制样本
				if(num==lis.length){
					imgBox.style.left=0;
					num=0;
				}
				num++;
				animate(imgBox,-num*liWidth,function(){
					flag=true;
				});
				//4.2 滚动同时,小圆点变化
				for(var i=0;i<lis.length;i++){
					circleBox.children[i].className='';
				}
				if(num==lis.length){
					circleBox.children[0].className='current';
				}else{
					circleBox.children[num].className='current';
				}
			}
		}
		left.onclick=function(){
			// 节流阀应用
			if(flag){
				flag=false;
				if(num==0){
					imgBox.style.left=-(lis.length)*liWidth+'px';
					num=lis.length;
				}
				num--;
				animate(imgBox,-num*liWidth,function(){
					flag=true;
				});
				//4.2 滚动同时,小圆点变化
				for(var i=0;i<lis.length;i++){
					circleBox.children[i].className='';
				}
				circleBox.children[num].className='current';
			}
		}

		// 5、自动播放
		// 5.1、刷新后自动播放
		// 5.2、鼠标经过,停止定时器
		// 5.3、鼠标移开,开启定时器
		var timer=setInterval(function(){
			// 手动调用事件
			right.click();
		},2000);
		
	</script>
</body>
</html>

animate.js文件如下:

// 左右移动
// obj为移动对象,target为距离页面左侧的距离
function animate(obj, target,callback) {
    clearInterval(obj.timer);
    obj.timer = setInterval(function () {
      var step = (target - obj.offsetLeft) / 10;
      step = step > 0 ? Math.ceil(step) : Math.floor(step);
        
      if (obj.offsetLeft == target) {
        clearInterval(obj.timer);
        // 如果回调函数存在,则在计时器结束后调用
        // if(callback){
        //     callback();
        // }
        callback && callback();
      }
      
      obj.style.left = obj.offsetLeft + step + "px";
    }, 15)
}

// 上下移动
// obj为移动对象,target为距离页面上方的距离
function animateY(obj, target,callback) {
  clearInterval(obj.timer);
  obj.timer = setInterval(function () {
    var step = (target - window.pageYOffset)/ 10;
    step = step > 0 ? Math.ceil(step) : Math.floor(step);
      
    if (window.pageYOffset == target) {
      clearInterval(obj.timer);
      // 如果回调函数存在,则在计时器结束后调用
      // if(callback){
      //     callback();
      // }
      callback && callback();
    }
    
    window.scroll(0,window.pageYOffset + step);
  }, 15)
}```

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值