如何使用图标/图像创建自定义复选框

时间:2018-03-15 05:10:34

标签: javascript html5 css3

我需要自定义我的复选框,就像这张图片一样。我怎样才能做到这一点?请帮我这样做。enter image description here

这是我的代码。但它只是一个标签和内容。我需要在用户点击时选择这些标签。并且用户也可以取消选择此标签。这应该像复选框一样工作。

.off-white-one{
	background-color: #e4e8f2;
}
.off-white-two{
	background-color: #dde2ee;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-beta.3/css/bootstrap.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
        <div class="row mt-30">
          <a href="" class="col-sm-4 col-xs-12 wrapper-xl dk fadeInRight animated off-white-two a-block"  data-animation="fadeInRight" data-delay="500">
            <p class="text-center">
              <span class="fa fa-stack fa-2x">
                <i class="fa fa-circle fa-stack-2x text-white"></i>
                <i class="fa fa-clock-o fa-stack-1x text-info"></i>
              </span>
            </p>
            <div>
              <h3 class="text-center">Late Check-outs</h3>
            </div>
          </a>
          <a href="" class="col-sm-4 col-xs-12 wrapper-xl fadeInDown animated off-white-one a-block"  data-animation="fadeInDown" data-delay="750">
            <p class="text-center">
              <span class="fa fa-stack fa-2x">
                <i class="fa fa-circle fa-stack-2x text-white"></i>
                <i class="fa fa-tint fa-stack-1x text-info"></i>
              </span>
            </p>
            <div>
              <h3 class="text-center">Mid-Stay Cleaning</h3>
            </div>
          </a>
          <a href="" class="col-sm-4 col-xs-12 wrapper-xl dker fadeInLeft animated off-white-two a-block data-animation="fadeInLeft" data-delay="1000">
            <p class="text-center">
              <span class="fa fa-stack fa-2x">
                <i class="fa fa-circle fa-stack-2x text-white"></i>
                <i class="fa fa-plus-circle fa-stack-1x text-info"></i>
              </span>
            </p>
            <div>
              <h3 class="text-center">More towels & Sheetbeds</h3>
            </div>
          </a>
        </div>
      </div>

2 个答案:

答案 0 :(得分:1)

&#13;
&#13;
 label:before {
      content: url("https://cdn1.iconfinder.com/data/icons/windows8_icons_iconpharm/26/unchecked_checkbox.png");
      position: absolute;
      z-index: 100;
    }
    :checked+label:before {
      content: url("https://cdn1.iconfinder.com/data/icons/windows8_icons_iconpharm/26/checked_checkbox.png");
    }
    input[type=checkbox] {
      display: none;
    }
    /*pure cosmetics:*/
    img {
      width: 150px;
      height: 150px;
    }
    label {
      margin: 10px;
    }
&#13;
 


 

<input type="checkbox" id="myCheckbox1" />
    <label for="myCheckbox1">
      <img src="https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcR0LkgDZRDTgnDrzhnXGDFRSItAzGCBEWEnkLMdnA_zkIH5Zg6oag">
    </label>
    <input type="checkbox" id="myCheckbox2" />
    <label for="myCheckbox2">
      <img src="https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcRhJjGB3mQxjhI5lfS9SwXou06-2qT_0MjNAr0atu75trXIaR2d">
    </label>
    <input type="checkbox" id="myCheckbox3" />
    <label for="myCheckbox3">
      <img src="https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcQuwWbUXC-lgzQHp-j1iw56PIgl_2eALrEENUP-ld72gq3s8cVo">
    </label>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

根据您的要求设计一个示例自定义复选框, 您可以根据需要更改图像,颜色和大小。

&#13;
&#13;
$('.input_class_checkbox').each(function(){
    $(this).hide().after('<div class="class_checkbox" />');

});

$('.class_checkbox').on('click',function(){
    $(this).toggleClass('checked').prev().prop('checked',$(this).is('.checked'))
    if($(this).is('.checked')){
      $(this).addClass("clock-img");
    }else{
      $(this).removeClass("clock-img");
    }
});
&#13;
.class_checkbox {
    width: 50px;  
    height: 50px;
    
}
#circle{
  margin-left : 16px;
   border: 2px solid #aeaec5;
    background-color: white;
    width: 64px;
    height: 64px; 
    border-radius: 32px; 
}
.checkbox-container{
  background-color:#86bbf3;
  width:100px;
  height:100px;
  padding-top: 8px;
}

.clock-img{
  background:url('https://cdn0.iconfinder.com/data/icons/super-mono-basic/blue/alarm-clock_basic_blue.png') no-repeat;
}

#circle div { position:relative; left: 4px; top: 2px; width: 90px; height: 90px; color: white; text-align:center; }
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class= "checkbox-container">

  <div id="circle"><div><input type="checkbox" class="input_class_checkbox"></div></div>
  <div>
  Late check out
  </div>
</div>
&#13;
&#13;
&#13;