javascript - 在javascript中隐藏 slider 的拇指

标签 javascript html css slider

我想在 javascript 中再次显示之前隐藏一个 slider 的拇指(这甚至是个好词吗?我会假设它是)。基本上,这个想法是在用户点击 slider 之前不显示它,以免他们的答案产生偏差。

对于 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;
}

我可以让 slider 的拇指消失。但是您将如何在 JavaScript 中实现这一点。

提前致谢。

最佳答案

事实证明你不能直接在 JS 中执行此操作。但一个简单的方法是使用两个不同的 CSS 类并使用 JS 使 slider 更改类。

这是部分解决方案

    /* 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"

关于javascript - 在javascript中隐藏 slider 的拇指,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54539876/

相关文章:

javascript - jQuery 选择选项不适用于尚未添加到 DOM 的选择

javascript - AND 多个参数

html - CSS图像缩放而不被裁剪

html - 制作响应式 Bootstrap 导航(对齐问题)

css - 未正确使用样式表

javascript - 使用 Google 云端硬盘图像替换 Google 文档表格单元格中的占位符文本标签

javascript - 如何在 vue js 中将从 firebase 检索到的数据显示到标题标记 <h1>{{ }}</h1> ?

html - 在HTML中包含YouTube视频播放器

javascript - 函数已定义但仍未定义

html - header 容器大于横幅图像