为什么这在 Firefox 中不起作用?
HTML
<body>
<p>Click on a color to begin!</p>
<canvas id="colorPicker" width="300" height="300"></canvas>
</body>
JavaScript
var elem = document.getElementById('colorPicker');
var context = elem.getContext('2d');
var color, hue = [[255, 0, 0 ],[255, 255, 0 ],[ 0, 255, 0 ],[ 0, 255, 255 ],[ 0, 0, 255 ],[255, 0, 255 ],[255, 0, 0],]
gradient = context.createLinearGradient(0, 0, elem.width, 0);
for (var i = 0; i <= 6; i++) {
color = 'rgb(' + hue[i][0] + ', ' + hue[i][1] + ', ' + hue[i][2] + ')';
gradient.addColorStop(i * 1/6, color);
}
gradient2 = context.createLinearGradient(0, 0, 0, elem.height);
gradient2.addColorStop(0, 'rgba(255, 255, 255, 1)');
gradient2.addColorStop(0.01, 'rgba(255, 255, 255, 1)');
gradient2.addColorStop(0.50, 'rgba(255, 255, 255, 0)');
gradient2.addColorStop(0.52, 'rgba(255, 255, 255, 0)');
gradient2.addColorStop(0.75, 'rgba(0, 0, 0, 1)');
gradient2.addColorStop(1, 'rgba(255, 255, 255, 1)');
context.fillStyle = gradient;
context.fillRect(0, 0, 500, 500);
context.fillStyle = gradient2;
context.fillRect(0, 0, 500, 500);
elem.addEventListener('click', function(e) {
var x = e.offsetX; // lame but close enough for now
var y = e.offsetY;
var data = context.getImageData(x, y, 1, 1).data;
// just paint it to the bottom right as an example for now:
var rgb = 'rgb(' + data[0] + ',' + data[1] + ',' + data[2] + ')';
$('#fluidWrap').css({backgroundColor: rgb});
$('body').css({backgroundColor: rgb});
$('.content').css({backgroundColor: rgb});
}, false);
它适用于除 Firefox 之外的所有浏览器。和它是rgb有关系吗?我看了一下,在 background-color
。
我该怎么做才能使这项工作成功?
HTML
<body>
<p>Click on a color to begin!</p>
<canvas id="colorPicker" width="300" height="300"></canvas>
</body>
JavaScript
var elem = document.getElementById('colorPicker');
var context = elem.getContext('2d');
var color, hue = [[255, 0, 0 ],[255, 255, 0 ],[ 0, 255, 0 ],[ 0, 255, 255 ],[ 0, 0, 255 ],[255, 0, 255 ],[255, 0, 0],]
gradient = context.createLinearGradient(0, 0, elem.width, 0);
for (var i = 0; i <= 6; i++) {
color = 'rgb(' + hue[i][0] + ', ' + hue[i][1] + ', ' + hue[i][2] + ')';
gradient.addColorStop(i * 1/6, color);
}
gradient2 = context.createLinearGradient(0, 0, 0, elem.height);
gradient2.addColorStop(0, 'rgba(255, 255, 255, 1)');
gradient2.addColorStop(0.01, 'rgba(255, 255, 255, 1)');
gradient2.addColorStop(0.50, 'rgba(255, 255, 255, 0)');
gradient2.addColorStop(0.52, 'rgba(255, 255, 255, 0)');
gradient2.addColorStop(0.75, 'rgba(0, 0, 0, 1)');
gradient2.addColorStop(1, 'rgba(255, 255, 255, 1)');
context.fillStyle = gradient;
context.fillRect(0, 0, 500, 500);
context.fillStyle = gradient2;
context.fillRect(0, 0, 500, 500);
elem.addEventListener('click', function(e) {
var x = e.offsetX; // lame but close enough for now
var y = e.offsetY;
var data = context.getImageData(x, y, 1, 1).data;
// just paint it to the bottom right as an example for now:
var rgb = 'rgb(' + data[0] + ',' + data[1] + ',' + data[2] + ')';
$('#fluidWrap').css({backgroundColor: rgb});
$('body').css({backgroundColor: rgb});
$('.content').css({backgroundColor: rgb});
}, false);
It works in every browser except firefox. Has it something to do with it being an rgb? I had a look and it is fine to use rgb in background-color
.
What can I do to make this work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您尝试在 eventHandler 上使用 Firebug 和断点,您将看到在 Firefox 中单击事件有:
但没有
e.offset
。更改它并适用于 Firefox。
If you try using Firebug and breakpoint on the eventHandler you'll see that in Firefox the click event has:
but not
e.offset
.Changed it and works for firefox.
它在 IE8 中也不起作用(对我来说)
已修复
http://jsfiddle.net/jdb1991/XR3QF/
It doesn't work in IE8 either (for me)
Fixed here
http://jsfiddle.net/jdb1991/XR3QF/