clip实现圆环环绕效果

3 篇文章 0 订阅

    最近遇到一个需求,需要实现一个圆形环绕效果,通过查阅相关资料,打算通过clip来实现圆环环绕的效果

1. Cliip

首先查看clip的定义:
在这里插入图片描述
在这里插入图片描述
通过定义来理解可能还是有点困难,下面通过几个例子可以快速帮你理解Clip的用法。

2.Clip实例

    首先通过下列代码创建一个矩形框

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style>
			.box{
				position: absolute;
				border: 1px solid black;
				top: 200px;
				left: 200px;
				width: 200px;
				height: 200px;
			}
			.clips{
				background-color: red;
				position: absolute;
				width: 200px;
				height: 200px;
				border: 1px solid black;
			}
		</style>
	</head>
	<body>
		<div class="box">
			<div class="clips"></div>
		</div>
	</body>
</html>

效果如下:在这里插入图片描述
然后通过clip裁剪矩形,使其显示左上角区域的矩形,

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style>
			.box{
				position: absolute;
				border: 1px solid black;
				top: 200px;
				left: 200px;
				width: 200px;
				height: 200px;
			}
			.clips{
				background-color: red;
				position: absolute;
				width: 200px;
				height: 200px;
				border: 1px solid black;
				clip: rect(0px,100px,100px,0px);
			}
		</style>
	</head>
	<body>
		<div class="box">
			<div class="clips"></div>
		</div>
	</body>
</html>

效果如下:
在这里插入图片描述
我们现在来分析一下这个clip: rect(0px,100px,100px,0px);弄清了这个也就可以弄清clip裁剪图形显示圆环的原理了。根据定义rect的参数分别为top,right,bottom,left四个,则可以理解为clip根据这四个参数确定的矩形进行裁剪图形,而这四个参数所确定的矩形正如图上所示的红色区域。如何确定裁剪区域能,很简单,top和bottom确定的是距离顶部和底部距离,则可以看成两个平行于x轴的直线,对于left和right则可以看成平行于y轴的直线,则这四条直线相交确定的矩形区域就是裁剪区域。根据这个道理,我想聪明的你因该知道如果要裁剪右上角则rect该怎么写吧
clip:rect(0px,200px,100px,100px)同理对于右下角clip:rect(100px,200px,200px,100px),左下角clip:rect(100px,100px,0x,0px).

3.绘制圆环以及环绕效果

    现在我们知道了clip的用法了,那怎么实现圆环或圆形流线效果呢,很简单只需要对圆形进行裁剪就可以了.
    1. 首先如何绘制一个圆形区域,很简单通过border-radius属性即可。来上代码.

	<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style>
			.box{
				position: absolute;
				top: 200px;
				left: 200px;
				width: 200px;
				height: 200px;
			}
			.clips{
				background-color: red;
				position: absolute;
				width: 200px;
				height: 200px;
				border: 2px solid black;
				border-radius: 50%;
				clip:rect(0px,220px,100px,100px)

			}
		</style>
	</head>
	<body>
		<div class="box">
			<div class="clips"></div>
		</div>
	</body>
</html>

效果如图所示:
在这里插入图片描述
这是裁剪了右上角的区域,显示的1/4个圆。完整的未裁剪的形状如图下所示;
在这里插入图片描述
那么,如何显示圆环环绕效果呢,首先去除背景颜色,加载单独的边框线条.然后旋转裁剪矩形,这样裁剪区域的轨迹就是一个圆形.

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style>
			.box{
				position: absolute;
				top: 200px;
				left: 200px;
				width: 200px;
				height: 200px;
			}
			.clips{
				position: absolute;
				width: 200px;
				height: 200px;
				border: 2px solid black;
				border-radius: 50%;
				clip:rect(0px,220px,100px,100px);
			}
		</style>
	</head>
	<body>
		<div class="box">
			<div class="clips"></div>
		</div>
	</body>
	<script>
		let clip = document.querySelector('.clips');
		rotate = 0;
		let loop = setInterval(() => {
		    if(rotate>100)
			rotate = rotate-100 ; 
		    rotate++;
		    clip.style.transform = 'rotate('+ 3.6*rotate + 'deg)';
		},100)
	</script>
</html>

在这里插入图片描述

上面已经实现了一个能够旋转的圆形线条了,那么怎么实现两个圆形线条环绕效果呢,很简单只需要再加上一个div,初始的时候裁剪左下角,然后两者旋转角度一致,这样就可以保证两个圆形线条环绕的效果.

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style>
			.box{
				position: absolute;
				top: 200px;
				left: 200px;
				width: 200px;
				height: 200px;
			}
			.clips{
				position: absolute;
				width: 200px;
				height: 200px;
				border: 2px solid black;
				border-radius: 50%;
				clip:rect(0px,220px,100px,100px);
			}
			.clips2{
				position: absolute;
				width: 200px;
				height: 200px;
				border: 2px solid black;
				border-radius: 50%;
				clip:rect(100px,100px,200px,0px);
			}
		</style>
	</head>
	<body>
		<div class="box">
			<div class="clips"></div>
			<div class="clips2"></div>
		</div>
	</body>
	<script>
		let clip = document.querySelector('.clips');
		let clip2 = document.querySelector('.clips2');
		rotate = 0;
		let loop = setInterval(() => {
		    if(rotate>100)
			rotate = rotate-100 ; 
		    rotate++;
		    clip.style.transform = 'rotate('+ 3.6*rotate + 'deg)';
			clip2.style.transform = 'rotate('+ 3.6*rotate + 'deg)';
		},100)
	</script>
</html>

在这里插入图片描述
这样就实现两个环绕的圆形效果.

4. 如何实现圆环动态加载类似于进度条加载

<fonr size="2> 对于进度条加载,可以看出两个半圆弧线,开始的时候,左半圆弧开始旋转到右边,然后转到180度的时候,右半圆弧开始旋转到左边,转到360度的时候,进度条加载完毕.代码如下

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style>
			.box{
				position: absolute;
				top: 200px;
				left: 200px;
				width: 200px;
				height: 200px;
				clip: rect(0 210px 210px 100px);/*这用于遮蔽开始时候左半圆弧,这样开始就可以从右上角逐步显示圆弧*/
			}
			.clips{
				position: absolute;
				width: 200px;
				height: 200px;
				border: 2px solid black;
				border-radius: 50%;
				clip:rect(0px,100px,210px,0px);
			}
			.clips2{
				position: absolute;
				width: 200px;
				height: 200px;
				border: 2px solid black;
				border-radius: 50%;
				clip:rect(0px,210px,210px,100px);

			}
		</style>
	</head>
	<body>
		<div class="box">
			<div class="clips"></div>
			<div class="clips2"></div>
		</div>
	</body>
	<script>
		let box  = document.querySelector('.box')
		let clip = document.querySelector('.clips');
		let clip2 = document.querySelector('.clips2');
		rotate = 0;
		let loop = setInterval(() => {
		<!-- 通过角度判断哪个半圆进行显示 -->
			if(rotate>100){
				rotate = rotate-100 ; 
				clip2.style.display= "none" ; 
				box.style.clip = "rect(0 210px 210px 100px)" ;
			}else if(rotate>50){
				clip2.style.display = "inline-block" ;
				clip2.style.transform = 'rotate('+ 3.6*(rotate-50) + 'deg)';
				box.style.clip = "auto"
			}else{
				clip2.style.display= "none" ; 
				clip.style.transform = 'rotate('+ 3.6*rotate + 'deg)';
			}
		    rotate++;
		    
		},100)
	</script>
</html>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值