offsetLeft之原生js制作轮播

  1. offset:偏移,检测
  2. 作用:获取元素尺寸,获取盒子宽高
    offsetWidth,offsetHeight
    包括宽高本身,padding,border,不包括margin
    offsetHeight=height+border+padding
    offsetHeight = Height+padding+border
  3. offsetTop和offsetLeft 检测距离父盒子有定位的左上的距离(offsetLeft从父亲的padding开始算,父亲的border不算,在父盒子有定位的情况下,offsetLeft=style.left)
  4. offsetParent复习盒子中带有定位的盒子,如果没有定位返回body,如果有返回最近那个
  5. offsetLeft和style.left的最大区别:(a)offsetLeft可以返回没有定位的盒子的距离左侧的位置,如果没有定位,以body为主。而style.left只能获取行内式,如果没有返回“”。(b)offsetLeft返回number,而style.left返回string(c)offsetLeft只能获取值,而style.left可以赋值
  6. 动画封装
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style>
			.box1{
				margin: 0;
				padding: 5px;
				height: 200px;
				background-color: #ddd;
				position: relative;
			}
			button{
				margin: 5px;
			}
			.box2{
				width: 100px;
				height: 100px;
				background-color: red;
				position: absolute;
				left: 0;
			}
		</style>
	</head>
	<body>
		<div class="box1">
			<button>运动到200</button>
			<button>运动到400</button>
			<div class="box2" ></div>
		</div>
		<script>
			var timer=null;
			var btnArr=document.getElementsByTagName("button");
			var box2=document.getElementsByClassName("box2")[0];
			btnArr[0].onclick=function(){
				animate(box2,200);
			}
			btnArr[1].onclick=function(){
				animate(box2,400);
			}
			
			//animate封装
			function animate(ele,target){
				clearInterval(ele.timer);
				var speed=target>ele.offsetLeft?10:-10;
				ele.timer=setInterval(function(){
					var val=target-ele.offsetLeft
					ele.style.left=ele.offsetLeft+speed+"px";
					if(Math.abs(val)<Math.abs(speed)){
						ele.style.left=target+"px";
						clearInterval(ele.timer);
					}
				},30);
			}
			
		</script>
	</body>
</html>

轮播图
7. 滑动焦点图
在这里插入图片描述

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style>
			*{
				padding: 0;
				margin: 0;
			}
			.box{
				width: 490px;
				height: 170px;
				padding: 5px;
				border:1px solid #ccc;
				margin: 100px auto;
			}
			.inner{
				width: 490px;
				height: 170px;
				position: relative;
				overflow: hidden;
			}
			ul{
				width: 500%;
				position: absolute;
				left: 0;
				list-style: none;
			}
			li{
				float: left;
			}
			.square{
				position: absolute;
				bottom:10px ;
				right: 10px;
			}
			span{
				display: inline-block;
				width: 16px;
				height: 16px;
				background-color: #fff;
				margin: 3px;
				border-radius: 20px;
				border: 1px solid #ccc;
				text-align: center;
				line-height: 16px;
				cursor: pointer;
			}
			.current{
				background-color: darkorange;
				color: #fff;
			}
		</style>
		<script>
			window.onload=function(){
				var inner=document.getElementById("inner");
				var ul=inner.children[0];
				var imgWidth=inner.offsetWidth;
				var spanArr=inner.children[1].children;
				
				//绑定事件
				for(var i=0;i<spanArr.length;i++){
					spanArr[i].index=i;
					spanArr[i].onmouseover=function(){
						//点亮盒子排他思想
						for(var j=0;j<spanArr.length;j++){
							spanArr[j].className=""
						}
						this.className="current";
						//移动ul
						animate(ul,-this.index*imgWidth);
					}
					
				}
				
				function animate(ele,target){
					clearInterval(ele.timer);
					var speed=target>ele.offsetLeft?10:-10;
					ele.timer=setInterval(function(){
						var val=target-ele.offsetLeft
						ele.style.left=ele.offsetLeft+speed+"px";
						if(Math.abs(val)<Math.abs(speed)){
							ele.style.left=target+"px";
							clearInterval(ele.timer);
						}
					},10);
				}
			}
		</script>
	</head>
	<body>
		<div class="box">
			<div class="inner" id="inner">
				<ul>
					<li><image src="images/01.jpg" alt="" /></li>
					<li><image src="images/01.jpg" alt="" /></li>
					<li><image src="images/01.jpg" alt="" /></li>
					<li><image src="images/01.jpg" alt="" /></li>
					<li><image src="images/01.jpg" alt="" /></li>
				</ul>
				<div class="square" >
					<span class="current">1</span>
					<span>2</span>
					<span>3</span>
					<span>4</span>
					<span>5</span>
				</div>
			</div>
		</div>
	</body>
