我有一个简单的问题,关于样式表的随机化,JS或Jquery的最佳实践方法是什么。
我计划处理5到6个不同的stylesheet.css文档,只处理颜色元素(背景,href,颜色)。我试图实现的是随机化每个用户访问加载哪个样式表文件(例如红色,绿色,黑色,白色)。
是否有用于此目的的JS库或已经使用的常用实践?
用这个
const stylesheets = ["main1.css","main2.css","main3.css","main4.css"];
let random = Math.floor(Math.random()*4);
document.head.appendChild('<link rel="stylesheet" href="' + stylesheets[random] + '">');
你可以试试像this这样的东西
HTML
<body>
<div width="100px" height="100px" class = "innerview">
</div>
</body>
JQuery的
$(document).ready(function() {
const colors = ["red","yellow","green","blue"];
var index = Math.floor(Math.random()*4);
$("body").removeClass()
$("body").addClass(colors[index])
})
CSS
.innerview {
height: 100px;
width: 100px;
}
.red .innerview {
background-color: red;
}
.yellow .innerview {
background-color: yellow;
}
.green .innerview {
background-color: green;
}
.blue .innerview {
background-color: blue;
}
加载不同的CSS将是一个矫枉过正的问题我想