var canvas = document.createElement('canvas'),ctx = canvas.getContext('2d'),checked = 0; // The state of the checkboxcanvas.width = canvas.height = 15; // Set the width and height of the canvasdocument.body.appendChild(canvas);document.body.appendChild(document.createTextNode(' Togglable Option'));
接下来,设计一种让画布自己更新的方法。
(function loop(){// Draws a borderctx.fillStyle = '#ccc';ctx.fillRect(0,0,15,15);ctx.fillStyle = '#fff';ctx.fillRect(1, 1, 13, 13);// Fills in canvas if checkedif(checked){ctx.fillStyle = '#000';ctx.fillRect(2, 2, 11, 11);}setTimeout(loop, 1000/10); // Refresh 10 times per second})();
span.bigcheck-target {font-family: FontAwesome; /* Use an icon font for the checkbox */}
input[type='checkbox'].bigcheck {position: relative;left: -999em; /* Hide the real checkbox */}
input[type='checkbox'].bigcheck + span.bigcheck-target:after {content: "\f096"; /* In fontawesome, is an open square (fa-square-o) */}
input[type='checkbox'].bigcheck:checked + span.bigcheck-target:after {content: "\f046"; /* fontawesome checked box (fa-check-square-o) */}
/* ==== Optional - colors and padding to make it look nice === */body {background-color: #2C3E50;color: #D35400;font-family: sans-serif;font-weight: 500;font-size: 4em; /* Set this to whatever size you want */}
span.bigcheck {display: block;padding: 0.5em;}
Unfortunately browser support is quite bad for the appearance option. From my personal testing I only got Opera and Chrome working correctly. But this would be the way to go to keep it simple when better support comes or you only want to use Chrome/Opera.
<style>.custom-checkbox {position: relative;}.custom-checkbox input{position: absolute;left: 0;top: 0;height:15px;width: 50px; /* Expand the checkbox so that it covers */z-index : 1; /* the label and span, increase z-index to bring it over */opacity: 0; /* the label and set opacity to 0 to hide it. */}.custom-checkbox input+label {position: relative;left: 0;top: 0;padding-left: 25px;color: black;}.custom-checkbox input+label span {position: absolute; /* a small box to display as checkbox */left: 0;top: 0;height: 15px;width: 15px;border-radius: 2px;border: 1px solid black;background-color: white;}.custom-checkbox input:checked+label { /* change label color when checked */color: orange;}.custom-checkbox input:checked+label span{ /* change span box color when checked */background-color: orange;border: 1px solid orange;}</style>
input[type="checkbox"] {position: absolute;opacity: 0;z-index: -1;}
/* Text color for the label */input[type="checkbox"]+span {cursor: pointer;font: 16px sans-serif;color: black;}
/* Checkbox un-checked style */input[type="checkbox"]+span:before {content: '';border: 1px solid grey;border-radius: 3px;display: inline-block;width: 16px;height: 16px;margin-right: 0.5em;margin-top: 0.5em;vertical-align: -2px;}
/* Checked checkbox style (in this case the background is green #e7ffba, change this to change the color) */input[type="checkbox"]:checked+span:before {/* NOTE: Replace the url with a path to an SVG of a checkmark to get a checkmark icon */background-image: url('https://cdnjs.cloudflare.com/ajax/libs/ionicons/4.5.6/collection/build/ionicons/svg/ios-checkmark.svg');background-repeat: no-repeat;background-position: center;/* The size of the checkmark icon, you may/may not need this */background-size: 25px;border-radius: 2px;background-color: #e7ffba;color: white;}
/* Adding a dotted border around the active tabbed-into checkbox */input[type="checkbox"]:focus+span:before,input[type="checkbox"]:not(:disabled)+span:hover:before {/* Visible in the full-color space */box-shadow: 0px 0px 0px 2px rgba(0, 150, 255, 1);
/* Visible in Windows high-contrast themesbox-shadow will be hidden in these modes andtransparency will not be hidden in high-contrastthus box-shadow will not show but the outline willproviding accessibility */outline-color: transparent; /*switch to transparent*/outline-width: 2px;outline-style: dotted;}
/* Disabled checkbox styles */input[type="checkbox"]:disabled+span {cursor: default;color: black;opacity: 0.5;}
/* Styles specific to this fiddle that you do not need */body {padding: 1em;}h1 {font-size: 18px;}
<h1>NOTE: Replace the url for the background-image in CSS with a path to an SVG in your solution or CDN. This one was found from a quick google search for a checkmark icon cdn</h1>
<p>You can easily change the background color, checkbox symbol, border-radius, etc.</p>
<label><input type="checkbox"><span>Try using tab and space</span></label>
<br>
<label><input type="checkbox" checked disabled><span>Disabled Checked Checkbox</span></label>
<br>
<label><input type="checkbox" disabled><span>Disabled Checkbox</span></label><br>
<label><input type="checkbox"><span>Normal Checkbox</span></label>
<br>
<label><input type="checkbox"><span>Another Normal Checkbox</span></label>
Simply set whatever CSS color (e.g. named value, hex code, etc.) you want in as the value of accent-color, and it will be applied.
This currently works in Chrome (v93+), Edge (v93+), Firefox (v92+), Opera (v79+), and Safari (v15.4+).
Note: Edge, Chrome, and Opera (and possibly Safari; I can't test that) currently don't support alpha channel values via rgba() either (the RGB values of rgba() will still "work"; the alpha channel will simply be ignored by the browser). See MDN Browser Support for more information.