24.骨架屏

骨架屏

html部分

css部分

*{
    margin: 0;
    padding: 0;
}
body{
    background-color: #ecf0f1;
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100vh;
    overflow: hidden;
}
.card{
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
    border-radius: 10px;
    overflow: hidden;
    width: 350px;
}
.card-header{
    height: 200px;
}
.card-header img{
    width: 100%;
    height: 100%;
    object-fit: cover;
}
.card-content{
    background-color: #fff;
    padding: 30px;
}
.card-title{
    height: 20px;
}
.card-text{
    color: #777;
    margin: 10px 0 20px;
}
.author{
    margin: 20px 0;
}
.user-img{
    height: 40px;
    width: 40px;
    border-radius: 50%;
}
.user-img > img{
    height: 100%;
    width: 100%;
    border-radius: 50%;
    object-fit: cover;
}
.animate-bg{
    background-image: linear-gradient(
        to right ,
        #f7f6f8 0%,
        #edeef1 10%,
        #f6f7f8 20%,
        #f6f7f8 100%
    );
    background-size: 200% 100%;
    animation: bg 1s linear infinite;
}

@keyframes  bg {
    0%{
        background-position: 50% 0;
    }
    100%{
        background-position: -150% 0;
    }
}

js部分

// 获取dom
const card_header=document.querySelector('.card-header')
const card_title=document.querySelector('.card-title')
const user_img=document.querySelector('.user-img')


// 2s后加载数据移除动画
setTimeout(init_data,2000)


function init_data(){
	// 创建dom
    const img=document.createElement("img")
    const uimg=document.createElement("img")
    img.setAttribute("src","./static/20180529205331_yhGyf.jpeg")
    uimg.setAttribute("src","./static/20190908084721_rjhtr.png")
    
    // 加载数据
    card_title.innerHTML="hello world"
    user_img.appendChild(uimg)
    card_header.appendChild(img)
	
	// 移除动画
    card_header.classList.remove("animate-bg")
    card_title.classList.remove("animate-bg")
    user_img.classList.remove("animate-bg")
}

效果

24.骨架屏_第1张图片

你可能感兴趣的:(前端开发50个案例,前端)