简体   繁体   English

如何在范围滑块的幻灯片上将文本放入本地存储?

[英]How do I get the text into local storage on slide of range slider?

I am using a range slider.我正在使用范围滑块。 On sliding, the images and phrases will change.滑动时,图像和短语会发生变化。

There are three stages of sliding.滑动分为三个阶段。 On sliding, the phrases and images change.滑动时,短语和图像会发生变化。 On every change, that phrase and image should save to local storage.每次更改时,该短语和图像都应保存到本地存储。 That is the problem.那就是问题所在。

My html code is:我的html代码是:

<div class="image mt-3 mb-3" id="sliderImages">
          <img src="../static/images/1.jpg" width="400" height="180">
          <img src="../static/images/2.jpg" width="400" height="180">
          <img src="../static/images/3.jpg" width="400" height="180">
        </div><br>
        <div class="rangeslider">
          <input type="range" min="1" max="3" value="1" class="myslider" id="sliderRange">
          <div class="container">
          <div id="sliderOutput">
            <div class="col-4">

              <h6 class="display-6 mt-3"><b><center>Starting From Scratch</center></b></h6>
              <p class="demo"><center>I'm designing the room </p>
            </div>
            <div class="col-4">
              <h6 class="display-6 mt-3"><b>Somewhere in Between</b></h6>
              <p class="demo">I'm designing around a few pieces I already own</p>
            </div>
            <div class="col-4">
              <h6 class="display-6 mt-3"><b>Mostly Furnished</b></h6>
              <p class="demo">I want to put the finishing touches on my room</p>
            </div>
          </div>
        </p>

My CSS code is:我的 CSS 代码是:

<style>
        .rangeslider {
          width: 50%;
          margin: 0 auto;
          position: absolute;
        }
        .myslider {
          -webkit-appearance: none;
          background: white;
          width: 100%;
          height: 20px;
          opacity: 0.8;
          margin-top: 180px;
        }
        .myslider::-webkit-slider-thumb {
          -webkit-appearance: none;
          cursor: pointer;
          background: #000080;
          width: 33%;
          height: 20px;
        }
        .col-4 {
          text-align: center;
        }
        .myslider:hover {
          opacity: 1;
        }
        .image {
          position: relative;
          width: 400px;
          margin: 0 auto;
        }
        .image>img {
          position: absolute;
          display: none;
        }
        .image>img.visible,
        .image>img:first-child {
          display: block;
        }
        #sliderOutput>div {
          display: none;
        }
        #sliderOutput>div.visible,
        #sliderOutput>div:first-child {
          display: block;
        }
        #p1{
          height: 10px;
        }
      </style>

My JS Code is:我的JS代码是:

<script>
        window.addEventListener('load', function() {
        var rangeslider = document.getElementById("sliderRange");
        var output = document.getElementById("sliderOutput");
        var images = document.getElementById("sliderImages");
        rangeslider.addEventListener('input', function() {
          for (var i = 0; i < output.children.length; i++) {
            output.children[i].style.display = 'none';
            images.children[i].style.display = 'none';
          }
          i = Number(this.value) - 1;
          output.children[i].style.display = 'block';
          images.children[i].style.display = 'block';
        });
      });
    </script>

在此处输入图片说明

On sliding, the image and phrase will change.滑动时,图像和短语会发生变化。 My requirement on changing the phrase and image path should store in the local storage.我对更改短语和图像路径的要求应存储在本地存储中。

Can you people please help me?你们能帮帮我吗?

Put the last stored image path and slider number in localStorage and use it将上次存储的图片路径和滑块编号放入localStorage并使用

<script>
    window.addEventListener('load', function() {

    var imagePath = "../static/images/";
    var localStorageSliderNumber;
    var localStorageImagePath; 
    if (window.localStorage.getItem('sliderValue') != null) {
        localStorageSliderNumber = 
            window.localStorage.getItem('sliderValue');
    } else {
        window.localStorage.setItem('sliderValue', '1');
        localStorageSliderNumber = 1;
    }
    if (window.localStorage.getItem('imagePath') != null) {
        imagePath = imagePath + window.localStorage.getItem('imagePath') + ".jpg";
    }

    var rangeslider = document.getElementById("sliderRange");
    var output = document.getElementById("sliderOutput");
    var images = document.getElementById("sliderImages");
    rangeslider.addEventListener('input', function() {
      for (var i = 0; i < output.children.length; i++) {
        output.children[i].style.display = 'none';
        images.children[i].style.display = 'none';
      }
      i = Number(this.value) - 1;
      output.children[i].style.display = 'block';
      images.children[i].style.display = 'block';

      window.localStorage.setItem('sliderValue', rangeslider.getAttribute('value'));
      window.localStorage.setItem('imagePath', (i+1));
    });
  });
</script>

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

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