CSS 经典实例
三维立方体
实时编辑器
<div class="bruce flex-ct-x" data-title="三维立方体"> <div class="td-cube"> <ul> <li class="front">1</li> <li class="back">2</li> <li class="top">3</li> <li class="bottom">4</li> <li class="left">5</li> <li class="right">6</li> </ul> </div> </div>
结果
Loading...
$width: 150px;
$height: 150px;
$length: 150px;
.td-cube {
width: $width;
height: $height;
perspective: 1000px;
ul {
position: relative;
width: 100%;
height: 100%;
transform: rotateX(-15deg) rotateY(15deg);
transform-style: preserve-3d;
animation: rotate 5s infinite linear;
}
li {
position: absolute;
display: flex;
align-items: center;
justify-content: center;
width: $width;
height: $height;
color: #fff;
font-size: 50px;
opacity: 0.8;
&.front {
background-color: #f66;
transform: translateZ($length / 2);
}
&.back {
background-color: #66f;
transform: rotateY(180deg) translateZ($length / 2);
}
&.top {
background-color: #f90;
transform: rotateX(90deg) translateZ($height / 2);
}
&.bottom {
background-color: #09f;
transform: rotateX(-90deg) translateZ($height / 2);
}
&.left {
background-color: #9c3;
transform: rotateY(-90deg) translateZ($width / 2);
}
&.right {
background-color: #3c9;
transform: rotateY(90deg) translateZ($width / 2);
}
}
}
@keyframes rotate {
from {
transform: rotateY(0) rotateX(0);
}
to {
transform: rotateY(-1turn) rotateX(-1turn);
}
}
使用:hover 控制悬浮边框
<div class="bruce flex-ct-x" data-title="使用:hover控制悬浮边框">
<div class="dynamic-border">iCSS</div>
</div>
.dynamic-border {
width: 200px;
height: 80px;
color: #f66;
font-weight: bold;
font-size: 50px;
line-height: 80px;
text-align: center;
background: linear-gradient(0, #f66 2px, #f66 2px) no-repeat left top/0 2px, linear-gradient(
-90deg,
#f66 2px,
#f66 2px
) no-repeat right top/2px 0,
linear-gradient(-180deg, #f66 2px, #f66 2px) no-repeat right bottom/0 2px, linear-gradient(
-270deg,
#f66 2px,
#f66 2px
) no-repeat left bottom/2px 0;
cursor: pointer;
transition: all 300ms;
&:hover {
background-size: 100% 2px, 2px 100%, 100% 2px, 2px 100%;
}
}
标签导航
<div class="bruce flex-ct-x" data-title="标签导航">
<div class="tab-navbar">
<input id="tab1" type="radio" name="tabs" hidden checked />
<input id="tab2" type="radio" name="tabs" hidden />
<input id="tab3" type="radio" name="tabs" hidden />
<input id="tab4" type="radio" name="tabs" hidden />
<nav>
<label for="tab1">标题1</label>
<label for="tab2">标题2</label>
<label for="tab3">标题3</label>
<label for="tab4">标题4</label>
</nav>
<main>
<ul>
<li>内容1</li>
<li>内容2</li>
<li>内容3</li>
<li>内容4</li>
</ul>
</main>
</div>
</div>
.active {
color: #fff;
background-color: #3c9;
}
.tab-navbar {
display: flex;
flex-direction: column-reverse;
width: 300px;
height: 400px;
overflow: hidden;
border-radius: 10px;
input {
&:nth-child(1):checked {
& ~ nav label:nth-child(1) {
@extend .active;
}
& ~ main ul {
background-color: #f66;
transform: translate3d(0, 0, 0);
}
}
&:nth-child(2):checked {
& ~ nav label:nth-child(2) {
@extend .active;
}
& ~ main ul {
background-color: #66f;
transform: translate3d(-25%, 0, 0);
}
}
&:nth-child(3):checked {
& ~ nav label:nth-child(3) {
@extend .active;
}
& ~ main ul {
background-color: #f90;
transform: translate3d(-50%, 0, 0);
}
}
&:nth-child(4):checked {
& ~ nav label:nth-child(4) {
@extend .active;
}
& ~ main ul {
background-color: #09f;
transform: translate3d(-75%, 0, 0);
}
}
}
nav {
display: flex;
height: 40px;
line-height: 40px;
text-align: center;
background-color: #f0f0f0;
label {
flex: 1;
cursor: pointer;
transition: all 300ms;
}
}
main {
flex: 1;
ul {
display: flex;
flex-wrap: nowrap;
width: 400%;
height: 100%;
transition: all 300ms;
}
li {
display: flex;
flex: 1;
align-items: center;
justify-content: center;
color: #fff;
font-weight: bold;
font-size: 20px;
}
}
}
折叠面板
<div class="bruce flex-ct-x" data-title="折叠面板">
<div class="accordion">
<input id="collapse1" type="checkbox" hidden />
<input id="collapse2" type="checkbox" hidden />
<input id="collapse3" type="checkbox" hidden />
<article>
<label for="collapse1">列表1</label>
<p>内容1<br />内容2<br />内容3<br />内容4</p>
</article>
<article>
<label for="collapse2">列表2</label>
<p>内容1<br />内容2<br />内容3<br />内容4</p>
</article>
<article>
<label for="collapse3">列表3</label>
<p>内容1<br />内容2<br />内容3<br />内容4</p>
</article>
</div>
</div>
.accordion {
width: 300px;
article {
cursor: pointer;
& + article {
margin-top: 5px;
}
}
input {
&:nth-child(1):checked ~ article:nth-of-type(1) p,
&:nth-child(2):checked ~ article:nth-of-type(2) p,
&:nth-child(3):checked ~ article:nth-of-type(3) p {
max-height: 600px;
border-bottom-width: 1px;
}
}
label {
display: block;
height: 40px;
padding: 0 20px;
color: #fff;
font-size: 16px;
line-height: 40px;
background-color: #f66;
cursor: pointer;
}
p {
max-height: 0;
padding: 0 20px;
overflow: hidden;
line-height: 30px;
border: 1px solid #f66;
border-top: none;
border-bottom-width: 0;
transition: all 500ms;
}
}
星级评分
<div class="bruce flex-ct-x" data-title="星级评分">
<div class="star-rating">
<input type="radio" name="rate" />
<input type="radio" name="rate" />
<input type="radio" name="rate" />
<input type="radio" name="rate" />
<input type="radio" name="rate" />
</div>
</div>
.star-rating {
display: flex;
flex-direction: row-reverse;
input {
width: 30px;
height: 30px;
font-size: 30px;
line-height: 30px;
text-align: center;
cursor: pointer;
transition: all 300ms;
appearance: none;
&::after {
color: #66f;
transition: all 300ms;
content: '☆';
}
&:hover {
transform: scale(1.2);
}
&:checked,
&:hover {
&::after,
& ~ input::after {
color: #f66;
content: '★';
}
}
}
}
加载中
<div class="bruce flex-ct-x" data-title="加载指示器">
<div class="loading-indicator">加载中<span></span></div>
</div>
.loading-indicator {
color: #09f;
font-size: 16px;
span {
display: inline-block;
height: 1em;
overflow: hidden;
line-height: 1;
vertical-align: -0.25em;
&::after {
display: block;
white-space: pre-wrap;
animation: loading 3s infinite step-start both;
content: '...\A..\A.';
}
}
}
@keyframes loading {
33% {
transform: translate3d(0, -2em, 0);
}
66% {
transform: translate3d(0, -1em, 0);
}
}