javascript - 使用切换开关而不是按钮或链接制作 bootstrap 4.x nav pills make it switch

标签 javascript html css

我想制作 boostrap nav tab pill,但我不想使用 nav pills 按钮,而是想将其设为拨动开关

到目前为止,我的代码包含用于切换内容的按钮/药丸,但我无法将其设为开关

<ul class="nav nav-pills mb-3" id="pills-tab" role="tablist">
  <li class="nav-item">
    <a class="nav-link active" id="pills-home-tab" data-toggle="pill" href="#pills-home" role="tab" aria-controls="pills-home" aria-selected="true">Home</a>
  </li>
  <li class="nav-item">
    <a class="nav-link" id="pills-profile-tab" data-toggle="pill" href="#pills-profile" role="tab" aria-controls="pills-profile" aria-selected="false">Profile</a>
  </li>
  <li class="nav-item">
    <a class="nav-link" id="pills-contact-tab" data-toggle="pill" href="#pills-contact" role="tab" aria-controls="pills-contact" aria-selected="false">Contact</a>
  </li>
</ul>
家 轮廓 接触

把一些药丸按钮改成开关

最佳答案

您可以通过 jQuery toggle 实现此目的,以显示 this 值并使用 siblings("class").hide() 隐藏 sibling > 下面是相同的工作片段

$(document).ready(function() {
  $('input[type="checkbox"]').click(function() {
    $('input[type="checkbox"]').not(this).prop("checked", false);
    var inputValue = $(this).attr("value");
    $("." + inputValue).toggle().siblings(".box").hide()
  });
});
.box {
  color: #fff;
  padding: 20px;
  display: none;
  margin-top: 20px;
}

.red {
  background: #ff0000;
  display: none
}

.green {
  background: #228B22;
  display: none
}

.blue {
  background: #0000ff;
  display: block;
}

.switch {
  position: relative;
  display: inline-block;
  width: 60px;
  height: 34px;
}

.switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.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: #2196F3;
}

input:focus+.slider {
  box-shadow: 0 0 1px #2196F3;
}

input:checked+.slider:before {
  -webkit-transform: translateX(26px);
  -ms-transform: translateX(26px);
  transform: translateX(26px);
}

.slider.round {
  border-radius: 34px;
}

.slider.round:before {
  border-radius: 50%;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
  <label class="switch">
            <input type="checkbox" name="colorCheckbox" value="blue" checked>
            <span class="slider round"></span>
        </label>
  <label class="switch">
            <input type="checkbox" name="colorCheckbox" value="green">
            <span class="slider round"></span>
        </label>
  <label class="switch">
            <input type="checkbox" name="colorCheckbox" value="red">
            <span class="slider round"></span>
        </label>
</div>

<div class="red box">You have selected <strong>red checkbox</strong> so i am here</div>
<div class="green box">You have selected <strong>green checkbox</strong> so i am here</div>
<div class="blue box">You have selected <strong>blue checkbox</strong> so i am here</div>

关于javascript - 使用切换开关而不是按钮或链接制作 bootstrap 4.x nav pills make it switch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56785170/

相关文章:

html - 什么时候使用&lt;!--[if !IE]>?

javascript - node.js mongoError 未授权管理员执行命令

javascript - Meteor可以在不依赖外部服务的情况下运行吗?

html - 如何在 HTML 中创建一个跨越整个页面宽度的按钮?

java - 当您将鼠标悬停在链接另一个 div 上时,图像在显示 div 中发生变化

css - 动态创建一个css文件

css 按钮事件状态导致文本移动?

html - 样式不考虑没有按钮

javascript - 为什么我的 catch block 没有捕获 "Cross origin requests are only supported for protocol schemes: http..."

javascript - 分页数据表中的复选框