Как использовать скрипт randomcolor ?
Здравствуйте. Есть вот такой скрипт https://github.com/davidmerfield/randomColor . Приведите пожалуйста пример использования этого скрипта randomcolor. Я сижу голову ломаю. Как-то всё сложно сделали. https://jsfiddle.net/bLbhcno3/2/ .Что я должен прописать, чтобы определённый класс получил случайный цвет?
|
drkrol,
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script type="text/javascript" src="https://randomcolor.llllll.li/randomColor.js"></script> <style type="text/css"> .swatch { width: 3em; height: 3em; border-radius: 50%; display: block; float: left; } </style> <title></title> <script> window.onload=function(){ [].forEach.call( document.querySelectorAll('.swatch'), function(el) { el.style.backgroundColor = randomColor(); }); } </script> </head> <body> <span class="swatch" hue="pink" count="18">123</span> <span class="swatch" hue="pink" count="18">123</span> <span class="swatch" hue="pink" count="18">123</span> </body> </html> |
рони, спасибо. А как при такой структуре сделать, чтобы менялись цвета? Класс span - это id. Как сделать, чтобы цвет у busy менялся только тогда, когда id меняется?
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script type="text/javascript" src="https://randomcolor.llllll.li/randomColor.js"></script> <style type="text/css"> body { background:#F3F5F6 } .date { float: left; width: 45px; text-align: center; z-index: 1; position: relative; height: 40px; line-height: 40px; color: #d0d0d0; } .cont { padding: 40px; background: white; margin: 50px; width: 530px; height: 120px; } .busy { width: 100%; height: 100%; position: absolute; top: 0; z-index: -1; } </style> <title></title> <script> window.onload=function(){ [].forEach.call( document.querySelectorAll('.busy'), function(el) { el.style.backgroundColor = randomColor(); }); } </script> </head> <body> <div class="cont"> <div class="date">12</div> <div class="date">13<div class='busy'><span class='140'></span></div></div> <div class="date">14<div class='busy'><span class='140'></span></div></div> <div class="date">15<div class='busy'><span class='140'></span></div></div> <div class="date">16<div class='busy'><span class='141'></span></div></div> <div class="date">17</div> <div class="date">18<div class='busy'><span class='142'></span></div></div> <div class="date">19<div class='busy'><span class='142'></span></div></div> <div class="date">20<div class='busy'><span class='143'></span></div></div> <div class="date">21<div class='busy'><span class='143'></span></div></div> </div> </body> </html> Сейчас тут у каждой даты свой цвет, а нужно, чтобы у дат с 13 по 15 - один цвет; 16 - другой цвет ; с 18 по 19 - третий цвет и т.д. |
Цитата:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script type="text/javascript" src="https://randomcolor.llllll.li/randomColor.js"></script> <style type="text/css"> body { background:#F3F5F6 } .date { float: left; width: 45px; text-align: center; z-index: 1; position: relative; height: 40px; line-height: 40px; color: #d0d0d0; } .cont { padding: 40px; background: white; margin: 50px; width: 530px; height: 120px; } .busy { width: 100%; height: 100%; position: absolute; top: 0; z-index: -1; } </style> <title></title> <script> window.onload=function(){ var color = {}; [].forEach.call( document.querySelectorAll('.busy'), function(el) { var id = el.querySelector('span').className, rgb = color[id] || (color[id]=randomColor()); el.style.backgroundColor = rgb; }); } </script> </head> <body> <div class="cont"> <div class="date">12</div> <div class="date">13<div class='busy'><span class='140'></span></div></div> <div class="date">14<div class='busy'><span class='140'></span></div></div> <div class="date">15<div class='busy'><span class='140'></span></div></div> <div class="date">16<div class='busy'><span class='141'></span></div></div> <div class="date">17</div> <div class="date">18<div class='busy'><span class='142'></span></div></div> <div class="date">19<div class='busy'><span class='142'></span></div></div> <div class="date">20<div class='busy'><span class='143'></span></div></div> <div class="date">21<div class='busy'><span class='143'></span></div></div> </div> </body> </html> |
Часовой пояс GMT +3, время: 07:20. |