Home > Web Front-end > JS Tutorial > body text

jquery implements color picking function

小云云
Release: 2018-01-23 09:39:55
Original
1396 people have browsed it

We know that ps has a color disappearing function. This article mainly introduces examples of jquery imitating the color picking function of ps, which has a good reference value. Let's take a look with the editor below, I hope it can help everyone.

1. Effect display

2.html code: index.html





 
 Title
 

Copy after login

3. Plug-in code:


##

(function ($) {
 $.fn.pickerColor=function (option) {
  var opt ={
  ck:function () {}
  },_this=this
  opt=$.extend(opt,option);
  _this.on('click',function (e) {
  var canvasObj = '';
  $('body').append(canvasObj);
  var cvs = document.getElementById("canvasPickerColor"),ctx =cvs.getContext('2d')
  cvs.height=1;cvs.width=1
  var img = new Image();
  img.src=_this.attr('src');
  var osX=e.offsetX,osY=e.offsetY
  ctx.drawImage(img,osX,osY,1,1,0,0,1,1);
  var imgData=ctx.getImageData(0,0,1,1);
  console.log(imgData)
  if(opt.ck) opt.ck(imgData.data[0]+','+imgData.data[1]+','+imgData.data[2]);
  })
 }
 })(jQuery)
Copy after login

3, plug-in call


$(function () {
 $('.source').pickerColor({
  ck:function (data) {
  console.log(data)
  $('.color').css('background','rgba('+data+',1)')
  }
 })
 })
Copy after login
Related recommendations:

Instance of javascript calculating gradient color

javascript font color Development of controls

Implementation method of JavaScript generating random colors

The above is the detailed content of jquery implements color picking function. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!