我一直试图在本网站的图片上进行随机颜色叠加。 http://www.reportageborsen.se/reportageborsen/wordpress
我想要合并的Javascript是:
$(function() {
$('.media-box .mask').each(function() {
var hue = 'rgb(' + (Math.floor((256 - 199) * Math.random()) + 200) + ',' + (Math.floor((256 - 199) * Math.random()) + 200) + ',' + (Math.floor((256 - 199) * Math.random()) + 200) + ')';
$(this).css("background-color", hue);
});
});
和/// ///
$('.media-box').hover(function() {
$(this).find('.mask').stop(true, true).fadeIn();
}, function() {
$(this).find('.mask').stop(true, true).fadeOut();
});
是否有可能以某种方式将它们组合在一起?
最好的问候
Fortes
答案 0 :(得分:2)
如果您想将两个功能合二为一,可以试试这个:
var getRandomInRange = function(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
};
$('.media-box').each(function() {
var mediaBox = $(this);
var mask = mediaBox.find('.mask');
var hue = 'rgb(' + getRandomInRange(200, 255) + ',' + getRandomInRange(200, 255) + ',' + getRandomInRange(200, 255) + ')';
mask.css("background-color", hue);
mediaBox.hover(function() {
mask.stop(true, true).fadeIn();
}, function() {
mask.stop(true, true).fadeOut();
});
});
注意,为了清楚起见,我还将随机数生成器移到了单独的函数中。如果有效,请告诉我。