1.DIV绘制三角形
<body>
<div id="main"></div>
</body>
</html>
<style>
#main{
width: 0;
height: 0;
border-top: 30px solid transparent;
border-left: 30px solid #C00;
border-right: 30px solid transparent;;
border-bottom: 30px solid transparent;
}
</style>
2.内容居中
<body>
<div id="main">
<div id="son"></div>
</div>
</body>
</html>
<style>
#main{
width: 400px;
height: 400px;
border: 1px solid #C00;
position: relative;
}
#son{
width: 100px;
height: 100px;
position: absolute;
background-color: aquamarine;
left: 50%;
top: 50%;
transform: translate(-50%,-50%);
}
</style>
也可以使用 flex 布局实现。
3.进度条效果
<body>
<div id="main">
<div class="progress-box">
<div class="progress"></div>
</div>
</div>
</body>
</html>
<style>
#main{
width: 400px;
height: 300px;
margin: 20px auto;
display: flex;
justify-content: center;
align-items: center;
}
.progress-box{
width: 160px;
height: 10px;
border-radius: 100px;
border: 1px solid #C00;
}
.progress{
width: 0%;
height: 10px;
border-radius: 100px;
background-color: aquamarine;
transition: width 700ms;
}
#main:hover .progress{
width: 100%;
}
</style>
4. 流光文字效果
<body>
<div class="coolTextBox">
<!-- 文字层 -->
<div class="text coolText">HelloWorld,你好世界</div>
<!-- 流光背景 -->
<div class="bgLight coolText">HelloWorld,你好世界</div>
</div>
</body>
</html>
<style>
body{
background-color: black;
}
.coolText{
font-size: 30px;
font-weight: bold;
}
.coolTextBox{
position: relative;
}
.coolTextBox .bgLight{
display: inline-block;
background-image: url(./lightBG.png);
background-repeat: no-repeat;
-webkit-background-clip: text;
animation-name: textAni;
animation-duration: 2s;
animation-iteration-count: infinite;
position: absolute;
z-index: 10;
top:0px;
}
.coolTextBox .text{
color: rgba(255, 183, 143, 1);
}
@keyframes textAni{
from{
background-position-x: -300%;
}
to{
background-position-x: 300%;
}
}
</style>
背景图只是一个白色的斜线。
5.渐变文字
在上面的基础上修改的,可以做到渐变且有流光的效果
<style>
body{
background-color: black;
}
.coolText{
font-size: 30px;
font-weight: bold;
}
.coolTextBox{
position: relative;
}
.coolTextBox .bgLight{
display: inline-block;
background-image: url(./lightBG.png);
background-repeat: no-repeat;
-webkit-background-clip: text;
animation-name: textAni;
animation-duration: 2s;
animation-iteration-count: infinite;
position: absolute;
z-index: 10;
top:0px;
}
/* 将文字层设置为渐变文字 */
.coolTextBox .text{
color: rgba(0, 0, 0, 0);
background: linear-gradient(133deg, #B97AFB 0%, #6886FF 100%);
-webkit-background-clip: text;
}
@keyframes textAni{
from{
background-position-x: -300%;
}
to{
background-position-x: 300%;
}
}
</style>
5.1 遇到问题
当字数很少的时候会出现刘光动画异常,目前解决方案就是调整背景图的大小
background-size: 30% 100%;