【问题标题】:Slider not staying on when switching pages切换页面时滑块不停留
【发布时间】:2019-03-03 22:58:03
【问题描述】:

我制作了主题滑块,网站的滑块从浅色变为深色。我的问题是,当用户使用滑块并加载不同的页面或刷新当前页面时,主题会切换回浅色模式。我怎样才能以某种方式保存用户的选择/选择?当前链接http://discordlokibot.ml/index.html

HTML

<div>    
  <div id="nav">
    <div id="slider-background">
    <div id="slider" class="indexSlider">
  </div>
</div>

CSS **不确定是否需要,请告诉我

JS

var status = 'light';
var slider = document.getElementById('slider')
var sliderbg = document.getElementById('slider-background')
var nav = document.getElementById('nav')
var mainback = document.getElementById('backmain')
var footer = document.getElementById('footer')
var text = document.querySelectorAll('p,li,a,h1,h2,h3')
var windows = document.querySelectorAll('#window1,#window2,#window3,#window4')
var announcement = document.querySelector('#announcement')
var closebtn = document.querySelector('.close-btn')
var whiteimages = document.querySelectorAll('.white-images')
var blackimages = document.querySelectorAll('.black-images')

slider.addEventListener('click', function() {
    if (status == 'light') {
        status = 'dark'
        nav.style.background = '#282A2E'
        footer.style.background = '#282A2E'
        backmain.style.background = '#1D1F21'
        sliderbg.style.background = '#f2f2f2'
        slider.style.background = '#000'
        slider.style.marginLeft = '1.6em'
        for (i = 0; i < text.length; i++) {
            text[i].style.color = '#f2f2f2'
        }
        for (i = 0; i < windows.length; i++) {
            windows[i].style.background = '#282A2E'
        }
        for (i = 0; i < whiteimages.length; i++) {
            whiteimages[i].style.display = 'inline-block'
        }
        for (i = 0; i < blackimages.length; i++) {
            blackimages[i].style.display = 'none'
        }
    } else {
        status = 'light'
        nav.style.background = '#f2f2f2'
        footer.style.background = '#f2f2f2'
        sliderbg.style.background = '#000'
        slider.style.background = '#f2f2f2'
        slider.style.marginLeft = '0.2em'
        backmain.style.background = '#e5e5e5'
        for (i = 0; i < text.length; i++) {
            text[i].style.color = '#000'
        }
        for (i = 0; i < windows.length; i++) {
            windows[i].style.background = '#f2f2f2'
        }
        for (i = 0; i < whiteimages.length; i++) {
            whiteimages[i].style.display = 'none'
        }
        for (i = 0; i < blackimages.length; i++) {
            blackimages[i].style.display = 'inline-block'
        }
    }
})

closebtn.addEventListener('click', function() {
    announcement.style.height = '0px'
    setTimeout(function() {
        announcement.style.display = 'none'
    }, 510)
})

【问题讨论】:

标签: javascript


【解决方案1】:

因为您正在刷新页面/跳转到另一个页面,如果您没有缓存任何内容,这可能会刷新您的 DOM。当您尝试在加载时获取此页面上的元素时,它将始终获得 css 默认值。

我建议你可以将路由中的状态值保存为参数或查询,例如http://discordlokibot.ml/index.html?status=dark,然后从路由中获取状态

// get your query in route
let query = new URLSearchParams(window.location.search);
let status = query.get("status") || 'light';

其他解决方案可能是:

(1) 如果您下次不想访问此值,请将此值保存到浏览器的会话存储中

 // when user click on slider
 if (window.sessionStorage) {
    sessionStorage.setItem("slider-status", 'light');
 }
 
 // when user jump/fresh page
 if (window.sessionStorage) {
    let status = sessionStorage.getItem("slider-status") ||  'light';
    ...
 }

(2) 或者如果下次要使用这个值,把这个值保存到本地存储


只有在检测到“点击”事件时才会访问该状态。为了实现同页模式,我们需要在页面加载时访问保存的值:

function initStatus() {
    // access your status value in storage
    
    // if storage is emtpy, set a default value
    
    // assign different color based on your 'status'
 }
 
 window.onload = initStatus();

【讨论】:

  • 有道理。但奇怪的是,当我输入 sessionStorage 甚至将其更改为本地存储时,它似乎禁用了滑块......你认为这可能是什么?
  • 更新到那个,我得到它的工作,但仍然有改变页面的问题。
  • 我猜是因为你使用'slider.addEventListener'来触发颜色变化。例如,当一个页面被加载并且用户没有真正点击滑块时,它将显示 css 颜色而不是访问状态值来显示颜色。您可能需要在页面加载时读取您的状态。
猜你喜欢
  • 2020-08-27
  • 1970-01-01
  • 1970-01-01
  • 2021-10-03
  • 1970-01-01
  • 2015-12-10
  • 1970-01-01
  • 1970-01-01
  • 2020-05-02
相关资源
最近更新 更多