简体   繁体   English

切换页面时切换显示返回

[英]switch display turning back when switching pages

I made a switch based on a something I found online, and it gave me some problems along the way. 我根据我在网上找到的东西做了一个转换,它给我带来了一些问题。 When I press the switch to the "on" state, the variable that it should change is changed properly, and the switch is also displaying that it is in the right mode. 当我将开关按到“开”状态时,它应该更改的变量会正确更改,并且开关也会显示它处于正确模式。 But when I switch to a different page and back, the switch appears to look like you have not turned it on, even though the variable that is supposed to change with it, actually did get remembered. 但是,当我切换到另一个页面并返回时,开关似乎看起来没有打开它,即使应该随之改变的变量实际上确实被记住了。

I was wondering how I can make it using html, css and javascript, such that the page sees that if the variable has the "on" value, it will always display the switch as if it were on, and always appear to be in the "off" state if that is not the case. 我想知道如何使用html,css和javascript来实现它,这样页面看到如果变量具有“on”值,它将始终显示开关,就好像它已经打开一样,并且总是看起来在如果不是这样的话,“关闭”状态。

Here is the code I am using: 这是我正在使用的代码:

Javascript: 使用Javascript:

  document.addEventListener('DOMContentLoaded', function () {
  var checkbox = document.querySelector('input[type="checkbox"]');

  checkbox.addEventListener('change', function () {
    if (checkbox.checked) {
      ProgramState = "off";
      put("weekProgramState", "week_program_state", ProgramState);
      console.log(ProgramState);
    } else {
      ProgramState = "on";
      put("weekProgramState", "week_program_state", ProgramState);
      console.log(ProgramState);
    }
  });
});

CSS: CSS:

/* The switch - the box around the slider */
.switch {
  position: relative;
  display: inline-block;
  width: 50px;
  height: 27px;
}

/* Hide default HTML checkbox */
.switch input {display:none;}

/* The slider */
.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #ccc;
  -webkit-transition: .4s;
  transition: .4s;
}

.slider:before {
  position: absolute;
  content: "";
  height: 20px;
  width: 20px;
  left: 4px;
  bottom: 4px;
  background-color: white;
  -webkit-transition: .4s;
  transition: .4s;
}

input:checked + .slider {
  background-color: #2196F3;
}

input:focus + .slider {
  box-shadow: 0 0 1px #2196F3;
}

input:checked + .slider:before {
  -webkit-transform: translateX(20px);
  -ms-transform: translateX(20px);
  transform: translateX(20px);
}

/* Rounded sliders */
.slider.round {
  border-radius: 34px;
}

.slider.round:before {
  border-radius: 50%;
}

HTML: HTML:

<h3> Program Override
<label class="switch">
  <input type="checkbox">
  <div class="slider round"></div>
</label> </h3>

If you want to do this using only client-side functionalities, then you will either use cookies , localStorage or sessionStorage. 如果您只想使用客户端功能,那么您将使用cookielocalStorage或sessionStorage。

If you decide to work with Javascript cookies, then you will need to work with document.cookie . 如果您决定使用Javascript cookie,那么您将需要使用document.cookie When you set its value, you can set parameters and expiration date. 设置其值时,可以设置参数和到期日期。 Via the parameters you can establish a way to store information and reload it, via an onoff parameter, for instance. 通过参数,您可以通过onoff参数建立存储信息并重新加载信息的方法。 By default, Javascript cookies will expire when the user closes the browser. 默认情况下,当用户关闭浏览器时,Javascript cookie将过期。 Helpful functions: 实用功能:

function setCookie(cname, cvalue, exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays*24*60*60*1000));
    var expires = "expires="+ d.toUTCString();
    document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}

and

function getCookie(cname) {
    var name = cname + "=";
    var decodedCookie = decodeURIComponent(document.cookie);
    var ca = decodedCookie.split(';');
    for(var i = 0; i <ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') {
            c = c.substring(1);
        }
        if (c.indexOf(name) == 0) {
            return c.substring(name.length, c.length);
        }
    }
    return "";
}

You can decide to use a library to help you if you aim for simplicity, or you can write your own. 如果您的目标是简单,您可以决定使用图书馆来帮助您,或者您可以编写自己的图书馆。 Somce of the libraries are js-cookie and CookieHandler . 库的Somce是js-cookieCookieHandler

If you decide to use localStorage, then you will work with localStorage.setItem("yourKey", "yourValue") to store values and localStorage.getItem("yourKey") to load values. 如果您决定使用localStorage,那么您将使用localStorage.setItem("yourKey", "yourValue")来存储值,并使用localStorage.getItem("yourKey")来加载值。 If you itend to remove items, then localStorage.removeItem("yourKey") will be quite handy. 如果你迭代删除项目,那么localStorage.removeItem("yourKey")将非常方便。 Items stored in localStorage will not expire. 存储在localStorage项目不会过期。 sessionStorage is very similar in concept and usage to localStorage , with the most notable difference being that sessionStorage will expire when the session expires. sessionStorage在概念和用法上与localStorage非常相似,最显着的区别是sessionStorage将在会话到期时到期。

Otherwise, if you want to store the state using the server, then you will either need to store the state in a database or a file on server-side, however, using a database is clearly superior than storing the data in files on server-side. 否则,如果要使用服务器存储状态,则需要将状态存储在数据库中或服务器端的文件中,但是,使用数据库显然优于将数据存储在服务器上的文件中 -侧。 Either way you will go, from the web-page you will need to post your changes either using an HTML form or AJAX , handle the request on server-side at application level and then on page load and/or the callback of your request handle the state of your page. 无论哪种方式,您都可以从网页上使用HTML表单AJAX发布更改,在应用程序级别在服务器端处理请求,然后在页面加载和/或请求句柄的回调上处理请求您的页面状态。 If your user is able to change the state of the pages for other users, then you will need server-side in your scenario, or if you want to persist the data accross different computers/browsers for the same user. 如果您的用户能够更改其他用户的页面状态,则您需要在方案中使用服务器端,或者如果要为同一用户在不同计算机/浏览器中保留数据。

EDIT 编辑

As for your particular case, you can do something like this: 至于你的具体情况,你可以这样做:

localStorage.setItem("ProgramState", "on");

and then on page load, you will be able to set the state of your ui, using the localStorage.getItem("ProgramState") === "on" formula 然后在页面加载时,您将能够使用localStorage.getItem("ProgramState") === "on"公式设置您的ui的状态

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

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