返回列表 发布新帖
查看: 39|回复: 0

[代码技巧] 子比主题 – 论坛帖子标签颜色修改

[复制链接]
SunJu_FaceMall
社区贡献

315

主题

190

回帖

1万

积分

等级头衔
Icon组别 : 管理员
Icon等级 :

积分成就
   钻石 : 524 颗
   贡献 : 1779 点
   金币 : 12 枚
Icon在线时间 : 1295 小时
Icon注册时间 : 2024-11-22
Icon最后登录 : 2025-11-14

荣誉勋章

会员LV.1会员LV.2会员LV.3会员LV.4会员LV.5会员LV.6会员LV.7会员LV.8会员LV.9会员LV.10

风云·优秀版主

飞流名人堂成员

1

实名认证 手机认证 vip vip-year FLLTCN发表于 2025-10-9 23:50:35 | 查看全部 |阅读模式 浙江金华

资源无需等待,交易就趁现在,全面资源整合网络大咖云集,让你轻松玩转互联网!

您需要 登录 才可以下载或查看,没有账号?立即注册

×
基于6ke论坛提供的美化代码修改了下颜色方案并添加了文本自适应根据色彩而改变(就是黑白两色而已),这样的话,找颜色方案的时候看着嘎嘎好看,一适配怎么就怪怪的的~简单留个记录吧

美化效果


tengfei_down (4).webp

