Created
December 21, 2020 13:35
-
-
Save mrtrimble/1c2770bd4ecd8dd2ffe9521fff438ecb to your computer and use it in GitHub Desktop.
Simple way to make skeletons in CSS and JavaScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.skeleton { | |
position: relative; | |
box-sizing: border-box; | |
display: -webkit-box; | |
display: flex; | |
-webkit-box-orient: vertical; | |
-webkit-box-direction: normal; | |
flex-flow: column; | |
width: 100%; | |
} | |
.skeleton__line { | |
overflow: hidden; | |
position: relative; | |
box-sizing: border-box; | |
display: block; | |
height: 1em; | |
width: 100%; | |
background: #efefef; | |
border-radius: 3px; | |
} | |
.skeleton__line + .skeleton__line:last-child { | |
width: 50%; | |
} | |
.skeleton--sm { | |
width: 25%; | |
} | |
.skeleton--md { | |
width: 50%; | |
} | |
.skeleton--lg { | |
width: 75%; | |
} | |
.skeleton--full { | |
width: 100%; | |
} | |
.skeleton--animation { | |
position: abosolute; | |
display: -webkit-box; | |
display: flex; | |
height: 100%; | |
background: white; | |
background: -webkit-gradient(linear, left top, right top, from(rgba(255, 255, 255, 0)), color-stop(10%, rgba(255, 255, 255, 0)), color-stop(50%, rgba(255, 255, 255, 0.5)), color-stop(90%, rgba(255, 255, 255, 0)), to(rgba(255, 255, 255, 0))); | |
background: linear-gradient(90deg, rgba(255, 255, 255, 0) 0%, rgba(255, 255, 255, 0) 10%, rgba(255, 255, 255, 0.5) 50%, rgba(255, 255, 255, 0) 90%, rgba(255, 255, 255, 0) 100%); | |
-webkit-animation: shine 3s infinite cubic-bezier(0.55, 0.055, 0.675, 0.19); | |
animation: shine 3s infinite cubic-bezier(0.55, 0.055, 0.675, 0.19); | |
} | |
.skeleton:last-child { | |
margin-bottom: 0; | |
} | |
p .skeleton__line { | |
margin-bottom: 0.6em; | |
} | |
.skeleton-image { | |
overflow: hidden; | |
display: block; | |
background-color: #efefef; | |
background-image: url("data:image/svg+xml;base64,P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sMTM3LjZ6IE0xNjUuNiwyMjkuN0g1OC44di0xNWwxNi44LTIwLjNsOS4zLDkuN2MxLjgsMS45LDQuMywyLjksNi44LDIuOGMyLjYtMC4xLDUtMS4zLDYuNi0zLjNsMzgtNDYuM2wyOS4yLDMyLjFWMjI5Ljd6Ii8+PC9nPjwvc3ZnPg=="); | |
background-blend-mode: overlay; | |
background-repeat: no-repeat; | |
background-position: center; | |
background-size: 15px; | |
} | |
.skeleton-image--sm { | |
height: 50px; | |
width: 50px; | |
} | |
.skeleton-image--md { | |
height: 100px; | |
width: 100px; | |
} | |
.skeleton-image--lg { | |
height: 200px; | |
width: 200px; | |
} | |
.skeleton-image--xl { | |
height: 400px; | |
width: 400px; | |
} | |
.skeleton-image--full { | |
height: 100%; | |
width: 100%; | |
} | |
.skeleton-image--landscape { | |
width: 100% !important; | |
} | |
.skeleton-image--portrait { | |
height: 100% !important; | |
} | |
.skeleton-image--circle { | |
border-radius: 50%; | |
box-shadow: inset 0px 0px 1px #707070; | |
} | |
h1 .skeleton__line, | |
h2 .skeleton__line, | |
h3 .skeleton__line, | |
h4 .skeleton__line, | |
h5 .skeleton__line, | |
h6 .skeleton__line { | |
margin-bottom: 0.2em; | |
} | |
@-webkit-keyframes shine { | |
0% { | |
-webkit-transform: translateX(-100%); | |
transform: translateX(-100%); | |
} | |
30% { | |
-webkit-transform: translateX(100%); | |
transform: translateX(100%); | |
} | |
100% { | |
-webkit-transform: translateX(100%); | |
transform: translateX(100%); | |
} | |
} | |
@keyframes shine { | |
0% { | |
-webkit-transform: translateX(-100%); | |
transform: translateX(-100%); | |
} | |
30% { | |
-webkit-transform: translateX(100%); | |
transform: translateX(100%); | |
} | |
100% { | |
-webkit-transform: translateX(100%); | |
transform: translateX(100%); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const skeletons = document.querySelectorAll(".skeleton"); | |
if (skeletons) { | |
skeletons.forEach((skelly) => { | |
let lineCount = parseInt(skelly.getAttribute("data-lines")); | |
let animation = skelly.getAttribute("data-animation") === "true"; | |
if (!lineCount && skelly.innerHTML.trim().length == 0) { | |
lineCount = 1; | |
} | |
for (i = 0; i < lineCount; i++) { | |
const line = document.createElement("span"); | |
line.classList.add("skeleton__line"); | |
if (animation === true) { | |
const animationElement = document.createElement("span"); | |
animationElement.classList.add("skeleton--animation"); | |
line.appendChild(animationElement); | |
} | |
skelly.appendChild(line); | |
} | |
}); | |
} | |
const skeletonImages = document.querySelectorAll(".skeleton-image"); | |
if (skeletonImages) { | |
skeletonImages.forEach((skelly) => { | |
let animation = skelly.getAttribute("data-animation") === "true"; | |
if (animation === true) { | |
const animationElement = document.createElement("span"); | |
animationElement.classList.add("skeleton--animation"); | |
skelly.appendChild(animationElement); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment