Jquery - 帮助创建最简单的颜色选择器 [英] Jquery - help to create the simplest color picker ever

查看:105
本文介绍了Jquery - 帮助创建最简单的颜色选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Stackoverflow是真棒!我刚刚发布了一个问题,它是社区在几分钟内回答,并得到一个工作答案!



我有另一个问题关于自定义颜色选择器。我知道有很多颜色选择器,但它是太复杂的使用。我需要一个更简单的一个。



所以我决定创建一个简单的4x4托盘的预定义颜色。

 < div class = supercolor> 
< div class =colordivid =#111111style =background-color:#111111;& nbsp;< / div>
< div class =colordivid =#222222style =background-color:#222222; & nbsp;< / div>
< div class =colordivid =#333333style =background-color:#333333;>& nbsp;< / div>
< / div>

在我的脚本部分,我手动添加点击功能到每个和个人ID动态创建输入选项复选框选中html)和刻度字符,以便用户知道选择了颜色。如果已经检查了除法,它将删除(输入选项复选框选中html)和刻度字符。



我使用数组来确定是否选中div ,如果是,则更新该数组索引。输入复选框是创建的,所以当页面提交时,我有一个方法来知道选择什么颜色的复选框值是十六进制的背景颜色。

  var selected_arry = []; 

$('#111111')。click(function(){
if(selected_arry == 1){
selected_arry [0] = 0;
$ ('#111111')。html();
} else {
selected_arry = 1;
$('#111111')。html(&#10003; input type ='checkbox'name ='selected_color []'hidden checked id ='#111111'/>);
}
}

- >对于接下来的15个div,重复相同的代码,但使用不同的ID

我的问题是,我必须重复这一切,我创建的所有部门,它看起来非常不优化,我认为必须有一个更好的方法来做。我只有几个月的Jquery曝光和一个新手开发商。



非常感谢!

p> 修改:工作代码



最后,在@egis& @Rob Cowie,代码完成具有非常高效和可扩展的功能。注意:我删除了一些部分(让我更容易理解初学者),并编辑一些部分以允许多个选择。



CSS代码:

 < style type =text / css> 
.colour {
width:40px;
height:40px;
float:left;
cursor:pointer;
line-height:40px;
text-align:center;
}
.red {background-color:#FF2622;}
.green {background-color:#2B6DB9;}
.blue {background-color:#4AFF41;}
< / style>

HTML代码:

 < div id =color-picker> 
< div class =color red>& nbsp;< / div>
< div class =color green>& nbsp;< / div>
< div class =color blue>& nbsp;< / div>
< / div>

脚本代码:

  $(document).ready(function(){
$('。color')。click(function(event){
var $ this = $(this);
$ this.toggleClass('selected');
if($ this.hasClass('selected')){
var color = $ this.css('backgroundColor');
$ this.html(&#10003;< input type ='checkbox'name ='selected_color []'hidden checked id ='+ color +'value ='+ color +'/> ;
} else {
$ this.html('');
};
});
});


解决方案

我创建了一个解决方案,你后。有关代码,请参见 https://gist.github.com/962872



总之,点击颜色div切换类'selected'并设置隐藏表单输入的值(将提交颜色)。



如果当前选中了点击的div,则会删除该类,并将隐藏输入的值设置为空。



通过添加到点击处理程序。


Stackoverflow is awesome! I just posted a question and it was answered by the community within minutes and a working answer was obtained!

I have another question about a custom color picker. I know there are many color picker out there but it is just too complicated to use. I need a simpler one.

So I decided to create a simple 4x4 pallet of predefined color. The pallets are formed using div with different background color under a div-class named 'supercolor'.

<div class="supercolor" >                       
  <div class="colordiv" id="#111111" style="background-color:#111111;">&nbsp;</div>
  <div class="colordiv" id="#222222" style="background-color:#222222;" >&nbsp;</div>                
  <div class="colordiv" id="#333333" style="background-color:#333333;">&nbsp;</div>
</div>

In my script section, I manually add on click function to each and individual id to dynamically create an (input option check box checked html) and a tick character on the division so the user knows that color is selected. If the division is already checked, it will removed the (input option check box checked html) and the tick character.

I used an array to determined if the div is checked, and if it is, update that array index. The input check box are created so when the page submits, I have a way to know which color was selected based on the check box value which is the background color in hex.

var selected_arry = [];

$('#111111').click(function(){
  if (selected_arry == 1){
    selected_arry[0] = 0;
    $('#111111').html("");
  } else {
    selected_arry = 1;
    $('#111111').html("&#10003;<input type='checkbox' name='selected_color[]' hidden checked id='#111111' />");
  }
});

->repeat same code for next 15 divs but with different ID

My question is, I have to repeat this for all the division I create and it does look very unoptimized and I think there must be a better way to do it. I only have a few months of Jquery exposure and a newbie developer. I am hoping all the gurus out there can point me to a more efficient way.

Thank you very much!

Edit : Working Code

Finally with the help from @egis & @Rob Cowie, the code is completed with very efficient and scalable function. Note: I removed some part (making it simpler for beginner like me to understand) and edited some part to allow multiple selection.

CSS Code:

<style type="text/css">
   .colour {
   width: 40px;
   height: 40px;
   float: left;
   cursor: pointer;
   line-height: 40px;
   text-align: center;
   }
   .red {background-color: #FF2622;}
   .green {background-color: #2B6DB9;}
   .blue {background-color: #4AFF41;}
</style>

HTML Code:

<div id="colour-picker">
   <div class="colour red">&nbsp;</div>
   <div class="colour green">&nbsp;</div>
   <div class="colour blue">&nbsp;</div>
</div>

Script Code:

$(document).ready(function() {
   $('.colour').click(function(event){
      var $this = $(this);
   $this.toggleClass('selected');
       if ($this.hasClass('selected')){
          var colour = $this.css('backgroundColor');
      $this.html("&#10003;<input type='checkbox' name='selected_color[]' hidden checked id='"+colour+"' value='"+colour+"' />");
       } else {
          $this.html('');
       };
   });
});

解决方案

I've created a solution that delivers what I think you're after. See https://gist.github.com/962872 for the code.

In summary, clicking on colour divs toggles the class 'selected' and sets the value of a hidden form input (which will submit the colour).

If the clicked div is currently selected, the class is removed, and the value of the hidden input is set to empty.

You can do more on click simply by adding to the click handler.

这篇关于Jquery - 帮助创建最简单的颜色选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