2014-11-20 101 views
3

我很努力为网站实现自定义进度条。这是形状,它应该有:自定义形状进度条

Progress bar with nothing selected

当用户选择了一圈,我想行(也是唯一的线,而不是圆)来填充不同的颜色,直到它到达圆,最后那个红点应该出现在中间,有这个作为最终结果,如果用户点击了第三圈:

Progress bar with item 3 selected and path highlighted

我不知道什么可能是最好的主意,这个简单的方法。我尝试了一些纯CSS,jQuery和JavaScript解决方案,但没有人可以重新创建这种效果。我是否应该有两个图像并逐渐覆盖它们,直到只到达单击的点?我应该完全忘记图像并尝试使用CSS或SVG重新创建图形并更改某个部分的颜色?

我通常都知道问题在这里有一些代码,但我不能表现出任何,因为我没有什么办法的主意,采取和研究网上导致解决方案的无穷多小时,并不适用于我的情况。

在此先感谢。

回答

7

它与CSS的混合和一点点的jQuery相当简单。

// Add click handler to the original dots 
 
$("UL.progress LI").click(function(e) { 
 
    // Deselect current selection 
 
    $("UL.progress LI.selected").removeClass("selected"); 
 
    var newDot = $(this); 
 
    // Which dot are we selecting? 
 
    var newProgressWidth = newDot.index(); 
 
    // Animate the new width of the red line 
 
    $("UL.progress LI.progressline").animate(
 
     {'width': (newProgressWidth * 90) + 'px'}, 
 
     400, 
 
     function() { 
 
      // When done, select the new dot 
 
      newDot.addClass("selected"); 
 
     }); 
 

 
}); 
 

 
// Add the black and red bars as additional <li> elements 
 
// without click handlers 
 
$("<li>").addClass("blackbar").appendTo("UL.progress"); 
 
$("<li>").addClass("progressline").appendTo("UL.progress"); 
 

 
// Select the first dot 
 
$("UL.progress LI").first().addClass("selected");
UL.progress { 
 
    list-style: none; 
 
    padding: 0; 
 
    position: relative; 
 
} 
 

 
/* the black dots */ 
 
UL.progress LI { 
 
    float: left; 
 
    width: 60px; 
 
    height: 60px; 
 
    background-color: black; 
 
    border-radius: 50%; 
 
    margin-left: 30px; 
 
    position: relative; 
 
    cursor: pointer; 
 
} 
 

 
/* first black dot has no gap to the left */ 
 
UL.progress LI:first-child { 
 
    margin-left: 0; 
 
} 
 

 
/* red dot when selected */ 
 
UL.progress LI.selected:after { 
 
    content: ''; 
 
    display: block; 
 
    position: absolute; 
 
    top: 15px; 
 
    left: 15px; 
 
    width: 30px; 
 
    height: 30px; 
 
    background-color: red; 
 
    border-radius: 50%; 
 
} 
 

 

 
/* the black and red lines at the back*/ 
 
UL.progress LI.blackbar, 
 
UL.progress LI.progressline { 
 
    z-index: -2; 
 
    content: ''; 
 
    display: block; 
 
    position: absolute; 
 
    top: 28px; 
 
    left: 30px; /* 60 (diameter)/2 */ 
 
    width: 450px; /* 5*60 + 5*30 (dot diameter and gap) */ 
 
    height: 4px; 
 
    background-color: black; 
 
    margin-left: 0; 
 
    border-radius: 0; 
 
} 
 

 
/* the black line */ 
 
UL.progress LI.blackbar { 
 
    z-index: -2; 
 
    background-color: black; 
 
} 
 

 
/* the red progress line */ 
 
UL.progress LI.progressline { 
 
    z-index: -1; 
 
    background-color: red; 
 
    width: 0; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
Example progress bar<br/> 
 

 
<ul class="progress"> 
 
    <li></li> 
 
    <li></li> 
 
    <li></li> 
 
    <li></li> 
 
    <li></li> 
 
    <li></li> 
 
</ul>

+2

这是不可思议的,这正是我的意思,非常感谢!我会把你的名字评论上面的CSS和jQuery。 – 2014-11-21 15:06:19

1

我会直接创建上面黑的一条红线。然后使用jquery的动画来增加宽度,直到达到所需的圆。然后,一旦完成,做一些类似的做红圈(如果你想它扩大,否则就把它放在那里)

0

简单的方法是使黑色线和点的SVG或PNG BG使用它作为背景repeat-x,然后将红色部分也作为图像放在bg顶部,并使用新的html元素,并使用background-repeat。然后,您可以使用css/js更改宽度为红色的元素以填充进度条。