使用html+css+js实现轮播图效果

使用html+css+js实现轮播图效果


因为初学js、css,好多属性都不懂,百度查了好久,所以在这里也简单介绍一下我遇到的难点(主要是布局)。

  1. display:flex

    Flex是Flexible Box的缩写,意为"弹性布局",用来为盒状模型提供最大的灵活性。设为Flex布局以后,子元素的float、clear和vertical-align属性将失效。

  2. flex-shrink:0

    flex-shrink 属性指定了 flex 元素的收缩规则。flex 元素仅在默认宽度之和大于容器的时候才会发生收缩,其收缩的大小是依据 flex-shrink 的值。

  3. justify-content:space-between || justify-content:space-around

    justify-content 用于设置或检索弹性盒子元素在主轴(横轴)方向上的对齐方式。

我在图片(这里是div)的父元素里使用display:flex后 图片将水平布局,但是当图片遇到边界依然会换行并且会改变长宽,很丑,所以需要使用flex-shrink:0来阻止它改变长度,因为弹性盒子的flex-wrap属性(定义换行情况)默认是nowrap不换行,所以不用管,图片的设置完毕,焦点、左右移动按钮在它们的父元素里都需要使用position:absolute绝对定位,让它们可以按百分比布局在他们的父元素中,这里我在它们的父元素中依然使用display:flex设置,然后使用justify-content分别让按钮跟焦点以space-between(项目位于各行之间留有空白的容器内。)space-around(项目位于各行之前、之间、之后都留有空白的容器内。) 方式对齐,这样简单的布局就完成了。
至于js部分,由于是初学,可能感觉有点乱,扩展性不好,大家可以自己试着改改。

话不多说,直接上代码。

<!DOCTYPE html>
<html>
<head>
<style>

	html, body{
		margin:0;
		height:100%;
		user-select:none;
		
	}
	#big{
		position:relative;
		margin:100px auto;
		width:500px;
		height:300px;
		border:3px solid black;
		overflow:hidden;
	}
	#middle{
		position:absolute;
		display:flex;
		left:0px;
		
	}
	#middle div{
		color:white;
		height:300px;
		width:500px;
		flex-shrink:0;
		font-size:35px;
		font-weight:bloder;
		text-align:center;
		line-height:270px;
	}
	.btn{
		position:absolute;
		width:100%;
		height:40px;
		display:flex;
		top:40%;
		justify-content:space-between;
	}
	a{
		text-decoration:none;
		color:black;
		height:40px;
		font-size:30px;
		font-weight:bolder;
		transition:transform .3s;
	}
	.lt:hover{
		transform:translateX(-5px)
	}
	.rt:hover{
		transform:translateX(5px)
	}
	ol{
		position:absolute;
		display:flex;
		left:40%;
		margin:0;
		padding:0;
		bottom:10px;
		width:100px;
		height:40px;
		justify-content:space-around;
		
	}
	li{
		list-style:none;
		cursor:pointer;
		height:25px;
		width:25px;
		border-radius:50%;
		background:#c3c3c3;
	}
	li.active{
		background:#f2f2f2;
	}
</style>
</head>
<body>
	<div id="big">
		
		<div id="middle">
			<div class="page" style="background:red">ONE</div>
			<div class="page" style="background:blue">TWO</div>
			<div class="page" style="background:green">THREE</div>
		</div>
		<div class="btn">
			<a href="javascript:void(0)" class="lt" style="background:#c3c3c3;">&lt;</a>
			<a href="javascript:void(0)" class="rt" style="background:#c3c3c3;">&gt;</a>
		</div>
		<ol>
			<li class="bat active"></li>
			<li class="bat"></li>
			<li class="bat"></li>
		</ol>
	</div>

<script>
	var big = document.getElementById('big');
	var middle = document.getElementById('middle');
	var ele = middle.firstElementChild.cloneNode(true);
	var  bats = document.getElementsByTagName('li');
	var btns = document.getElementsByTagName('a');
	var i,value,ts,z = 0;
	var whidhts = getComputedStyle(big)['width'].slice(0,-2);
	var times;
	middle.appendChild(ele);
	middle.style.width = getComputedStyle(big)['width'].slice(0,-2) * 3 + 'px';
	var item = middle.style.width.slice(0,-2);
	
	// 自动轮播动画
	function autoplay(){
		if (z < item){
			i = 0;
			
			function move(){
				if (i < whidhts){
					middle.style.left = middle.style.left.slice(0,-2) - 20 +'px';
					i += 20;
					z += 20;
				}else{
					clearInterval(t);
					setTimeout(batt,5);
				}
			}
		}else{
			z = 0;
			middle.style.left = '0px';
			autoplay()
		}
		var t = setInterval(move,5);
	}
	
	// 让焦点跟着图片移动
	function batt(){
		for (var x=0; x < bats.length; x++){
			bats[x].className = 'bat';
		}
		if (Math.floor(z / whidhts) == 3){
			bats[0].classList.add('active');
		}else{bats[Math.floor(z / whidhts)].classList.add('active')}
	}
	// 点击焦点或左右移动按钮实现的滚动效果
	function buttons(){
		//console.log('Value:'+value + "Z:"+z);
		if (-(value) > -(z)){
			if (z != item){
				middle.style.left = "-" + (-(middle.style.left.slice(0,-2)) - 20 +'px');
				//console.log("MIDDLE.LEFT" + middle.style.left.slice(0,-2));
				z -= 20;
			}else{
				middle.style.left = "0px";
				z = 0;
			}
			
		}else if (-(value) < -(z)){
			middle.style.left = middle.style.left.slice(0,-2) - 20 +'px';
			z += 20;
		}else {
			clearInterval(ts);
			setTimeout(batt,5);
		}
	}
	
	//当鼠标移入图片时暂停轮播, 移出继续播放
	big.onmouseover = () => {clearInterval(times)}
	big.onmouseout = () => {times = setInterval(autoplay,3000)}
	
	// 为左右移动按钮设置单击事件
	for (var p=0; p < btns.length; p++){
		btns[p].id = p;
		btns[p].onclick = function(){
			if (this.id == 1){
				if (z == item){value = whidhts}
				else{value = z + parseInt(whidhts)}
			}else{
				if (z == 0){
					middle.style.left = "-" + (item - 20) +'px';
					value = parseInt(item) - parseInt(whidhts);
					z = item - 20;
				}
				else if (z == item){
					middle.style.left = "-" + (item - 20) +'px';
					value = item - whidhts;
					z = item - 20;
					
				}
				else{
					value = z - whidhts
				}
			}	
			ts = setInterval(buttons,5);
		}
	}
	// 为每个焦点设置单击事件
	for (var p=0; p < bats.length; p++){
		bats[p].id = p;
		bats[p].onclick = function(){
			value = this.id * whidhts;
			ts = setInterval(buttons,5);
			
		}
	}
	
	debugger;
	times = setInterval(autoplay,3000);
	//当切换最小化页面或切换页面时停止播放,返回页面继续播放
	document.onvisibilitychange = () => document.visibilityState == 'hidden' ? clearInterval(times) : times = setInterval(autoplay,3000);
</script>
</body>
</html>

你可能感兴趣的:(css,html,javascript)