</html>
  1. 左右轮播图
    在这里插入图片描述
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style>
			*{
				padding: 0;
				margin: 0;
			}
			#box{
				width: 490px;
				height: 200px;
				padding: 5px;
				border:1px solid #ccc;
				margin: 100px auto;
				position: relative;
				overflow: hidden;
			}
			.ad{
				width: 490px;
				height: 200px;
				position: relative;
				overflow: hidden;
			}
			#box img{
				width:490px;
			}
			ul{
				width:2940px;
				position: absolute;
				left: 0;
				top: 0;
				list-style: none;
			}
			li{
				float: left;
			}
			#arr{
				display: none;
			}
			#box:hover #arr{
				display: block;
			}
			#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>
	</head>
	<body>
		<div class="all" id="box">
			<div class="ad">
				<ul id="imgs">
					<li><img src="images/1.jpg" /></li>
					<li><img src="images/2.jpg" /></li>
					<li><img src="images/3.jpg" /></li>
					<li><img src="images/4.jpg" /></li>
					<li><img src="images/5.jpg" /></li>
				</ul>
			</div>
			<div id="arr">
				<span id="left"><</span>
				<span id="right">></span>
			</div>
		</div>
		<script>
			var box=document.getElementById("box");
			var imgWidth=box.children[0].offsetWidth;
			var btnArr=box.children[1].children;
			var ul=box.children[0].children[0];
			
			//定义计数器
			var index=0;
			//点击右侧图片像左移动
			btnArr[1].onclick=function(){
				index++;
				if(index>ul.children.length-1){
					index=ul.children.length-1;
				}
				animate(ul,-index*imgWidth);
			}
			btnArr[0].onclick=function(){
				index--;
				if(index<0){
					index=0;
				}
				animate(ul,-index*imgWidth);
			}
			
			function animate(ele,target){
				clearInterval(ele.timer);
				var speed=target>ele.offsetLeft?10:-10;
				ele.timer=setInterval(function(){
					var val=target-ele.offsetLeft
					ele.style.left=ele.offsetLeft+speed+"px";
					if(Math.abs(val)<Math.abs(speed)){
						ele.style.left=target+"px";
						clearInterval(ele.timer);
					}
				},10);
			}
		</script>
	</body>
</html>
  1. 定时左右轮播
    在这里插入图片描述
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style>
			*{
				padding: 0;
				margin: 0;
				list-style: none;
				border: 0;
			}
			.all{
				width: 500px;
				height: 200px;
				padding: 7px;
				border: 1px solid #CCCCCC;
				margin: 100px auto;
				position: relative;
			}
			.screen{
				width: 500px;
				height: 200px;
				position: relative;
				overflow: hidden;
			}
			.screen li{
				width: 500px;
				height: 200px;
				overflow: hidden;
				float: left;
			}
			.screen ul{
				position: absolute;
				left: 0;
				top: 0;
				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;
			}
			#all:hover #arr{
				display: block;
			}
			#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>
	</head>
	<body>
		<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>
			var all=document.getElementById("all");
			var screen=all.firstElementChild||all.firstChild;
			var imgWidth=screen.offsetWidth;
			var ul=screen.firstElementChild||screen.firstChild;
			var ol=screen.children[1];
			var div=screen.lastElementChild||screen.firstChild;
			var spanArr=div.children;
			
			//复制第一张图片所在的li,添加到ul的后面
			var ulnewLi=ul.children[0].cloneNode(true);
			ul.appendChild(ulnewLi);
			for(var i=0;i<ul.children.length-1;i++){
				var olnewLi=document.createElement("li");
				olnewLi.innerHTML=i+1;
				ol.appendChild(olnewLi);
			}
			var olLiArr=ol.children;
			olLiArr[0].className="current";
			
			for(var i=0;i<olLiArr.length;i++){
				olLiArr[i].index=i;
				olLiArr[i].onmouseover=function(){
					for(var j=0;j<olLiArr.length;j++){
						olLiArr[j].className="";
					}
					this.className="current";
					animate(ul,-this.index*imgWidth)
				}
			}
			
			//添加定时器
			var timer=setInterval(autoPlay,2000);
			var key=0;
			var square=0;
			function autoPlay(){
				key++;
				if(key>olLiArr.length){
					ul.style.left=0;
					key=1;
				}
				animate(ul,-key*imgWidth)
				square++;
				if(square>olLiArr.length-1){
					square=0;
				}
				for(var j=0;j<olLiArr.length;j++){
					olLiArr[j].className="";
				}
				olLiArr[square].className="current"
			}
			
			//划过清楚定时器
			all.onmouseover=function(){
				clearInterval(timer);
			}
			all.onmouseout=function(){
				timer=setInterval(autoPlay,2000);
			}
			
			//左右按钮切换图片
			spanArr[0].onclick=function(){
				key--;
				if(key<0){
					ul.style.left=-imgWidth*(olLiArr.length)+"px";;
					key=olLiArr.length-1;
				}
				animate(ul,-key*imgWidth)
				square--;
				if(square<0){
					square=olLiArr.length-1;
				}
				for(var j=0;j<olLiArr.length;j++){
					olLiArr[j].className="";
				}
				olLiArr[square].className="current"
			}
			spanArr[1].onclick=function(){
				autoPlay();
			}
			
			function animate(ele,target){
				clearInterval(ele.timer);
				var speed=target>ele.offsetLeft?10:-10;
				ele.timer=setInterval(function(){
					var val=target-ele.offsetLeft
					ele.style.left=ele.offsetLeft+speed+"px";
					if(Math.abs(val)<Math.abs(speed)){
						ele.style.left=target+"px";
						clearInterval(ele.timer);
					}
				},10);
			}
		</script>
	</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值