简体   繁体   English

切换页面时滑块不停留

[英]Slider not staying on when switching pages

I have made theme slider, the slider changes from light to dark for the website. 我制作了主题滑块,该滑块从网站的浅色变为深色。 My issue is that when a user uses the slider and loads a different page or refreshes the current page the theme switches back to light mode. 我的问题是,当用户使用滑块并加载其他页面或刷新当前页面时,主题将切换回浅色模式。 How can I somehow save the user's selection/choice? 如何以某种方式保存用户的选择/选择? Current link http://discordlokibot.ml/index.html 当前链接http://discordlokibot.ml/index.html

HTML HTML

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

CSS **Not sure if needed so let me know CSS **不确定是否需要,请通知我

JS 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)
})

Because you are refreshing a page/jumping to another page, which might refresh your DOM if you did not cache anything. 因为您正在刷新页面/跳转到另一个页面,所以如果您不缓存任何内容,则可能会刷新DOM。 When you try to get an element on this page when loaded it will always get the css default value. 当您尝试在加载时在此页面上获取元素时,它将始终获得css的默认值。

I will suggest that you can save the status value in your route as a param or query, eg, http://discordlokibot.ml/index.html?status=dark , then get status from route 我建议您可以将路径中的状态值另存为参数或查询,例如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'; 

Other solutions might be: 其他解决方案可能是:

(1) Save this value to session storage on browsers if you do not want to access this value next time (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) Or save this value to local storage if you want to use this value next time (2)如果要下次使用此值,则将该值保存到本地存储中


The status is only accessed when 'click' event is detected. 仅在检测到“单击”事件时才访问状态。 To achieve keeping same page mode, we need to access the saved value when page loaded: 为了保持页面模式不变,我们需要在页面加载时访问保存的值:

 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(); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM