JavaScript 代码:动态背景渐变效果
var colors = [ '#FF9966', '#FF5E62', '#00F260', '#0575E6', '#E1EEC3', '#F05053' ];
function getRandomColor() { var randomIndex = Math.floor(Math.random() * colors.length); return colors[randomIndex]; }
function changeBackgroundColor() { var color1 = getRandomColor(); var color2 = getRandomColor(); var gradient = 'linear-gradient(to right, ' + color1 + ', ' + color2 + ')'; document.body.style.background = gradient; }
setInterval(changeBackgroundColor, 5000);
var granimInstance = new Granim({ element: '#canvas-basic', direction: 'radial', isPausedWhenNotInView: true, states : { 'default-state': { gradients: [ [colors[0], colors[1]], [colors[2], colors[3]], [colors[4], colors[5]] ] } } });
function moveColors() { var gradient = document.body.style.background; var parts = gradient.split(','); var newGradient = ''; for (var i = 0; i < parts.length; i++) { var part = parts[i]; var color = part.match(/#[0-9A-F]{6}/)[0]; var direction = part.includes('to right') ? 'to left' : 'to right'; var position = part.match(/\d+(?:.\d+)?%$/); if (position) { var newPosition = parseFloat(position[0]) + 1; if (newPosition > 100) { newPosition = 0; color = getRandomColor(); } part = part.replace(position[0], newPosition + '%'); } else { part += ' 0%'; } part = part.replace(/(to\s(left|right))/, direction); part = part.replace(/#[0-9A-F]{6}/, color); newGradient += part + (i == parts.length - 1 ? '' : ','); } document.body.style.background = newGradient; }
setInterval(moveColors, 50);
AI 优化版
原文地址:https://www.cveoy.top/t/topic/nr2s 著作权归作者所有。请勿转载和采集!