简体   繁体   English

在HTML和JavaScript中添加两种不同类型的滑块

[英]Adding two different types of slider in HTML and JavaScript

I initially have one type of slider already present in my code to switch on/off my device, I wish to add another range slider to vary duration of ON time but when I do so all my slider turn to range slider, is there some other way to do so, since I'm using both HTML and JavaScript to fetch the user input and let the device act accordingly. 最初,我的代码中已经存在一种类型的滑块,用于打开/关闭设备,我希望添加另一种范围滑块来改变开启时间,但是当我这样做时,我所有的滑块都变成了范围滑块,还有其他吗?这样做的方法,因为我同时使用HTML和JavaScript来获取用户输入并让设备采取相应措施。

This is my slider code: 这是我的滑块代码:

 .switch { position: relative; display: inline-block; width: 100px; height: 34px; } .switch input { display: none; } .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: 26px; width: 26px; left: 4px; bottom: 4px; background-color: white; -webkit-transition: .4s; transition: .4s; } input:checked+.slider { background-color: green; } input:focus+.slider { box-shadow: 0 0 1px green; } input:checked+.slider:before { -webkit-transform: translateX(66px); -ms-transform: translateX(66px); transform: translateX(66px); } .button { background-color: #4CAF50; /* Green */ border: none; color: white; padding: 15px 32px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; width: 200; } /* this is another slider which i wish to add: */ .slidecontainer { width: 100%; } .slider { -webkit-appearance: none; width: 100%; height: 15px; border-radius: 5px; background: #d3d3d3; outline: none; opacity: 0.7; -webkit-transition: .2s; transition: opacity .2s; } .slider:hover { opacity: 1; } .slider::-webkit-slider-thumb { -webkit-appearance: none; appearance: none; width: 25px; height: 25px; border-radius: 50%; background: #4CAF50; cursor: pointer; } .slider::-moz-range-thumb { width: 25px; height: 25px; border-radius: 50%; background: #4CAF50; cursor: pointer; } 
 <tr height=40> <td> <style="text-align:middle">PORT1</td> <td width=40></td> <td><label id="sw1" , class="switch"> <input type="checkbox"> <span class="slider"></span> </label></td> <td> <div class="slidercontainer"> <input type="range" min="10" max="120" value="20" class="slider" id="pulse_time"> <p>Time:<span id="P1_PT"></span></p> </div> </td> </tr> 

you can do this to show slider if first is checked, you'll need to review css 您可以执行此操作以显示滑块(如果首先选中),则需要查看CSS

 function changeVisibility(e) { if (e.target.checked) { document.getElementById("onlyShowIfYes").style.visibility = "visible" } else { document.getElementById("onlyShowIfYes").style.visibility = "hidden" } } document.getElementById("check").onchange = changeVisibility changeVisibility({target: {checked: false}}) 
 .switch { position: relative; display: inline-block; width: 100px; height: 34px; } .switch input { display: none; } .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: 26px; width: 26px; left: 4px; bottom: 4px; background-color: white; -webkit-transition: .4s; transition: .4s; } input:checked+.slider { background-color: green; } input:focus+.slider { box-shadow: 0 0 1px green; } input:checked+.slider:before { -webkit-transform: translateX(66px); -ms-transform: translateX(66px); transform: translateX(66px); } .button { background-color: #4CAF50; /* Green */ border: none; color: white; padding: 15px 32px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; width: 200; } /* this is another slider which i wish to add: */ .slidecontainer { width: 100%; } .slider { -webkit-appearance: none; width: 100%; height: 15px; border-radius: 5px; background: #d3d3d3; outline: none; opacity: 0.7; -webkit-transition: .2s; transition: opacity .2s; } .slider:hover { opacity: 1; } .slider::-webkit-slider-thumb { -webkit-appearance: none; appearance: none; width: 25px; height: 25px; border-radius: 50%; background: #4CAF50; cursor: pointer; } .slider::-moz-range-thumb { width: 25px; height: 25px; border-radius: 50%; background: #4CAF50; cursor: pointer; } 
 <tr height=40> <td> <style="text-align:middle">PORT1</td> <td width=40></td> <td><label id="sw1" , class="switch"> <input id="check" type="checkbox"> <span class="slider"></span> </label></td> <td> <div id="onlyShowIfYes" class="slidercontainer"> <input type="range" min="10" max="120" value="20" class="slider" id="pulse_time"> <p>Time:<span id="P1_PT"></span></p> </div> </td> </tr> 

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

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