You need to enable JavaScript to run this app. 如何用HTML+CSS+jQuery实现刷新页面时保留已选中的header背景图
优惠活动
大模型
产品
解决方案
定价
更多

如何用HTML+CSS+jQuery实现刷新页面时保留已选中的header背景图

背景切换偏好持久化实现

直接使用原生localStorage存储用户选中的背景配置即可,无需额外引入Cookie操作逻辑,CSS部分无需任何修改,仅需调整页面脚本逻辑即可实现需求。

完整可运行代码

#header{
  background-image: url('https://i.imgur.com/oKe8JBR.png');
  background-repeat:no-repeat ;
  background-position: center;
  background-size: cover;
  width:500px;
  height:500px;
}

.test{
    background: url('https://i.imgur.com/oKe8JBR.png');
    border: 0;
    padding: 37px;
    background-size: cover;
    padding-left: 70px;
}

.test2{
    background: url('https://i.imgur.com/UZhKDMu.png');
    border: 0;
   padding: 37px;
    background-size: cover;
    padding-left: 70px;
}

.test3{
    background: url('https://i.pinimg.com/originals/a0/df/19/a0df19b8781bca3f2356013600131728.jpg');
    border: 0;
    padding: 37px;
    background-size: cover;
    padding-left: 70px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

<body>
  <header id="header"></header>
  <button class="test"></button>
  <button class="test2"></button>
  <button class="test3"></button>
  
  <script>
    // 本地存储键名,避免和其他业务冲突
    const STORAGE_KEY = 'header_bg_config';
    // 默认背景配置,和原有#header的CSS样式完全对齐
    const DEFAULT_CONFIG = {
      url: 'https://i.imgur.com/oKe8JBR.png',
      positionY: '50%',
      size: 'cover'
    };

    // 公共方法:将传入的背景配置应用到#header元素
    function applyHeaderBg(config) {
      $('#header').css({
        'background-image': `url(${config.url})`,
        'background-position-y': config.positionY,
        'background-size': config.size,
        'background-repeat': 'no-repeat'
      });
    }

    $(document).ready(function() {
      // 页面加载时读取本地存储的配置
      const savedConfig = localStorage.getItem(STORAGE_KEY);
      if (savedConfig) {
        try {
          // 解析并应用存储的配置
          applyHeaderBg(JSON.parse(savedConfig));
        } catch (err) {
          // 存储内容异常时回退到默认配置
          applyHeaderBg(DEFAULT_CONFIG);
        }
      } else {
        // 无存储记录时使用默认配置
        applyHeaderBg(DEFAULT_CONFIG);
      }

      // 绑定三个切换按钮的点击事件
      $('.test').click(function() {
        const config = {
          url: 'https://i.imgur.com/oKe8JBR.png',
          positionY: '13.2%',
          size: 'cover'
        };
        applyHeaderBg(config);
        // 存储当前选中的配置
        localStorage.setItem(STORAGE_KEY, JSON.stringify(config));
      });

      $('.test2').click(function() {
        const config = {
          url: 'https://i.imgur.com/UZhKDMu.png',
          positionY: '40%',
          size: 'cover'
        };
        applyHeaderBg(config);
        localStorage.setItem(STORAGE_KEY, JSON.stringify(config));
      });

      $('.test3').click(function() {
        const config = {
          url: 'https://i.pinimg.com/originals/a0/df/19/a0df19b8781bca3f2356013600131728.jpg',
          positionY: '50%',
          size: 'cover'
        };
        applyHeaderBg(config);
        localStorage.setItem(STORAGE_KEY, JSON.stringify(config));
      });
    });
  </script>
</body>

核心逻辑说明

  • 所有配置存储在localStorage中,无过期限制,关闭浏览器后再次打开页面仍会保留用户最后选择的背景
  • 增加了异常兼容:如果本地存储的配置被手动篡改、格式损坏,会自动回退到默认背景,不会出现样式错乱
  • 完全兼容原有功能逻辑,没有改动原有按钮的交互和样式

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.29 22:45:03