JSdemo:上下滚动的轮播图封装

记录一下,方便后续用。


<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Demo</title>
	<style>
		* {
			padding: 0;
			margin: 0;
		}
		#box {
			position: relative;
			width: 100px;
			height: 30px;
			overflow: hidden;
		}
		#box ul {
			position: absolute;
			top: 0;
			left: 0;
			width: 100%;
			height: 400%;
		}
		#box ul li {
			width: 100%;
			height: 25%;
			font-size: 20px;
		}
	</style>
</head>
<body>
	<div id="box">
		<ul>
			<li>111</li>
			<li>222</li>
			<li>333</li>
		</ul>
	</div>
	<script>
		const box = document.getElementById('box')
		const ulTips = document.getElementsByTagName('ul')[0]
		let numTips = 0
		let objTips = {
			4: ['444', '999'],
			5: ['555', '1020'],
			29: ['2901', '2902'],
			30: ['3001', '3002']
		}
		ulTips.children[0].innerHTML = objTips[4][0]
		ulTips.children[1].innerHTML = objTips[4][1]
		ulTips.children[2].innerHTML = objTips[5][0]
		const liTipsCopy = ulTips.children[0].cloneNode(true)
		ulTips.appendChild(liTipsCopy)
		console.log('ulTips: ', ulTips.offsetTop, ulTips.children[0].offsetHeight)
		function animate(obj, target, callback) {
			clearInterval(obj.timer)
			function move() {
				var step = (target - obj.offsetTop) / 10;
				step = step > 0 ? Math.ceil(step) : Math.floor(step)
				if(obj.offsetTop == target) {
					clearInterval(obj.timer);
					callback&&callback();
				}
				obj.style.top = obj.offsetTop + step + 'px'
				
			}
			obj.timer = setInterval(move, 40)
		}
		setInterval(() => {
			if(numTips === ulTips.children.length - 1) {
				ulTips.style.top = 0
				numTips = 0
			} else {
				numTips++
			}
			animate(ulTips, -numTips * ulTips.children[0].offsetHeight)
		}, 1500)
	</script>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值