在网页中一些小的UI元素通常不会引起用户的关注。它们被忽略是因为它们的尺寸,但是我们可以通过给它们一些特别的效果来引起用户的关注。本教程将和大家分享一些奇妙的圆点按钮效果和样式。当用户用鼠标hover或点击圆点按钮时,产生各种美妙的动画效果。我们将使用的技术包括css transitions、伪元素、透视效果和SVG。
注意:不是每一个浏览器都支持SVG transition和3D transform-style,最好是使用Chrome或Firefox浏览器观看。
HTML结构:
html结构非常简单:
<div class="dotstyle dotstyle-fillup">
<ul>
<li class="current"><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Products</a></li>
<li><a href="#">Portfolio</a></li>
<li><a href="#">Blog</a></li>
<li><a href="#">Contact</a></li>
</ul>
</div>
在一些demo中,我们添加了一些额外的空列表元素,这样做的目的是为了产生当前元素“移动”到圆点按钮的动画效果。
CSS样式:
以下是通用样式:
.dotstyle ul {
position: relative;
display: inline-block;
margin: 0;
padding: 0;
list-style: none;
cursor: default;
}
.dotstyle li {
position: relative;
display: block;
float: left;
margin: 0 16px;
width: 16px;
height: 16px;
cursor: pointer;
}
.dotstyle li a {
top: 0;
left: 0;
width: 100%;
height: 100%;
outline: none;
border-radius: 50%;
background-color: #fff;
background-color: rgba(255,255,255,0.3);
text-indent: -999em;
cursor: pointer; /* make the text accessible to screen readers */
position: absolute;
}
下面的例子是一个“填充”效果,我们通过设置a元素overflow:hidden,并用它的伪元素实现“填充”效果。
/* Fill up */
.dotstyle-fillup li a {
overflow: hidden;
background-color: rgba(0,0,0,0);
box-shadow: inset 0 0 0 2px rgba(255,255,255,1);
transition: background 0.3s;
}
.dotstyle-fillup li a::after {
content: '';
position: absolute;
bottom: 0;
height: 0;
left: 0;
width: 100%;
background-color: #fff;
box-shadow: 0 0 1px #fff;
transition: height 0.3s;
}
.dotstyle-fillup li a:hover,
.dotstyle-fillup li a:focus {
background-color: rgba(0,0,0,0.2);
}
.dotstyle-fillup li.current a::after {
height: 100%;
}
下面的例子是stroke效果。我们使用SVG来画圆圈,并用stroke-dashoffset属性来使圆圈运动起来。
/* SVG draw circle stroke */
.dotstyle-drawcircle li {
width: 18px;
height: 18px;
}
.dotstyle-drawcircle li a {
top: 3px;
left: 3px;
width: 12px;
height: 12px;
background-color: #c44d48;
-webkit-transition: opacity 0.3s;
transition: opacity 0.3s;
}
.dotstyle-drawcircle li svg {
z-index: 10;
}
.dotstyle-drawcircle li svg circle {
opacity: 0;
fill: none;
stroke: #fff;
stroke-width: 3;
stroke-linecap: round;
stroke-linejoin: round;
stroke-dasharray: 39 39;
stroke-dashoffset: 39; /* ~ length of circle path (pi*2r) */
transition: stroke-dashoffset 0.3s, opacity 0.3s;
}
.dotstyle-drawcircle li.current a,
.dotstyle-drawcircle li a:hover,
.dotstyle-drawcircle li a:focus {
opacity: 0.5;
}
.dotstyle-drawcircle li.current svg circle {
opacity: 1;
stroke-dashoffset: 0;
transition: stroke-dashoffset 0.3s, opacity 0.15s;
}
在“hop”效果中,我们使用了一些javascript来切换a元素的class。
其它效果请参看下载包中的css文件。
本教程就到这里,希望它对你有所帮助。