點選開關顯示或者隱藏input文字框

antzone發表於2017-04-17

這種需求在應用中也是比較常見的,只是形式上的不同而已,實質是大同小異。

下面就分享一個這樣的程式碼,點選右側的開關按鈕可以實現底部文字框的顯示或者隱藏。

程式碼例項如下:

[HTML] 純文字檢視 複製程式碼執行程式碼
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="author" content="http://www.softwhy.com/" />
<title>螞蟻部落</title>
<style>
input[type="checkbox"].iswitch {
  font-size: 10px;
  position: relative;
  display: inline-block;
  width: 38px;
  height: 22px;
  line-height: 22px;
  border-radius: 11px;
  padding: 0;
  box-shadow: inset 0 0 0 1px #a4a4a4;
  outline: 1px solid transparent !important;
  cursor: pointer;
  border: none;
  background: #cccccc;
  user-select: none;
  -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
  -webkit-touch-callout: none;
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
  margin-right: 4px;
  margin-bottom: 7px;
  transition: box-shadow 0.3s ease-in-out, padding 0.25s ease-in-out;
  transition-delay: .1s, 0s;
  /* Animations if supported */
}
input[type="checkbox"].iswitch:checked {
  transition-delay: 0s, 0s;
  box-shadow: inset 0 0 0 12.57142857px #ccc !important;
  padding-left: 16px;
}
input[type="checkbox"].iswitch.iswitch-secondary:checked {
  box-shadow: inset 0 0 0 12.57142857px #5cae6c !important;
}
input[type="checkbox"].iswitch:before,
input[type="checkbox"].iswitch:after {
  content: "";
}
input[type="checkbox"].iswitch:after {
  position: absolute;
  top: -6px;
  left: -6px;
  bottom: -6px;
  right: -6px;
}
input[type="checkbox"].iswitch:before {
  display: inline-block;
  height: 18px;
  width: 18px;
  margin: 2px 0 0 2px;
  background-color: #ffffff;
  border-radius: 9px;
  box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.2), 0 0 1px 1px rgba(0, 0, 0, 0.1);
  -webkit-transition: all .1s ease .1s;
  -moz-transition: all .1s ease .1s;
  -o-transition: all .1s ease .1s;
  transition: all .1s ease .1s;
}
::-webkit-input-placeholder {
  color: #707070;
}
::-moz-placeholder {
  color: #707070;
}
:-moz-placeholder {
  color: #707070;
}
.section-other {
  padding: 22px 5% 0px;
}
.check-input {
  margin-top: -3px;
  float: right;
}
.other-charge {
  color: #575757;
  font-size: 15px;
}
.other-middle {
  border: 1px dashed #ccc;
  background: #fff;
  padding: 14px 14px 0;
  position: relative;
  margin-top: 20px;
}
.other-middle:before {
  position: absolute;
  content: " ";
  border-top: 1px dashed #ccc;
  border-right: 1px dashed #ccc;
  background: #fff;
  -webkit-transform: rotate(-45deg);
  transform: rotate(-45deg);
  left: 31px;
  top: -6px;
  width: 10px;
  height: 10px;
}
.input_div {
  border: #c9c9c9 solid 1px;
  border-radius: 5px;
  background: #fff;
  padding: 7px 0;
  box-sizing: border-box;
  margin-bottom: 14px;
}
.input_div input {
  width: 100%;
  height: 20px;
  background-color: transparent;
  color: #575757;
  vertical-align: middle;
  font-size: 13px;
  line-height: 20px;
  padding-left: 10px;
  box-sizing: border-box;
  outline: none;
  border: none;
}
</style>
<script>
function mySwitch() {
  var other_middle=document.getElementById('other-middle');
  var other_middle=document.getElementById('other-middle');
  other_middle.style.display = other_middle.style.display == 'none' ? 'block' : 'none';
}
window.onload = function () {
  var isreceipt = document.getElementById("isreceipt");
  isreceipt.onclick = function () {
    mySwitch();
  }
}
</script>
</head>
<body>
  <div class="section-other">
    <div class="other-top">
      <span class="other-charge">選擇按鈕:</span>
      <input name="isreceipt" id="isreceipt" type="checkbox" class="iswitch iswitch-secondary check-input">
    </div>
    <div class="other-middle" id="other-middle" style="display:none">
      <div class="input_div">
        <input type="text" placeholder="請輸入姓名">
      </div>
      <div class="input_div">
        <input type="text" placeholder="請輸入賬號">
      </div>
    </div>
  </div>
</body>
</html>

相關文章