javascript編程開(kāi)發(fā)中取色器及封裝$函數(shù)用法示例
本文實(shí)例講述了javascript編程開(kāi)發(fā)中取色器及封裝$函數(shù)用法。分享給大家供大家參考,具體如下:
1.封裝$函數(shù)
function $(str){
//如果傳入的是'#' 則選擇id標(biāo)簽
//如果傳入的是'.' 則選擇所有的類(lèi)名標(biāo)簽
//如果傳入的既不是'#也不是'.' 選擇復(fù)合標(biāo)簽
//判斷傳入的值
if(typeof str !='string'){
console.log('傳入的參數(shù)有誤!');
return null;
}
//獲取參數(shù)的第一個(gè)字母
var firstChar=str.charAt(0);
var name=str.substr(1);
switch(firstChar){
case '#':
console.log('id選擇器');
return document.getElementById(name);
break;
case '.':
//類(lèi)選擇器:getElementsByClassName是在js中新加入的,對(duì)于老的ie6,ie7無(wú)法兼容
console.log('類(lèi)選擇器');
//兼容性
//1.判斷是否能夠使用getElementsByClassName
if(!document.getElementsByClassName){
return document.getElementsByClassName(name);
}else{
//2.獲取所有的標(biāo)簽
//3.逐個(gè)判斷是否帶有該類(lèi)名
//4.如果有則添加到數(shù)組中
var resultElements=[];
var elements=document.getElementsByTagName('*');
// console.log(element);
for(var i=0;i<elements.length;i++){
var element=elements[i];
var class_name=element.className.split(' ');
for(var j=0;j<class_name.length;j++){
if(class_name[j]==name){
//加入到數(shù)組中去
resultElements.push(elements[i]);
console.log(resultElements);
}
}
}
return resultElements;
}
break;
default:
console.log('標(biāo)簽選擇器');
name=str.substr(0);
return document.getElementsByTagName(name);
}
}
2.取色器
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="js/select.js"></script>
<style>
*{
margin: 0;
padding: 0;
}
#box{
width: 300px;
height: 300px;
background:rgb(0,0,0);
margin:20px;
}
#btn{
padding:0 20px;
}
input{
width: 100px;
}
</style>
<script>
//javascript的style屬性只能獲取內(nèi)聯(lián)樣式,對(duì)于外部樣式和嵌入式樣式需要用currentStyle屬性。但是,currentStyle在FIrefox和Chrome下不支持,需要使用如下兼容性代碼
HTMLElement.prototype.__defineGetter__("currentStyle", function () {
return this.ownerDocument.defaultView.getComputedStyle(this, null);
});
function bgColor(obj){
return obj.currentStyle.backgroundColor;
}
function compare(color1,color2){
if(color1>color2){
return -1;
}else if(color1<color2){
return 1;
}else{
return 0;
}
}
window.onload=function(){
var red;
var green;
var blue;
var boxColor=[];
$('#btn').onclick=function(){
red=Number($('#red').value);
green=Number($('#green').value);
blue=Number($('#blue').value);
boxColor=bgColor($('#box')).split('(');
boxColor=boxColor[1].split(')');
boxColor=boxColor[0].split(',');
if(red>255||red<0 || green>255||green<0 || blue>255||blue<0){
alert("顏色值出錯(cuò)了!");
return;
}
//變色定時(shí)器
var timer=setInterval(function(){
boxColor[0]=Number(boxColor[0])+compare(boxColor[0],red);
boxColor[1]=Number(boxColor[1])+compare(boxColor[1],green);
boxColor[2]=Number(boxColor[2])+compare(boxColor[2],blue);
$('#box').style.background='rgb('+boxColor[0]+','+boxColor[1]+','+boxColor[2]+')';
if(boxColor[0]==red&&boxColor[1]==green&&boxColor[2]==blue){
clearInterval(timer);
}
},10);
}
}
</script>
</head>
<body>
<div id="box"></div>
red:<input type="text" id="red">
green:<input type="text" id="green">
blue:<input type="text" id="blue">
<button id="btn">取色</button>
</body>
</html>
點(diǎn)擊此處查看在線(xiàn)演示效果。
PS:這里再為大家推薦幾款本站的相關(guān)在線(xiàn)工具:
在線(xiàn)RGB、HEX顏色代碼生成器:
http://tools.jb51.net/color/rgb_color_generator
RGB顏色查詢(xún)對(duì)照表_顏色代碼表_顏色的英文名稱(chēng)大全:
http://tools.jb51.net/color/jPicker
在線(xiàn)網(wǎng)頁(yè)調(diào)色板工具:
http://tools.jb51.net/color/color_picker
在線(xiàn)顏色選擇器工具/RGB顏色查詢(xún)對(duì)照表:
http://tools.jb51.net/color/colorpicker
更多關(guān)于JavaScript相關(guān)內(nèi)容可查看本站專(zhuān)題:《JavaScript切換特效與技巧總結(jié)》、《JavaScript查找算法技巧總結(jié)》、《JavaScript動(dòng)畫(huà)特效與技巧匯總》、《JavaScript錯(cuò)誤與調(diào)試技巧總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》及《JavaScript數(shù)學(xué)運(yùn)算用法總結(jié)》
希望本文所述對(duì)大家JavaScript程序設(shè)計(jì)有所幫助。
- JS實(shí)現(xiàn)的RGB網(wǎng)頁(yè)顏色在線(xiàn)取色器完整實(shí)例
- js從10種顏色中隨機(jī)取色實(shí)現(xiàn)每次取出不同的顏色
- js 顏色選擇器(兼容firefox)
- 5款Javascript顏色選擇器
- javascript使用onclick事件改變選中行的顏色
- JS小功能(button選擇顏色)簡(jiǎn)單實(shí)例
- js實(shí)現(xiàn)的簡(jiǎn)單radio背景顏色選擇器代碼
- JavaScript獲取系統(tǒng)自帶的顏色選擇器功能(圖)
- 原生Javascript封裝的一個(gè)AJAX函數(shù)分享
- Javascript中的匿名函數(shù)與封裝介紹
相關(guān)文章
如何從JavaScript數(shù)組中刪除空對(duì)象
JS中數(shù)組是我們較為常用的一種數(shù)據(jù)結(jié)構(gòu),下面這篇文章主要給大家介紹了關(guān)于如何從JavaScript數(shù)組中刪除空對(duì)象的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-05-05
javascrit中undefined和null的區(qū)別詳解
這篇文章主要介紹了javascrit中undefined和null的區(qū)別詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-04-04
layui select 禁止點(diǎn)擊的實(shí)現(xiàn)方法
今天小編就為大家分享一篇layui select 禁止點(diǎn)擊的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-09-09
setTimeout()遞歸調(diào)用不加引號(hào)出錯(cuò)的解決方法
用了setTimeout()想實(shí)現(xiàn)遞歸調(diào)用,如果第一個(gè)參數(shù)不加引號(hào)的話(huà),就會(huì)出錯(cuò),下面與大家分享下該如何解決2014-09-09
jquery方法+js一般方法+js面向?qū)ο蠓椒▽?shí)現(xiàn)拖拽效果
多種方法制作的div拖拽,簡(jiǎn)單實(shí)用,包括了jquery方法、js一般方法、js面向?qū)ο蠓椒?/div> 2012-08-08
返回頂部按鈕響應(yīng)滾動(dòng)且動(dòng)態(tài)顯示與隱藏
返回頂部功能在很多的網(wǎng)站上都有,判斷滾動(dòng)參數(shù)動(dòng)態(tài)顯示與隱藏,下面的示例很實(shí)用,喜歡的朋友可以收藏下2014-10-10
使用JavaScript實(shí)現(xiàn)一個(gè)簡(jiǎn)單的哈希映射功能
哈希表大家應(yīng)該都經(jīng)常用到吧,那么大家有沒(méi)有想過(guò)哈希表是怎么實(shí)現(xiàn)的呢,本文我們就來(lái)從一道簡(jiǎn)單的題目來(lái)了解一下哈希表的簡(jiǎn)單原理和實(shí)現(xiàn)吧2024-02-02
JS實(shí)現(xiàn)查找數(shù)組中對(duì)象的屬性值是否存在示例
這篇文章主要介紹了JS實(shí)現(xiàn)查找數(shù)組中對(duì)象的屬性值是否存在,涉及javascript針對(duì)json數(shù)組的遍歷、查找相關(guān)操作技巧,需要的朋友可以參考下2019-05-05最新評(píng)論

