Web Component Switches
How do I make an web component switches?
Encapsulated switches built with web components and shadow DOM. But I'm not so happy with the description at the buttons. Would like to wrap the text into , but it doesn't et displayed…. What is a web component switches? How do you make a web component switches? This script and codes were developed by Daniel Gooß on 10 November 2022, Thursday.
Web Component Switches - Script Codes HTML Codes
<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Web Component Switches</title> <meta name="viewport" content="width=device-width; initial-scale=1.0; user-scalable=no"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css"> <link rel='stylesheet prefetch' href='https://fonts.googleapis.com/css?family=Montserrat'> <style> /* NOTE: The styles were added inline because Prefixfree needs access to your styles and they must be inlined if they are on local disk! */ body { font-family: 'Montserrat', sans-serif;
}
.content { padding: 0 20px;
}
.list { padding: 0; list-style: none;
}
.list > li { padding: 10px 0; border-width: 0 0 1px; border-style: solid; border-color: #ccc;
}
.list > li:first-child { border-top-width: 1px;
}
.list > li switch-button { margin: 0 20px 0 0;
}
switch-button { display: inline-block; vertical-align: middle;
}
switch-button::shadow .switch { font-size: 0; line-height: 0; display: block; overflow: hidden; width: 70px; height: 30px; margin: -5px 0 0; padding: 5px 0 1px; cursor: pointer; user-select: none; perspective: 2000px; perspective-origin: 50% 50%; transform-style: preserve-3d;
}
switch-button::shadow .switch:before,
switch-button::shadow .switch:after { content: 'O'; font-size: 1rem; line-height: 1; display: inline-flex; width: 50%; height: 100%; transition: all .05s linear; transform: rotateX(20deg) rotateY(0deg); transform-origin: 100% 50%; color: #fff; background-color: #eee; box-shadow: 0 10px 0 #bbbbbb; justify-content: center; align-items: center;
}
switch-button::shadow .switch:after { content: 'I'; background-color: #e1e1e1; transform: rotateX(20deg) rotateY(-20deg); transform-origin: 0% 50%;
}
switch-button::shadow .state:checked ~ .switch::before { background-color: #b1d540; box-shadow: 0 10px 0 #86a524; transform: rotateX(20deg) rotateY(20deg);
}
switch-button::shadow .state:checked ~ .switch::after { background-color: #bada55; box-shadow: 0 10px 0 #86a524; transform: rotateX(20deg) rotateY(0deg);
} </style> <script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>
</head>
<body> <main class="content"> <h1>Devices</h1> <ul class="list"> <li> <switch-button data-name="on"></switch-button> TV </li> <li> <switch-button data-name="on"></switch-button> Radio </li> <li> <switch-button data-name="on"></switch-button> Coffee Machine </li> </ul>
</main>
<template class="js_switch-button-tpl"> <input type="checkbox" hidden class="state" value="" /> <label class="switch js_switch"> </label>
</template> <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script> <script src="js/index.js"></script>
</body>
</html>
Web Component Switches - Script Codes CSS Codes
body { font-family: 'Montserrat', sans-serif;
}
.content { padding: 0 20px;
}
.list { padding: 0; list-style: none;
}
.list > li { padding: 10px 0; border-width: 0 0 1px; border-style: solid; border-color: #ccc;
}
.list > li:first-child { border-top-width: 1px;
}
.list > li switch-button { margin: 0 20px 0 0;
}
switch-button { display: inline-block; vertical-align: middle;
}
switch-button::shadow .switch { font-size: 0; line-height: 0; display: block; overflow: hidden; width: 70px; height: 30px; margin: -5px 0 0; padding: 5px 0 1px; cursor: pointer; user-select: none; perspective: 2000px; perspective-origin: 50% 50%; transform-style: preserve-3d;
}
switch-button::shadow .switch:before,
switch-button::shadow .switch:after { content: 'O'; font-size: 1rem; line-height: 1; display: inline-flex; width: 50%; height: 100%; transition: all .05s linear; transform: rotateX(20deg) rotateY(0deg); transform-origin: 100% 50%; color: #fff; background-color: #eee; box-shadow: 0 10px 0 #bbbbbb; justify-content: center; align-items: center;
}
switch-button::shadow .switch:after { content: 'I'; background-color: #e1e1e1; transform: rotateX(20deg) rotateY(-20deg); transform-origin: 0% 50%;
}
switch-button::shadow .state:checked ~ .switch::before { background-color: #b1d540; box-shadow: 0 10px 0 #86a524; transform: rotateX(20deg) rotateY(20deg);
}
switch-button::shadow .state:checked ~ .switch::after { background-color: #bada55; box-shadow: 0 10px 0 #86a524; transform: rotateX(20deg) rotateY(0deg);
}
Web Component Switches - Script Codes JS Codes
// Create new prototype element
var SwitchButtonProto = Object.create(HTMLElement.prototype);
// Callback function when prototype element was created
SwitchButtonProto.createdCallback = function(){ // Create new shadow DOM element var shadow = this.createShadowRoot(); // Create DOM nodes for markup in shadow DOM var input = document.createElement('input'); input.type = 'checkbox'; input.hidden = 'true'; input.className = 'state'; input.name = this.dataset.name; input.id = 'switch' var label = document.createElement('label'); label.setAttribute('for', 'switch'); label.classList.add('switch','js_switch'); // Append nodes to wrapping element shadow.appendChild(input); // Append collected nodes to shadow element shadow.appendChild(label);
}
// register custom element for usage in DOM
var SwitchButton = document.registerElement('switch-button', { prototype: SwitchButtonProto
});
Developer | Daniel Gooß |
Username | daniel_gooss |
Uploaded | November 10, 2022 |
Rating | 3 |
Size | 4,335 Kb |
Views | 14,168 |
Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!
Name | Size |
Folded Video | 3,913 Kb |
Dashboard | 25,043 Kb |
Mobile Navigation Pattern | 5,939 Kb |
Revolver Slideshow | 5,445 Kb |
Apple menu redesign | 25,821 Kb |
Revolver Slideshow with Touch | 7,609 Kb |
CSS Paperfold Effect | 3,473 Kb |
CSSConf EU | 4,125 Kb |
Text Transition Effect | 232,607 Kb |
Base-Style | 2,614 Kb |
Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!
Name | Username | Size |
Long Shadow Button | Uixcrazy | 3,550 Kb |
Confirm Close Of Foundation Reveal Modal | Winghouchan | 1,818 Kb |
Reviews and Ratings Star | Zbnmstry | 1,591 Kb |
Animated Donut Chart | Jplhomer | 3,808 Kb |
Nice responsive team page | Infomiho | 3,139 Kb |
Loading animation with css | Icebob | 2,947 Kb |
Segments mouse following | Nosir | 2,909 Kb |
Table Exercise | Fresco | 9,585 Kb |
Resize image | Happyhj | 1,892 Kb |
Client-side Email Validation | Collizo4sky | 1,538 Kb |
Surf anonymously, prevent hackers from acquiring your IP address, send anonymous email, and encrypt your Internet connection. High speed, ultra secure, and easy to use. Instant setup. Hide Your IP Now!