css实现文字颜色叠加
通过写两个同样的文本标签,顶层文本标签设置overflow: hidden;覆盖实现。
实现效果
html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>
<script type="text/javascript" src="https://cdn.staticfile.org/vue/3.0.5/vue.global.js"></script>
<style>
.text-box {
position: relative;
width: 200px;
height: 40px;
background-color: #f2f2f2;
box-sizing: border-box;
border-radius: 10px;
overflow: hidden;
}
.text {
position: relative;
display: block;
width: 100%;
height: 100%;
text-align: center;
line-height: 40px;
color: #23CA92;
z-index: 1;
}
.text.btn {
cursor: pointer;
background-color: #f2f2f2;
z-index: 3;
}
/* 进度 */
.text-progress {
position: absolute;
height: 100%;
top: 0;
left: 0;
background: #23CA92;
z-index: 2;
}
/* 叠加文本 */
.text-hood {
position: absolute;
height: 100%;
top: 0;
left: 0;
overflow: hidden;
z-index: 2;
}
.text-hood>span {
position: relative;
display: block;
width: 200px; /* 计算text-box宽度 */
height: 100%;
line-height: 40px;
color: white;
white-space:nowrap;
overflow: hidden;
text-align: center;
z-index: 2;
}
</style>
</head>
<body>
<div id="app">
<div class="text-box">
<div class="text-progress" :style="`width: ${progress}%;`"></div>
<div class="text-hood" :style="`width: ${progress}%;`"><span >{{progressStr}}</span></div>
<span v-if="isStart && progress < 100" class="text">{{progressStr}}</span>
<span v-else-if="progress === 100" class="text btn" @click="startInstall">安装失败,重新安装</span>
<span v-else class="text btn" @click="startInstall">开始安装</span>
</div>
</div>
<script type="text/javascript">
const app = {
data() {
return {
isStart: false,
progress: 0
}
},
computed: {
progressStr() {
return `正在安装${this.progress}%`;
}
},
methods: {
startInstall() {
this.isStart = true;
this.progressAnimation();
},
progressAnimation() {
this.progress = 0;
let storNum = 0;
let timing = 50; // 速度
const step = timestamp => {
if(timestamp - storNum >= timing) {
storNum = timestamp;
this.progress += 1;
}
if(this.progress < 99) {
window.requestAnimationFrame(step);
}
else{
this.progress = 100;
window.cancelAnimationFrame(timer);
}
}
const timer = window.requestAnimationFrame(step);
}
}
}
Vue.createApp(app).mount('#app')
</script>
</body>
</html>