Raj*_*was 1 html javascript css jquery
大家.我在HTML/CSS/Javascript中大约一周大的初学者.我为一个页面编写了这个代码,该页面用于在鼠标在页面上移动时根据"随机"RGB值(始终为alpha值固定为1)更改颜色.
<!DOCTYPE html>
<head>
<script src='https://code.jquery.com/jquery-1.10.2.js'></script>
</head>
<body>
<script>
$('*').mousemove(function() {
var red = Math.floor(Math.random() * 255);
var green = Math.floor(Math.random() * 255);
var blue = Math.floor(Math.random() * 255);
var rgba = 'rgba(' + red + ',' + blue + ',' + green + ')';
$('*').css('background', rgba);
});
</script>
</body>Run Code Online (Sandbox Code Playgroud)
我尝试过Chrome,Chromium,Vivaldi,Opera甚至是Internet Explorer,所有这些浏览器都显示了一个网页,其默认的白色背景页面对鼠标移动没有反应.
您的代码的主要问题是您正在设置RGBA颜色,但您忘记提供A值.
我也建议你使用document监听的mousemove事件和更新background-color的body.*应尽可能避免使用通配符选择器,因为它很慢 - 尤其是在附加事件处理程序时.
$(document).mousemove(function() {
var red = Math.floor(Math.random() * 255);
var green = Math.floor(Math.random() * 255);
var blue = Math.floor(Math.random() * 255);
var rgba = 'rgba(' + red + ',' + blue + ',' + green + ',1)'; // note '1' at the end
$('body').css('background', rgba);
});Run Code Online (Sandbox Code Playgroud)
<script src='https://code.jquery.com/jquery-1.10.2.js'></script>Run Code Online (Sandbox Code Playgroud)
这个片段应该有癫痫警告......
| 归档时间: |
|
| 查看次数: |
79 次 |
| 最近记录: |