jquery+css3实现圆环进度条

这张图就是我们常见的原形进度条,接下来写一下我的制作过程。

首先,构建圆弧,我们先看一下他的html结构。

<div class="circle">
	    <div class="pie_left"><div class="left"></div></div>
	    <div class="pie_right"><div class="right"></div></div>
	    <div class="mask"><span>0</span>%</div>
    </div>

结构很简单如上述代码所示,其实原形进度条的就是有左右俩个半圆弧组成的。接下来我们闲来看看半圆弧怎么形成的。依右半圆为例子。

.circle{
			width:200px;
			display:inline-block;
			height:200px;
			margin:20px;
			position: relative;
			background: #ccc;
			border-radius: 50%;
		}
.pie_right{
			width:200px;
			height:200px;
			position: absolute;
			top:0px;
			left:0px;
		}
.right{
			position: absolute;
			left:0px;
			top:0px;
			border-radius:50%;
			background: green;
			width:100%;
			height:100%;
			
		}
.pie_right, .right{
			clip:rect(0, auto, auto, 100px);
		}

这样如图所示:灰色部分是外层circle,绿色部分是右半圆弧,其实它是整个园只不过对它进行了切割 在css 中添加了“clip:rect(0, auto, auto, 100px);”这样就把它左半边的切除了(clip属性使用: http://www.w3school.com.cn/cssref/pr_pos_clip.asp),接着在css中添加“transform: rotate(-100deg);”圆弧就会旋转了如下图所示:

半圆弧原理就是这样,下面是完整的css 样式:

	.circle{
			width:200px;
			display:inline-block;
			height:200px;
			margin:20px;
			position: relative;
			background: #ccc;
			border-radius: 50%;
		}
	.pie_left, .pie_right{
			width:200px;
			height:200px;
			position: absolute;
			top:0px;
			left:0px;
		}
	.left, .right{
			position: absolute;
			left:0px;
			top:0px;
			border-radius:50%;
			background: green;
			transform:rotate(-180px);
			width:100%;
			height:100%;
			transform: rotate(-180deg);
		}
	.pie_left, .left{
			clip: rect(auto, 100px, auto, auto);
			 /*clip:rect(0,100px,auto,0);*/
		}
	.pie_right, .right{
			clip:rect(0, auto, auto, 100px);
		}
	.mask{
			position: absolute;
			top:20px;
			left:20px;
			width:160px;
			height:160px;
			line-height: 160px;
			font-size:18px;
			font-weight: 600;
			border-radius: 50%;
			background: #fff;
			text-align: center
		}

如图:

接下来就是旋转角度了,也就是左右两个圆弧旋转角度的问题了,如果0%的话两个圆弧都是隐藏不显示的,所以刚开始我们默认两圆弧的旋转角度为“-180deg"。圆弧的旋转角度是有里面的数字百分比决定的。从0%-100%,这样就是每1%占3.6deg,而在50%以内的话只有右半圆弧转动,左半圆弧不显示。这样的话下面我们用jquery来实现圆弧的转动。

<script type="text/javascript">
  	$(function () {
  		$(".circle").each(function(){
  			var num=$(this).find(".mask span").text()*3.6;
  			if(num<=180){
  				// $(this).find(".right").css("transform","rotate("+(num-180)+"deg)");
  				$(this).find(".right").css({"transform":"rotate("+(num-180)+"deg)","transition": "transform 0.5s ease"});
  			}else{
  				$(this).find(".right").css({"transform":"rotate(0deg)", "transition": "transform 0.5s ease-in"});
  				 $(this).find(".left").css({"transform":"rotate("+(num-360)+"deg)","transition": "transform 0.2s 0.5s ease-in"});
  				// $(this).find(".right").css("transform","rotate(0deg)");
  				// $(this).find(".left").css("transform","rotate("+(num-360)+"deg)");
  			}
  		})
  	})

  </script>

这样一来就可以了.

转载于:https://my.oschina.net/conqueror/blog/727713

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用jQuery和CSS实现滑动的标签分栏切换效果,具体步骤如下: HTML结构: ```html <div class="tab-wrap"> <div class="tabs"> <a href="#" class="active">Tab 1</a> <a href="#">Tab 2</a> <a href="#">Tab 3</a> </div> <div class="tab-content"> <div class="tab-pane active">Tab 1 Content</div> <div class="tab-pane">Tab 2 Content</div> <div class="tab-pane">Tab 3 Content</div> </div> </div> ``` CSS样式: ```css .tab-wrap { position: relative; width: 100%; overflow: hidden; } .tabs { display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px; } .tabs a { position: relative; padding: 10px; color: #000; text-decoration: none; cursor: pointer; } .tabs a.active:before { content: ""; position: absolute; bottom: 0; left: 0; width: 100%; height: 2px; background-color: #000; transform: scaleX(1); transition: transform 0.3s ease-in-out; } .tabs a:hover:before { transform: scaleX(1); } .tab-content { position: relative; height: 300px; overflow: hidden; } .tab-pane { position: absolute; top: 0; left: 0; width: 100%; height: 100%; opacity: 0; transition: opacity 0.3s ease-in-out; } .tab-pane.active { opacity: 1; } ``` JavaScript代码: ```javascript $(function() { var $tabs = $(".tabs a"); var $tabContent = $(".tab-content .tab-pane"); $tabs.on("click", function(e) { e.preventDefault(); var $this = $(this); var target = $this.attr("href"); $tabs.removeClass("active"); $this.addClass("active"); $tabContent.removeClass("active"); $(target).addClass("active"); var offsetLeft = $this.offset().left - $tabs.offset().left; $(".tabs:before").css("transform", "translateX(" + offsetLeft + "px)"); }); }); ``` 首先,我们给标签分栏容器设置了一个相对定位和一个隐藏溢出。然后,在标签容器中创建了一个带有活动类的链接,并将其余标签链接设置为不活动状态。接着,在内容容器中创建了一个带有活动类的选项卡面板,并将其余选项卡面板设置为不活动状态。 在CSS部分,我们使用了Flexbox布局来设置选项卡链接,使它们在选项卡容器中居中对齐并沿水平轴均匀分布。我们给选项卡链接添加了一个:hover伪类,以便在鼠标悬停时显示下划线。我们还使用了CSS过渡效果来创建平滑的动画效果。 JavaScript部分,我们使用jQuery来捕捉选项卡链接的点击事件,并在单击选项卡链接时将其标记为活动状态,并显示相应的选项卡面板。我们还使用jQuery计算出选项卡链接的偏移量,并使用CSS transform属性将指示器移动到正确的位置。 这就是滑动标签分栏切换效果的实现方法。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值