0

大家好,我有这个任务,我需要将 Yahoo UI 颜色选择器集成到网站中。当有人选择一种颜色时,页面的背景需要变成那种颜色。

我按照这里的说明http://developer.yahoo.com/yui/colorpicker/#events但我似乎无法弄清楚如何将背景颜色更改为选择的颜色。当有人选择一种颜色时,我可以更改背景,但是我不知道如何获取被选择为输入/的颜色(参见 // 注释的代码),直到现在我有这个:

在头部:

<!-- 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>
4

1 回答 1

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] + ");"  
于 2011-01-22T02:42:19.763 回答