首页 > 解决方案 > 即使在javascript中的页面刷新时如何保留范围滑块的最后拖动位置

问题描述

我的网页由一个范围滑块组成,该滑块具有三个值 1、2、3,每个值都会在图像中发生变化,现在我的一些短语是刷新页面,范围滑块设置为我不想要的值即使在页面刷新时,我也希望它与图像和短语一起恢复上次拖动的位置我的 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;
  }
  .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;
  }
  </style>

我的 HTML 代码:

<div class="image mt-3 mb-3" id="sliderImages">
  <img src="/static/images/1.jpg" width="400" height="180"><!--Image1-->
  <img src="/static/images/2.jpg" width="400" height="180"><!--Image2-->
  <img src="/static/images/3.jpg" width="400" height="180"><!--Image3-->
</div><br><!-- End of Image sliding-->
<!--Range slider starts-->
<div class="rangeslider">
  <input type="range" min="1" max="3" value="1" class="myslider" id="sliderRange" onload="showVal(this.value)">

  <div class="container">
    <div id="sliderOutput">
      <div class="col-4" id="range_1">
        <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" id="range_2">
        <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" id="range_3">
        <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><!--End of Range slider-->

我的JS代码:

window.onload = 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('imagepath', rangeslider.getAttribute('value'));
    window.localStorage.setItem('sliderValue', (i+1));


});
}

在这里,我将值存储在本地存储中,即滑块值,但现在我想保留范围滑块的最后拖动位置以及短语和图像

       [1]: https://codepen.io/lakshmi123__/pen/abzYeLP

标签: javascriptjqueryhtml

解决方案


干得好。

您只需要在加载时调用您的 setter 方法。我所做的是,我分离了用于单独设置数据的通用代码,并在加载和值更改时调用它。这是一个更新的代码。

window.onload = 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.value = localStorageSliderNumber;
  //The common line of code extracted into a method
  setData(rangeslider, output, images, localStorageSliderNumber);
  rangeslider.addEventListener('input', function() {
    //call the method once again
    setData(rangeslider, output, images, this.value);
  });
}

function setData(rangeslider, output, images, value) {
  for (var i = 0; i < output.children.length; i++) {
    output.children[i].style.display = 'none';
    images.children[i].style.display = 'none';
  }
  i = Number(value) - 1;

  output.children[i].style.display = 'block';
  images.children[i].style.display = 'block';
  window.localStorage.setItem('imagepath', rangeslider.getAttribute('value'));
  window.localStorage.setItem('sliderValue', (i + 1));
  window.localStorage.setItem('sliderPosition', (i + 1))
}

这是更新的笔


推荐阅读