前端简单粗暴方法实现骨架屏(新手可懂)

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<style type="text/css">
		.skt-loading {
			pointer-events: none;
		}

		/* 加载中阻止事件 */
		.skt-loading .skeleton {
			position: relative;
			overflow: hidden;
			border: none !important;
			border-radius: 5px;
			background-color: transparent !important;
			background-image: none !important;
		}

		.skt-loading .skeleton::after {
			content: '';
			position: absolute;
			left: 0;
			top: 0;
			z-index: 9;
			width: 100%;
			height: 100%;
			background-color: #EBF1F8;
			display: block;
		}

		/* 下面这部分都是自定义的,看需求修改 */
		.skt-loading .skeleton::after {
			border-radius: 4px;
		}

		.skt-loading .skeleton::before {
			position: absolute;
			top: 0;
			width: 30%;
			height: 100%;
			content: "";
			background: linear-gradient(to right, rgba(255, 255, 255, 0) 0,
					rgba(255, 255, 255, .3) 50%, rgba(255, 255, 255, 0) 100%);
			transform: skewX(-45deg);
			z-index: 99;
			animation: skeleton-ani 1s ease infinite;
			display: block;
		}



		.skt-loading .skeleton.badge::after {
			background-color: #F8FAFC;
		}

		@keyframes skeleton-ani {

			/* 骨架屏动画 */
			from {
				left: -100%;
			}

			to {
				left: 150%;
			}
		}
	</style>
	<body>
		<div class="skt-loading">
			<div style="width: 500px;height: 30px;background-color: antiquewhite;">
				你好
			</div>
			<div style="width: 500px;height: 30px;background-color: antiquewhite;margin: 20px;">
				你好
				<div style="width: 500px;height: 30px;background-color: antiquewhite;margin: 20px;">
					你好
					<div style="width: 500px;height: 30px;background-color: antiquewhite;margin: 20px;">
						你好
						<div style="width: 500px;height: 30px;background-color: antiquewhite;margin: 20px;">
							你好

						</div>
					</div>
				</div>
			</div>
			<div style="width: 100%;height: 100px;background-color: antiquewhite;margin: 20px;">
				你好
			</div>
			<div style="width: fit-content;">
				<img src="img/test.jpg" />
			</div>
			<div>
				
			</div>
		</div>
	</body>
	<script type="text/javascript">
		function handleSkeleton(type) {
			let child = document.querySelector('.skt-loading').children;
			for (var i = 0; i < child.length; i++) {
				child.item(i).classList[type]('skeleton')
			}
		}

		handleSkeleton('add')
		setTimeout(() => {
			handleSkeleton('remove')
			document.querySelector('.skt-loading').classList.remove('skt-loading');
		}, 3000)
	</script>

</html>

使用骨架屏
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Young soul2

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

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

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

打赏作者

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

抵扣说明:

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

余额充值