MediaWiki:Common.js:修订间差异
MediaWiki界面页面
更多操作
无编辑摘要 |
无编辑摘要 |
||
| 第22行: | 第22行: | ||
function weightedRandomSelection(items) { | function weightedRandomSelection(items) { | ||
// Calculate the total weight of all items. | // Calculate the total weight of all items. | ||
var totalWeight = 0; | |||
for (var i = 0; i < items.length; i++) { | |||
totalWeight += items[i][1]; | |||
} | |||
// Generate a random value between 0 and the total weight. | // Generate a random value between 0 and the total weight. | ||
var randomValue = Math.random() * totalWeight; | |||
// Iterate over the items, keeping track of the current weight. | // Iterate over the items, keeping track of the current weight. | ||
var currentWeight = 0; | |||
for ( | for (i = 0; i < items.length; i++) { | ||
currentWeight += | currentWeight += items[i][1]; | ||
// If the current weight is greater than or equal to the random value, return the item. | // If the current weight is greater than or equal to the random value, return the item. | ||
if (currentWeight >= randomValue) { | if (currentWeight >= randomValue) { | ||
return | return items[i][0]; | ||
} | } | ||
} | } | ||
// If we reach here, it means that the random value was greater than the total weight, which should never happen. | |||
throw new Error("Random value was greater than total weight."); | |||
} | } | ||
var bg_links = [ | |||
('url(https://0v0.zjsnrwiki.com/static/bg_4.webp)', 0.16), | ('url(https://0v0.zjsnrwiki.com/static/bg_4.webp)', 0.16), | ||
('url(https://0v0.zjsnrwiki.com/static/bg_5.webp)', 0.16), | ('url(https://0v0.zjsnrwiki.com/static/bg_5.webp)', 0.16), | ||
| 第47行: | 第53行: | ||
('url(https://0v0.zjsnrwiki.com/static/bg_8b.webp)', 0.16), | ('url(https://0v0.zjsnrwiki.com/static/bg_8b.webp)', 0.16), | ||
]; | ]; | ||
var random_bg_link = weightedRandomSelection(bg_links); | |||
console.log(random_bg_link); | console.log(random_bg_link); | ||
$('body.skin-vector').css('background',random_bg_link[0]); | $('body.skin-vector').css('background',random_bg_link[0]); | ||