Code Snippets
Get class name from element
Get class name from element
A nice little script that will return the first class applied to an element. It's part of our internal slider code, but has lots of uses when other js libraries are involved.
<script>
function getFirstWord(element) {
// Get the class name of the element
let str = element.className;
// Trim any leading or trailing spaces from the string
str = str.trim();
// Find the first space in the string
const spaceIndex = str.indexOf(' ');
if (spaceIndex === -1) {
// If there are no spaces in the string, return the whole string
return str;
}
// If there is a space in the string, return the substring before the space
return str.substring(0, spaceIndex);
}
</script>
Similar Code Snippets
Fade non-hovered links
A fun animation where hovering over links will fade out all the items except the hovered item.
Fix side scrolling on all devices
The .page-wrapper is a common class used to wrap the entire content of a page. Sometimes, you might encounter elements that overflow the boundaries of your page, causing horizontal scrollbars or layout shifts. To address this, you can use the overflow: clip; property to prevent any content from overflowing the boundaries of the .page-wrapper:
Calculate sizes in CSS
CSS's calc() function allows you to perform calculations to determine CSS property values. It's incredibly useful for responsive design, such as when you need to adjust sizes dynamically. For example, if you want a div to be 100% of the viewport width minus a certain margin, you can use calc():