javascript - 如果我滚动并刷新网页,我的导航标题就会丢失其属性。 jQuery

标签 javascript jquery

当我滚动时,我的导航标题从透明变为纯色,并且效果非常好。但是每当我刷新页面一半时,我的导航标题就会丢失其属性,并且看起来 jquery 会重置并且我的导航标题又恢复为透明。如果我刷新页面并且在页面的中间位置,我希望它保持纯色。

$(document).ready(function(){       
   var scroll_start = 0;
   var startchange = $('.header');
   var offset = startchange.offset();
   $(document).scroll(function() { 
      scroll_start = $(this).scrollTop();
      if(scroll_start > offset.top) {
          $('.header').css('background-color', '#111417');
       } else {
          $('.header').css('background-color', 'transparent');
       }
   });

});

最佳答案

您可以将状态保存在 cookie 中,并且每当页面刷新时,从 cookie 中读取状态并设置适当的 css 或类。

 $(document).scroll(function() { 
      scroll_start = $(this).scrollTop();
      if(scroll_start > offset.top) {
          $('.header').css('background-color', '#111417');
           //here save the cookie for example class or the css
       } else {
          $('.header').css('background-color', 'transparent');
           //here save the cookie
       }
   });

关于javascript - 如果我滚动并刷新网页,我的导航标题就会丢失其属性。 jQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35804734/

相关文章:

javascript - 了解 React Router 参数

javascript - 主干路由器函数 goto 不存在

javascript - 拖放和可排序 jquery 插件之间的冲突

javascript - 数组填充时监听事件 (Array.push(object))

jquery - 在 jQuery Accordion 导航上切换 CSS 类

jquery - Bootstrap Accordion 允许多个打开

javascript - 使用 Javascript 移动 "disabled"类

javascript - 如何在javascript中存储类似的对象

javascript - Datatables jQuery 事件适用于第二页,不适用于其余页面

javascript - 如何根据浏览器大小调整我的 div 的高度