在我的网站上,我有h3个元素,其中每个字母都在它自己的跨度。这是因为我选择的字体是一个气球字体,我想给人的印象是它们是不同颜色的气球来组成我的标题,没有颜色会在另一个旁边看到,它们将在页面加载时随机选择,通过我下面的代码循环它们。
var defaultColors = ['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet'],
availColors = [],
randomColor = '';
$('h3 span').each(function () {
if (availColors.length == 0)
availColors = defaultColors.slice(0);
randomColor = availColors[Math.floor(Math.random() * availColors.length)];
availColors = $.grep(availColors, function (value) {
return value != randomColor;
});
$(this).css('color', randomColor);
});
我想通过将每个角色顺时针或逆时针旋转30°来添加进一步的现实主义元素,就好像他们是漂浮的一样。
看看实时视图当前的样子:http://rafflebananza.com/new-website/index.html
尝试更改
$(this).css('color', randomColor);
var degree = Math.random() > 0.5 ? '30' : '-30';
$(this).css({
color : randomColor,
'-webkit-transform' : 'rotate(' + degree + 'deg)',
'-moz-transform' : 'rotate(' + degree + 'deg)',
'-ms-transform' : 'rotate(' + degree + 'deg)',
'-o-transform' : 'rotate(' + degree + 'deg)',
transform : 'rotate(' + degree + 'deg)',
zoom : 1,
display : 'inline-block'
});