You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WordPress Neve主题滚动100px后修改头部文字颜色的代码求助

解决Neve主题滚动后头部文字颜色修改问题

实现思路

通过监听页面滚动事件,当滚动距离超过100px时为目标元素添加自定义类,再通过高优先级CSS样式覆盖主题默认规则,实现文字颜色切换。

步骤1:添加滚动监听JavaScript代码

将以下代码添加到WordPress后台「外观→自定义→额外代码→自定义JS」区域,或主题的functions.php文件中(通过wp_enqueue_script注册):

window.addEventListener('scroll', () => {
  const scrollThreshold = 100;
  const header = document.querySelector('header.header');
  const primaryBtn = document.querySelector('a.button.button-primary');
  const bookNowEl = document.querySelector('.book-now-header');

  if (window.scrollY > scrollThreshold) {
    header.classList.add('scrolled-active');
    primaryBtn.classList.add('scrolled-btn');
    bookNowEl.classList.add('scrolled-book-text');
  } else {
    header.classList.remove('scrolled-active');
    primaryBtn.classList.remove('scrolled-btn');
    bookNowEl.classList.remove('scrolled-book-text');
  }
});

步骤2:添加自定义CSS样式

将以下代码添加到「外观→自定义→额外代码→自定义CSS」区域,或主题的style.css文件中:

/* 滚动后头部背景色(若插件未生效可在此设置) */
header.header.scrolled-active {
  background-color: #222222; /* 替换为你的目标背景色 */
}

/* 滚动后主按钮文字颜色 */
a.button.button-primary.scrolled-btn {
  color: #ffffff !important; /* 替换为你的目标文字颜色 */
}

/* 滚动后book-now区域文字颜色 */
.book-now-header.scrolled-book-text {
  color: #ffffff !important; /* 替换为你的目标文字颜色 */
}

注意事项

  • 把CSS中的颜色值替换为你实际需要的颜色代码
  • 使用!important是为了强制覆盖Neve主题的默认样式优先级,确保修改生效
  • 如果元素选择器匹配有问题,可以通过浏览器开发者工具(F12)确认目标元素的准确类名

内容的提问来源于stack exchange,提问作者Ryan1827

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 09:25:39