CSS代码


  1. /*论坛标签美化颜色JS代码*/
  2. const tagColors = [
  3.     { bg: `linear-gradient(120deg, #FFF0DE 20%, #6FB4BC 80%)`, textColor: getTextColor('#FFF0DE', '#6FB4BC') },
  4.     { bg: `linear-gradient(120deg, #FFE6E3 20%, #8379A8 80%)`, textColor: getTextColor('#FFE6E3', '#8379A8') },
  5.     { bg: `linear-gradient(120deg, #E3FDFF 20%, #D17AAA 80%)`, textColor: getTextColor('#E3FDFF', '#D17AAA') },
  6.     { bg: `linear-gradient(120deg, #FFF0DF 20%, #8A3F3F 80%)`, textColor: getTextColor('#FFF0DF', '#8A3F3F') },
  7.     { bg: `linear-gradient(120deg, #E4F6DA 20%, #898CC7 80%)`, textColor: getTextColor('#E4F6DA', '#898CC7') },
  8.     { bg: `linear-gradient(120deg, #FCE5FF 20%, #87AEBE 80%)`, textColor: getTextColor('#FCE5FF', '#87AEBE') },
  9.     { bg: `linear-gradient(120deg, #FFE8DE 20%, #7FBB8A 80%)`, textColor: getTextColor('#FFE8DE', '#7FBB8A') },
  10.     { bg: `linear-gradient(120deg, #E8E5FF 20%, #BE87B9 80%)`, textColor: getTextColor('#E8E5FF', '#BE87B9') },
  11.     { bg: `linear-gradient(120deg, #E5FFFD 20%, #FF768D 80%)`, textColor: getTextColor('#E5FFFD', '#FF768D') },
  12.     { bg: `linear-gradient(120deg, #FFFEDA 20%, #5E6BFF 80%)`, textColor: getTextColor('#FFFEDA', '#5E6BFF') },
  13.     { bg: `linear-gradient(120deg, #FFFCDD 20%, #1899B6 80%)`, textColor: getTextColor('#FFFCDD', '#1899B6') },
  14.     { bg: `linear-gradient(120deg, #FFF4E3 20%, #30A9FF 80%)`, textColor: getTextColor('#FFF4E3', '#30A9FF') },
  15.     { bg: `linear-gradient(120deg, #E2FFFD 20%, #F5600F 80%)`, textColor: getTextColor('#E2FFFD', '#F5600F') },
  16.     { bg: `linear-gradient(120deg, #FFDDDD 20%, #5D62CC 80%)`, textColor: getTextColor('#FFDDDD', '#5D62CC') },
  17.     { bg: `linear-gradient(120deg, #FFD0D0 20%, #18B670 80%)`, textColor: getTextColor('#FFD0D0', '#18B670') },
  18.     { bg: `linear-gradient(120deg, #DDFEFF 20%, #B61877 80%)`, textColor: getTextColor('#DDFEFF', '#B61877') },
  19.     { bg: `linear-gradient(120deg, #D2FFD2 20%, #FF3730 80%)`, textColor: getTextColor('#D2FFD2', '#FF3730') },
  20.     { bg: `linear-gradient(120deg, #FFEF72 20%, #8274FF 80%)`, textColor: getTextColor('#FFEF72', '#8274FF') },
  21.     { bg: `linear-gradient(120deg, #FBF2E3 20%, #28333E 80%)`, textColor: getTextColor('#FBF2E3', '#28333E') },
  22.     { bg: `linear-gradient(120deg, #E2E1E4 20%, #747598 80%)`, textColor: getTextColor('#E2E1E4', '#747598') },
  23.     { bg: `linear-gradient(120deg, #8A8C01 20%, #158888 80%)`, textColor: getTextColor('#8A8C01', '#158888') },
  24.     { bg: `linear-gradient(120deg, #FBF2E3 20%, #2B333E 80%)`, textColor: getTextColor('#FBF2E3', '#2B333E') },
  25.     { bg: `linear-gradient(120deg, #FBEBE6 20%, #AC1F18 80%)`, textColor: getTextColor('#FBEBE6', '#AC1F18') },
  26.     { bg: `linear-gradient(120deg, #FDEEE9 20%, #1E2020 80%)`, textColor: getTextColor('#FDEEE9', '#1E2020') }
  27. ];

  28. function getTextColor(color1, color2) {
  29.     const luminance1 = calculateLuminance(color1);
  30.     const luminance2 = calculateLuminance(color2);
  31.     const avgLuminance = (luminance1 + luminance2) / 2;
  32.     return avgLuminance > 0.5 ? '#000000' : '#ffffff';
  33. }

  34. function calculateLuminance(hexColor) {
  35.     const r = parseInt(hexColor.slice(1, 3), 16) / 255;
  36.     const g = parseInt(hexColor.slice(3, 5), 16) / 255;
  37.     const b = parseInt(hexColor.slice(5, 7), 16) / 255;
  38.     const RsRGB = r <= 0.03928 ? r / 12.92 : Math.pow((r + 0.055) / 1.055, 2.4);
  39.     const GsRGB = g <= 0.03928 ? g / 12.92 : Math.pow((g + 0.055) / 1.055, 2.4);
  40.     const BsRGB = b <= 0.03928 ? b / 12.92 : Math.pow((b + 0.055) / 1.055, 2.4);
  41.     return 0.2126 * RsRGB + 0.7152 * GsRGB + 0.0722 * BsRGB;
  42. }

  43. let tagsInitialized = false;

  44. function applyRandomColor(tag) {
  45.     if (tag.dataset.colorApplied) return;
  46.     const randomColor = tagColors[Math.floor(Math.random() * tagColors.length)];
  47.     tag.style.setProperty('background', randomColor.bg, 'important');
  48.     tag.style.setProperty('color', randomColor.textColor, 'important');
  49.     tag.style.setProperty('text-shadow', '0 1px 2px rgba(0, 0, 0, 0.1)', 'important');
  50.     tag.dataset.colorApplied = 'true';
  51. }

  52. function setupTagDrawer() {
  53.     const tagWrappers = document.querySelectorAll('.tag-wrapper');
  54.     const isMobile = window.innerWidth <= 768;
  55.     const visibleTagCount = isMobile ? 2 : 3;

  56.     tagWrappers.forEach(wrapper => {
  57.         const container = wrapper.querySelector('.tag-container');
  58.         const tags = container.querySelectorAll('a.liuke-biaoqian.tag-link');

  59.         if (!tagsInitialized) {
  60.             tags.forEach(applyRandomColor);
  61.         }

  62.         setTimeout(() => {
  63.             const visibleTags = Array.from(tags).slice(0, visibleTagCount);
  64.             const initialWidth = visibleTags.reduce((sum, tag) => sum + tag.offsetWidth + 6, 0);
  65.             const fullWidth = Array.from(tags).reduce((sum, tag) => sum + tag.offsetWidth + 6, 0);

  66.             wrapper.style.width = `${initialWidth}px`;

  67.             wrapper.addEventListener('mouseenter', () => {
  68.                 wrapper.style.width = `${fullWidth}px`;
  69.             });

  70.             wrapper.addEventListener('mouseleave', () => {
  71.                 wrapper.style.width = `${initialWidth}px`;
  72.             });
  73.         }, 100);
  74.     });

  75.     tagsInitialized = true;
  76. }

  77. function initializeTags() {
  78.     setTimeout(setupTagDrawer, 100);
  79. }

  80. function applyTagEffects() {
  81.     const tags = document.querySelectorAll('a.liuke-biaoqian.tag-link');
  82.     tags.forEach(applyRandomColor);
  83.     initializeTags();
  84. }
  85. document.addEventListener('DOMContentLoaded', function () {
  86.     applyTagEffects();
  87.     document.addEventListener('ajaxComplete', function (event) {
  88.         applyTagEffects();
  89.     });
  90.     const observer = new MutationObserver((mutations) => {
  91.         let tagsAdded = false;
  92.         mutations.forEach((mutation) => {
  93.             if (mutation.type === 'childList') {
  94.                 mutation.addedNodes.forEach((node) => {
  95.                     if (node.nodeType === Node.ELEMENT_NODE) {
  96.                         if (node.matches('a.liuke-biaoqian.tag-link')) {
  97.                             tagsAdded = true;
  98.                         } else if (node.querySelector('a.liuke-biaoqian.tag-link')) {
  99.                             tagsAdded = true;
  100.                         }
  101.                     }
  102.                 });
  103.             }
  104.         });
  105.         if (tagsAdded) {
  106.             applyTagEffects();
  107.         }
  108.     });
  109.     const config = { childList: true, subtree: true };
  110.     if (document.body) {
  111.         observer.observe(document.body, config);
  112.     } else {
  113.         console.warn('document.body is not available yet.');
  114.     }
  115. });
  116. window.addEventListener('load', function () {
  117.     setTimeout(setupTagDrawer, 100);
  118. });
  119. window.addEventListener('resize', setupTagDrawer);
  120. const style = document.createElement('style');
  121. style.textContent = `
  122.     a.liuke-biaoqian.tag-link {
  123.         transition: background 0.3s ease, color 0.3s ease;
  124.         opacity: 0;
  125.         animation: fadeIn 0.3s ease forwards;
  126.     }
  127.     @keyframes fadeIn {
  128.         to { opacity: 1; }
  129.     }
  130. `;
  131. document.head.appendChild(style);
  132. /*论坛标签美化颜色JS代码*/
