在JavaScript中隐藏滑块的拇指

时间:2019-02-05 17:22:50

标签: javascript html css slider

我想使滑块的拇指(这是否是个好词?我会假设它是隐藏的)暂时隐藏一会儿,然后再用javascript显示。基本上,这个想法是在用户单击滑块之前不显示它,以免偏颇他们的答案。

对于MWE,我认为这是一个不错的选择。 https://www.w3schools.com/howto/tryit.asp?filename=tryhow_css_js_rangeslider

在此步骤中,通过更改:

.slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 25px;
  height: 25px;
  background: #4CAF50;
  cursor: pointer;
}

进入

.slider::-webkit-slider-thumb {
   -webkit-appearance: none;
   appearance: none;
   width: 25px;
   height: 25px;
   background: #4CAF50;
   cursor: pointer;
   display: none;
}

我可以使滑块的拇指消失。但是您将如何在javascript中实现这一目标。

谢谢。

1 个答案:

答案 0 :(得分:0)

原来,您不能直接在JS中执行此操作。但是一种简单的方法是使用两个不同的CSS类,然后使用JS来更改滑块。

这是部分解决方案

    /* The slider itself */
.slider {
    -webkit-appearance: none;  /* Override default CSS styles */
    appearance: none;
    width: 25%; /* Full-width */
    height: 10px; /* Specified height */
    background: #d3d3d3; /* Grey background */
    outline: none; /* Remove outline */
    opacity: 0.7; /* Set transparency (for mouse-over effects on hover) */
    -webkit-transition: .2s; /* 0.2 seconds transition on hover */
    transition: opacity .2s;
}

/* Mouse-over effects */
.slider:hover {
    opacity: 1; /* Fully shown on mouse-over */
}




/* The slider handle (use -webkit- (Chrome, Opera, Safari, Edge) and -moz- (Firefox) to override default look) */ 
.slider::-webkit-slider-thumb {
    -webkit-appearance: none; /* Override default look */
    appearance: none;
    width: 25px; /* Set a specific slider handle width */
    height: 25px; /* Slider handle height */
    background: #4CAF50; /* Green background */
    cursor: pointer; /* Cursor on hover */
}

.slider::-moz-range-thumb {
    width: 25px; /* Set a specific slider handle width */
    height: 25px; /* Slider handle height */
    background: #4CAF50; /* Green background */
    cursor: pointer; /* Cursor on hover */
}


/*An other class to make the thumb of the slider invisible*/
.slider2 {
    -webkit-appearance: none;  /* Override default CSS styles */
    appearance: none;
    width: 25%; /* Full-width */
    height: 10px; /* Specified height */
    background: #d3d3d3; /* Grey background */
    outline: none; /* Remove outline */
    opacity: 0.7; /* Set transparency (for mouse-over effects on hover) */
    -webkit-transition: .2s; /* 0.2 seconds transition on hover */
    transition: opacity .2s;
}

/* Mouse-over effects */
.slider2:hover {
    opacity: 1; /* Fully shown on mouse-over */
}




/* The slider handle (use -webkit- (Chrome, Opera, Safari, Edge) and -moz- (Firefox) to override default look) */ 
.slider2::-webkit-slider-thumb {
    -webkit-appearance: none; /* Override default look */
    appearance: none;
    width: 25px; /* Set a specific slider handle width */
    height: 25px; /* Slider handle height */
    background: #4CAF50; /* Green background */
    cursor: pointer; /* Cursor on hover */
    visibility: hidden;
}

.slider2::-moz-range-thumb {
    width: 25px; /* Set a specific slider handle width */
    height: 25px; /* Slider handle height */
    background: #4CAF50; /* Green background */
    cursor: pointer; /* Cursor on hover */
}

visibility: hidden;通知slider2

然后使用js更改类,例如:

document.getElementbyId("slider").className = "slider2"