学习链接
Git Hub前端50天50个项目 | 第24 内容文本
纯CSS渐变动画
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/85d293f4568dc3a0a233b9a5196b5ea1.gif#pic_center)
<style lang="scss" scoped>
.card-wrapper {
width: 100%;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
}
.img {
width: 100%;
height: 100%;
object-fit: cover;
}
.card {
width: 260px;
border-radius: 6px;
overflow: hidden;
background-color: #fff;
box-shadow: 0 2px 5px 0 rgba(0,0,0,0.05);
.card-header {
height: 170px;
}
}
// 卡片头部图片
.card-header img {
width: 100%;
}
// 卡片内容
.card-content {
padding: 20px;
.card-title {
font-weight: bold;
}
.card-bio {
margin: 5px 0 10px;
font-size: 14px;
color: #7c7c7c;
}
.author {
display: flex;
.author-avatar {
width: 42px;
height: 42px;
border-radius: 50%;
overflow: hidden;
flex-shrink: 0;
}
.author-intro {
padding-left: 8px;
flex: 1;
overflow: hidden;
display: flex;
flex-direction: column;
justify-content: space-around;
.author-name {
font-size: 16px;
text-decoration: none;
color: #000;
text-overflow: ellipsis;
white-space: nowrap;
overflow: hidden;
}
.author-date {
font-size: 13px;
color: #7c7c7c;
}
}
}
}
.animated_txt {
height: 20px;
border-radius: 8px;
}
.animated_bg {
background-image: linear-gradient(to right,
#f6f7f8 0,
#d0d1d2 25%,
#f6f7f8 50%,
#f6f7f8 100%
);
background-size: 200% 100%;
// background-position: 0% 0;
animation: grad 1s linear infinite;
}
@keyframes grad {
0% {
background-position: 0% 0;
}
100% {
background-position: -200% 0;
}
}
</style>
<template>
<div class="card-wrapper">
<div class="card">
<div class="card-header animated_bg">
</div>
<div class="card-content">
<div class="card-title animated_bg animated_txt">
</div>
<p class="card-bio animated_bg animated_txt">
</p>
<div class="author">
<a href="#" class="author-avatar animated_bg">
</a>
<div class="author-intro">
<a href="#" class="author-name animated_bg animated_txt">
</a>
<div class="author-date animated_bg animated_txt">
</div>
</div>
</div>
</div>
</div>
</div>
</template>
<script>
export default {
name: '',
components: {
},
methods: {
}
}
</script>
结合vue指令简单使用
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/5de073e27319141648cdd22f82ef66ff.gif#pic_center)
- 在vue指令刚开始绑定到元素上时,给元素加上animated_bg样式,让它产生动画。自定义指令中添加txt修饰符,给默认文本一个高度。
- 点击按钮时,更新了cardInfo响应式数据的值,当子组件中有用到这个响应式数据时,v-animated_bg指令的componentUpdated方法就会触发,在这个钩子函数中移除值。
<style lang="scss" scoped>
.card-wrapper {
width: 100%;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
}
.img {
width: 100%;
height: 100%;
object-fit: cover;
}
.card {
width: 260px;
border-radius: 6px;
overflow: hidden;
background-color: #fff;
box-shadow: 0 2px 5px 0 rgba(0,0,0,0.05);
.card-header {
height: 170px;
}
}
// 卡片头部图片
.card-header img {
width: 100%;
}
// 卡片内容
.card-content {
padding: 20px;
.card-title {
font-weight: bold;
}
.card-bio {
margin: 5px 0 10px;
font-size: 14px;
color: #7c7c7c;
}
.author {
display: flex;
.author-avatar {
width: 42px;
height: 42px;
border-radius: 50%;
overflow: hidden;
flex-shrink: 0;
}
.author-intro {
padding-left: 8px;
flex: 1;
overflow: hidden;
display: flex;
flex-direction: column;
justify-content: space-around;
.author-name {
font-size: 16px;
text-decoration: none;
color: #000;
text-overflow: ellipsis;
white-space: nowrap;
overflow: hidden;
}
.author-date {
font-size: 13px;
color: #7c7c7c;
}
}
}
}
.animated_txt {
height: 20px;
border-radius: 8px;
}
.animated_bg {
background-image: linear-gradient(to right,
#f6f7f8 0,
#d0d1d2 25%,
#f6f7f8 50%,
#f6f7f8 100%
);
background-size: 200% 100%;
// background-position: 0% 0;
animation: grad 1s linear infinite;
}
@keyframes grad {
0% {
background-position: 0% 0;
}
100% {
background-position: -200% 0;
}
}
</style>
<template>
<div class="card-wrapper">
<button @click="getData">button</button>
<div class="card">
<div class="card-header" v-animated-bg.img>
<img v-if="cardInfo.bgUrl" class="img" :src="cardInfo.bgUrl" alt="">
</div>
<div class="card-content">
<div class="card-title" v-animated-bg.txt>
{{ cardInfo.title }}
</div>
<p class="card-bio" v-animated-bg.txt>
{{ cardInfo.bio }}
</p>
<div class="author">
<a href="#" class="author-avatar" v-animated-bg.img>
<img v-if="cardInfo.avatar" class="img" :src="cardInfo.avatar" alt="">
</a>
<div class="author-intro">
<a href="#" class="author-name" v-animated-bg.txt>
{{ cardInfo.authorName }}
</a>
<div class="author-date" v-animated-bg.txt>
{{ cardInfo.date }}
</div>
</div>
</div>
</div>
</div>
</div>
</template>
<script>
export default {
name: '',
components: {
},
data() {
return {
a:'',
cardInfo: {
}
}
},
methods: {
getData() {
this.cardInfo = {
bgUrl: 'http://119.23.61.24/api/static/img/article/90876672-26ec54f7c7ea7d27b3079ec685effdf2.jpg',
title: 'PSCOOL的签名',
bio: ' 热爱技术, 一点点的学习',
avatar: 'http://119.23.61.24/api/static/img/avatar/avatar.png',
authorName: 'PSCOOL',
date: '2023-05-21'
}
}
},
directives: {
'animated-bg': {
bind(el, binding, vnode) {
console.log(el,'el-bind');
el.classList.add('animated_bg')
if(binding.modifiers.txt) {
el.classList.add('animated_txt')
}
},
componentUpdated(el, binding) {
console.log(el,'el-componentUpdated');
el.classList.remove('animated_bg')
el.classList.remove('animated_text')
}
}
}
}
</script>