嘿所有,我有这个任务,我需要将Yahoo UI颜色选择器集成到一个网站。当有人选择颜色时,页面的背景需要变成那种颜色。
我按照http://developer.yahoo.com/yui/colorpicker/#events的说明操作,但我似乎无法弄清楚如何将backgroundcolor更改为拾取的颜色。我可以在有人选择颜色时更改背景,但是我不知道如何获取作为输入选择的颜色/(参见//注释的代码)直到现在我有这个:
头部:
<!-- Dependencies -->
<script src="http://yui.yahooapis.com/2.8.2r1/build/utilities/utilities.js" ></script>
<script src="http://yui.yahooapis.com/2.8.2r1/build/slider/slider-min.js" ></script>
<!-- Color Picker source files for CSS and JavaScript -->
<link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.8.2r1/build/colorpicker/assets/skins/sam/colorpicker.css">
<script src="http://yui.yahooapis.com/2.8.2r1/build/colorpicker/colorpicker-min.js" ></script>
然后在正文标记中:class="yui-skin-sam"
和身体:
<div id="container"></div>
<script type="text/javascript">
var picker = new YAHOO.widget.ColorPicker("container", {
showhsvcontrols: true,
showhexcontrols: true,
images: {
PICKER_THUMB: "picker_thumb.png",
HUE_THUMB: "hue_thumb.png"
}
});
//a listener for logging RGB color changes;
//this will only be visible if logger is enabled:
var onRgbChange = function(o) {
/*o is an object
{ newValue: (array of R, G, B values),
prevValue: (array of R, G, B values),
type: "rgbChange"
}
*/
// with this code i change the background when a color is picked, but not with the input col.
// document.body.style.backgroundColor = 'green';
YAHOO.log("The new color value is " + o.newValue, "info", "example");
}
//subscribe to the rgbChange event;
picker.on("rgbChange", onRgbChange);
</script>
答案 0 :(得分:0)
将背景颜色设置为RGB值的语法(o.newValue给出的内容)如下所示:
doc.style.backgroundColor="rgb(239,239,239)";
...所以,尝试这样的事情:
document.body.style.backgroundColor = "rgb(" + o.newValue[0] + "," + o.newValue[1] + "," + o.newValue[2] + ");"