js实现DIV背景色随机
来源:互联网 发布:excel数据分类 编辑:程序博客网 时间:2024/06/05 02:59
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>RandomColor</title> <style> *{ box-sizing: border-box; list-style: none; border: none; padding: 0; margin: 0; } p{ text-align: center; margin: 20px; } p a{ font-size: 20px; font-weight: 300; color: #e4393c; text-decoration: none; padding: 6px 10px; border: 1px solid currentColor; } .bg_color,.bg_two{ width: 100px; height: 100px; border: 1px solid #aa00aa; margin-left: 10px; } .main,.main ul{ overflow: hidden; } .main{ width: 440px; margin:30px auto; } .main ul li{ float: left; } </style> </head> <body> <div class="main"> <p><a href="javascript:;" id="btn-one">RandomColor-rgb</a></p> <ul> <li><div class="bg_color"></div></li> <li><div class="bg_color"></div></li> <li><div class="bg_color"></div></li> <li><div class="bg_color"></div></li> </ul> </div> <div class="main"> <p><a href="javascript:;" id="btn-two">RandomColor-#XXXXXX</a></p> <ul> <li><div class="bg_two"></div></li> <li><div class="bg_two"></div></li> <li><div class="bg_two"></div></li> <li><div class="bg_two"></div></li> </ul> </div> </body> <script> (function(){ //1、随机色的函数-rgb function getRandomColor(){ var rgb='rgb('+Math.floor(Math.random()*255)+','+Math.floor(Math.random()*255)+','+Math.floor(Math.random()*255)+')'; console.log(rgb); return rgb; } // 获取按钮 var btn_one=document.querySelector("#btn-one"); var Divs=document.querySelectorAll(".bg_color"); btn_one.onclick=function(){ for(var i=0;i<Divs.length;i++){ Divs[i].style.backgroundColor=getRandomColor(); } }; //2、随机颜色#XXXXXX var btn_two=document.querySelector("#btn-two"); var divsTwo=document.querySelectorAll(".bg_two"); var chars = ['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F']; function generateMixed(n) { var res = "#"; var id; for(var i = 0; i < n ; i ++) { id= Math.floor(Math.random()*16); res += chars[id]; } console.log(id,res); return res; } btn_two.onclick=function(){ for(var i=0;i<divsTwo.length;i++){ divsTwo[i].style.backgroundColor=generateMixed(6); } }; })() </script> </html>
演示地址:https://liuyanzhao.com/demo/randomBg/index.html
本文地址:https://liuyanzhao.com/3574.html
转载请注明
阅读全文
0 0
- js实现DIV背景色随机
- 随机填充div背景色
- JS:三个"点按钮出随机背景色的DIV"的程序
- swift 随机背景色的实现
- swift 随机背景色的实现
- JQ div随机显示背景色/取整函数/
- javascript如何设置DIV背景色为随机色
- javascript如何设置DIV背景色为随机色
- div背景色渐变
- div+css自适应背景色 div+css自适应背景色
- JS实现HTML页面背景色平滑转变
- JS背景色图片透明
- js 设置背景色更改
- js控件设置背景色
- 如何用原生js给特定元素随机添加背景色
- 【代码笔记】iOS-背景色随机显示
- jQuery实现点击DIV同时点击CheckBox,并为DIV上背景色
- 验证码实现(随机背景色及字体颜色,带扰乱线条)
- 详讲:vue2+vuex+axios
- HTML 列表
- 《PHP中将二维数组中相同的字段放在同一数组中》
- java22
- JavaScript基础(二)- JS在页面中的位置
- js实现DIV背景色随机
- java23
- Ansible Playbook 介绍
- c# 判断输入的号码是否是合法的手机号码
- android studio常见错误及异常处理记录
- java24
- 为工业机器人的学生分享一款ABB公司的机器人仿真软件
- 线程中CreateEvent和SetEvent及WaitForSingleObject的用法
- 注解(Annotation)自定义注解(四)--编译期注解解析讲解和手写ButterKnife