jquery调色板_使用jQuery的调色板生成器

怼烎@ 2023-02-22 01:25 85阅读 0赞

jquery调色板

As I continue to learn jQuery, I think it’s important that I begin by porting over scripts I’ve created using MooTools. One of those scripts is my Color Palette Generator script, which debuted on Eric Wendelin’s blog. For those of you that missed it, my script analyzes all of the colors on the page (minus images) and builds a palette of colors. Here it is in some jQuery goodness.

随着我继续学习jQuery,我认为从移植使用MooTools创建的脚本开始很重要。 这些脚本之一是我的调色板生成器脚本,该脚本在Eric Wendelin的博客上首次亮相。 对于那些想念它的人,我的脚本会分析页面上的所有颜色(减去图像)并构建一个调色板。 这就是jQuery的优点。

View Demo 观看演示

XHTML (The XHTML)

  1. <input type="button" id="get-colors" value="Get Colors" class="button" />
  2. <div id="colors-wrapper"></div>

All we need to begin with is the button that triggers the palette generation and a DIV container that will hold all of the DIVs my jQuery creates.

我们所需要做的只是触发调色板生成的按钮和一个DIV容器,该容器将保存我的jQuery创建的所有DIV。

CSS (The CSS)

  1. .dcolor { height:40px; }
  2. .dtext { }
  3. .dwrapper { width:200px; float:left; padding:10px; margin:0 20px 20px 0; border:1px solid #ccc; }

These CSS classes act as containers for the text DIV I generate and the color-displaying DIV I generate. Those two DIVs are held in one wrapping DIV.

这些CSS类充当我生成的文本DIV和我生成的颜色显示DIV的容器。 这两个DIV被保存在一个包装的DIV中。

jQuery JavaScript (The jQuery JavaScript)

  1. /* when the dom is ready */
  2. $(document).ready(function() {
  3. $('#get-colors').click(function() {
  4. //my colors array
  5. var colors = new Array();
  6. //get all elements
  7. $('*').each(function() {
  8. if($(this).css('background-color') && $(this).css('background-color') != 'transparent') { colors.push($(this).css('background-color')); }
  9. if($(this).css('color')) { colors.push($(this).css('color')); }
  10. if($(this).css('border-color')) { colors.push($(this).css('border-color')); }
  11. });
  12. //remove dupes and sort
  13. colors.sort();
  14. //create a color block for all of them
  15. jQuery.each(colors,function(it,value) {
  16. if(!$('div[rel=\'' + value + '\']').length)
  17. {
  18. //inject the wrapper
  19. var wrapper_id = 'w' + it;
  20. $('<div class="dwrapper" id="' + wrapper_id + '" rel="' + value + '"> </div>').appendTo('#colors-wrapper');
  21. //inject the color div
  22. $('<div class="dcolor" style="background-color:' + value + '"> </div>').appendTo('#' + wrapper_id);
  23. //inject text div
  24. $('<div class="text">' + value + '</div>').appendTo('#' + wrapper_id);
  25. }
  26. });
  27. });
  28. });

When someone clicks the “Get Colors” button, I grab every element in the DOM and collect its color, background-color, and border-color. Once I’ve cycled through all of the elements, cycle through all of the colors and display them as DIVs inside my colors-wrapper element. You’ll note that I utilized the rel attribute to prevent duplicates.

当有人单击“获取颜色”按钮时,我抓取DOM中的每个元素并收集其颜色,背景色和边框色。 遍历所有元素后,遍历所有颜色并将它们显示为我的color -wrapper元素内的DIV。 您会注意到,我利用rel属性来防止重复。

View Demo 观看演示

There’s the jQuery Color Palette Generator for you!

有适合您的jQuery调色板生成器!

翻译自: https://davidwalsh.name/color-palette-generator-jquery

jquery调色板

发表评论

表情:
评论列表 (有 0 条评论,85人围观)

还没有评论,来说两句吧...

相关阅读

    相关 R语言调色板——colors()

    R语言中画图避免不了使用颜色,在R语言中也有很多颜色得包和函数,比如彩虹色、`rgb`、`colors`还有不同色系得调色板等等。今天我们使用`colors`来展示其中得`65