复制代码


本帖被以下淘专辑推荐:

路虽远,行则将至;事虽难,做则必成。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

飞流广播+ 发布

系统消息:柒沐已经连续答对3道难题,逆天学霸谁与争锋?!#每日答题#
10-30 17:02
系统消息:柒沐已经连续答对10道难题,逆天学霸谁与争锋?!#每日答题#
10-09 09:07
系统消息:柒沐已经连续答对3道难题,逆天学霸谁与争锋?!#每日答题#
09-24 09:00
系统消息:柒沐已经连续答对3道难题,逆天学霸谁与争锋?!#每日答题#
09-11 11:40
系统消息:柒沐已经连续答对3道难题,逆天学霸谁与争锋?!#每日答题#
09-02 09:17
系统消息:柒沐已经连续答对3道难题,逆天学霸谁与争锋?!#每日答题#
08-27 08:56
系统消息:柒沐已经连续答对3道难题,逆天学霸谁与争锋?!#每日答题#
08-20 15:12
系统消息:柒沐已经连续答对3道难题,逆天学霸谁与争锋?!#每日答题#
08-03 10:22
系统消息:柒沐已经连续答对10道难题,逆天学霸谁与争锋?!#每日答题#
06-30 08:57
系统消息:柒沐已经连续答对3道难题,逆天学霸谁与争锋?!#每日答题#
06-18 09:14
系统消息:清风网络已经连续答对10道难题,逆天学霸谁与争锋?!#每日答题#
04-11 09:40
系统消息:清风网络已经连续答对3道难题,逆天学霸谁与争锋?!#每日答题#
04-10 09:31
系统消息:IXM77777已经连续答对3道难题,逆天学霸谁与争锋?!#每日答题#
04-09 13:44
系统消息:清风网络已经连续答对3道难题,逆天学霸谁与争锋?!#每日答题#
04-09 09:22
系统消息:柒沐已经连续答对10道难题,逆天学霸谁与争锋?!#每日答题#
04-09 08:52
系统消息:清风网络已经连续答对3道难题,逆天学霸谁与争锋?!#每日答题#
04-08 09:24
系统消息:柒沐已经连续答对3道难题,逆天学霸谁与争锋?!#每日答题#
04-07 09:02
系统消息:柒沐已经连续答对10道难题,逆天学霸谁与争锋?!#每日答题#
02-27 09:35
系统消息:柒沐已经连续答对3道难题,逆天学霸谁与争锋?!#每日答题#
02-26 09:06
系统消息:柒沐已经连续答对3道难题,逆天学霸谁与争锋?!#每日答题#
02-25 08:49
站内通告

提供资源交易、信息共享、靓号交流、技术变现、学习问答、兴趣娱乐等全面服务。

1.丰富功能系统,扩展社区特色玩法,打造最好的互联网聚集圈子。

2.准确信息真实交易,安全快捷又方便,让虚拟交易面对面。

3. 天上不会掉馅饼,话术骗术迷人心,切勿脱离平台线下交易,被骗与平台无关!

4. 欺诈骗钱,违规违法将视情受到警告&禁言&封号甚至检举至👮🏻‍♀️处理!

官方Q群:123129钉推群:BAYR2383 站长QQ:3220000000

投诉/建议/商务合作联系

fl@fllt.cn

严禁私下交易,被骗与本站无关。
违反交易细则,取证立查严惩。
  • 钉钉新帖推送群
  • 官方交流QQ群
  • 站长唯一微信号

👮曝光Ta|🧿小黑屋|📴手机页|飞流网 ( 渝ICP备2025054677号-1|电信增值许可 渝B2-20250789 )|网站地图

GMT+8, 2025-11-16 13:47 , Processed in 0.081072 second(s), 61 queries, MemCached On , Gzip On.

Based on XJ-TX X3.5 Licensed

飞流论坛 HanAnalytics icp Astro vhAstro-Theme

关灯 在本版发帖
扫一扫添加微信客服
QQ客服返回顶部
快速回复 返回顶部 返回列表