JS随机网页背景色(如何让用户自闭)

如果有关注我博客的朋友应该发现了,我博客的左上角多出来了一个“彩蛋”按钮,如果你点击了的话,就会有一种神奇的光芒在你眼前绽放

咳咳..这是我在做一个点击替换背景图片的功能的时候弄出来的副产品,解决了“如何快速让你网站的用户自闭”这一问题

JS的内容还是很简单的,本文章主要是给小白们看的:

<a id="starteggs" style="width:100px;height:100px;font-size:100px;color:#000;" onclick="setInterval(starteggs,1);" >点我</a> <!--setInterval(starteggs,1);的意思是每1毫秒执行一次starteggs这个函数-->
<a id="aa" style="width:100px;height:100px;font-size:100px;color:#FFF;" >AAAAA</a>
<a id="bb" style="width:100px;height:100px;font-size:100px;background:#000;" >BBBBB</a>
<script>
    function starteggs(){
    console.log(randomColor())                 //浏览器调试log输出
	color = randomColor();                     //赋值随机颜色代码
	document.getElementById("starteggs").style.background = color;    //改变ID为starteggs的元素的background属性
    document.getElementById("aa").style.background = color;        //改变ID为aa的元素的background属性
	document.getElementById("bb").style.color = color;       //改变ID为bb的元素的color属性
    }
	function randomRange(min, max) {                
        return Math.floor(Math.random() * (max - min + 1) + min);
    };
	function randomColor() {                     //随机输出十六位颜色代码
        var color = randomRange(0, 0xFFFFFF);
        return '#' + ('000000' + color.toString(16)).slice(-6);
    };
</script>
效果预览

内容还是非常简单的,只不过你在照搬之前请把你需要变换颜色的元素的ID加上。而且需要注意的是,一旦有一个元素ID在页面上找不到,那么之后的document都不会再执行

郑重声明,本人最讨厌某些x彩,波x,x挂,x刷,网站的此类故意和用户过不去的杀马特广告,本JS仅供学习

文章来源:

Author:沧水
link:https://cangshui.net/?p=4143