我的代码很难将rgb背景颜色转换为Hex
它说“TypeError:rgb未定义”。我只是不明白。
我使用jquery 1.9.1
任何的想法?最好
$(document).ready(function(){
$(".main").onepage_scroll({
sectionContainer: "section"
});
function rgb2hex(rgb) {
var hexDigits = ["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"];
rgb = rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
function hex(x) {
return isNaN(x) ? "00" : hexDigits[(x - x % 16) / 16] + hexDigits[x % 16];
}
return "#" + hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3]);
}
$('.background').each(function(i){
var id = '#projet' +i;
var idColor ='#paragraphe' +i;
var color = $(idColor).css('background-color');
var hexa = rgb2hex(color);
$(id).bind('inview', function(event, visible){
if(visible){
$('body').stop().animate({
backgroundColor: 'hexa'
}, 1000);
console.log(idColor);
console.log(hexa);
} return false;
});
});
});