这次给大家带来jQuery获取随机颜色步骤详解,jQuery获取随机颜色的注意事项有哪些,下面就是实战案例,一起来看一下。
1.js
//获取十六进制颜色 function randomColor1(){ var r = Math.floor(Math.random()*256); var g = Math.floor(Math.random()*256); var b = Math.floor(Math.random()*256); if(r <p style="text-align: left;"><strong>2.html</strong></p><p style="text-align: left;"><code>.<h3>获取任意颜色</h3> </code></p><p style="text-align: left;">效果:</p><p style="text-align: left;"><img alt="" src="https://img.php.cn/upload/article/000/061/021/c28f3622f90c0e16835abe9347da377d-0.png"></p><p style="max-width:90%"><span style="color: #ff0000"><strong>下面在看一段代码关于js几种生成随机颜色方法</strong></span></p><pre class="brush:php;toolbar:false">nbsp;html> <meta> <title></title> <button>调用第一种</button> <button>调用第二种</button> <button>调用第三种</button> <script> var btn1=document.getElementById('btn1'); btn1.onclick=function(){ document.body.style.background=bg1() }; var btn2=document.getElementById('bnt2'); btn2.onclick=function(){ document.body.style.background=bg2(); }; var btn3=document.getElementById('btn3'); btn3.onclick=function(){ document.body.style.background=bg3(); }; function bg1(){ return '#'+Math.floor(Math.random()*256).toString(10); } function bg2(){ return '#'+Math.floor(Math.random()*0xffffff).toString(16); } function bg3(){ var r=Math.floor(Math.random()*256); var g=Math.floor(Math.random()*256); var b=Math.floor(Math.random()*256); return "rgb("+r+','+g+','+b+")";//所有方法的拼接都可以用ES6新特性`其他字符串{$变量名}`替换 } </script>
相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
推荐阅读:
以上就是jQuery获取随机颜色步骤详解的详细内容,更多请关注php中文网其它相关文章!
Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号