【问题标题】:How can I display the checkbox over the images for selection?如何在图像上显示复选框以供选择?
【发布时间】:2012-02-11 06:20:24
【问题描述】:

我想在每张图片的右下角显示一个复选框以供选择..

我该怎么做?

请记住,单击图像有一个独特的事件(我会设法做到!),但单击该复选框应该选择/取消选择那个?

【问题讨论】:

  • 我认为解决这个问题的正确方法是,通过div 并将back-ground images 放在它上面,在每个div 内你可以有复选框..
  • 我也是这么想的,但我不知道如何使用css(即如何为此构造html)!任何人都可以提供 html/css 来显示此单张图片吗?

标签: html css


【解决方案1】:

这可以使用纯 CSS 来完成,假设您为所有图像设置了固定的宽度和高度。诀窍是为复选框设置绝对位置,然后将bottomright 分配为零。

HTML 示例:

<div class="container">
    <img src="image1.jpg" /> 
    <input type="checkbox" class="checkbox" id="check1" />
</div>
<div class="container">
    <img src="image2.jpg" />
    <input type="checkbox"  class="checkbox" id="check2" />
</div>

CSS:

.container { position: relative; width: 100px; height: 100px; float: left; margin-left: 10px; }
.checkbox { position: absolute; bottom: 0px; right: 0px; }

Live test case.

至于点击事件,只需将点击处理程序应用于每个复选框,它就可以正常工作。请参阅this updated fiddle

【讨论】:

    【解决方案2】:

    您可以使用 "label" 标签,其中包含 "img" 标签。

    这是一个使用图像作为复选框和单选框的示例。

    * {
      font-family: Lato;
      margin: 0;
      padding: 0;
      --transition: 0.15s;
      --border-radius: 0.5rem;
      --background: #ffc107;
      --box-shadow: #ffc107;
    }
    
    .cont-bg {
      min-height: 100vh;
      background-image: radial-gradient(
        circle farthest-corner at 7.2% 13.6%,
        rgba(37, 249, 245, 1) 0%,
        rgba(8, 70, 218, 1) 90%
      );
      padding: 1rem;
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
    }
    
    .cont-title {
      color: white;
      font-size: 1.25rem;
      font-weight: 600;
      margin-bottom: 1rem;
    }
    
    .cont-main {
      display: flex;
      flex-wrap: wrap;
      align-content: center;
      justify-content: center;
    }
    
    .cont-checkbox {
      width: 150px;
      height: 100px;
      border-radius: var(--border-radius);
      box-shadow: 0 0.125rem 0.25rem rgba(0, 0, 0, 0.075);
      background: white;
      transition: transform var(--transition);
    }
    
    .cont-checkbox:first-of-type {
      margin-bottom: 0.75rem;
      margin-right: 0.75rem;
    }
    
    .cont-checkbox:active {
      transform: scale(0.9);
    }
    
    input {
      display: none;
    }
    
    input:checked + label {
      opacity: 1;
      box-shadow: 0 0 0 3px var(--background);
    }
    
    input:checked + label img {
      -webkit-filter: none; /* Safari 6.0 - 9.0 */
      filter: none;
    }
    
    input:checked + label .cover-checkbox {
      opacity: 1;
      transform: scale(1);
    }
    
    input:checked + label .cover-checkbox svg {
      stroke-dashoffset: 0;
    }
    
    label {
      display: inline-block;
      cursor: pointer;
      border-radius: var(--border-radius);
      overflow: hidden;
      width: 100%;
      height: 100%;
      position: relative;
      opacity: 0.6;
    }
    
    label img {
      width: 100%;
      height: 70%;
      object-fit: cover;
      clip-path: polygon(0% 0%, 100% 0, 100% 81%, 50% 100%, 0 81%);
      -webkit-filter: grayscale(100%); /* Safari 6.0 - 9.0 */
      filter: grayscale(100%);
    }
    
    label .cover-checkbox {
      position: absolute;
      right: 5px;
      top: 3px;
      z-index: 1;
      width: 20px;
      height: 20px;
      border-radius: 50%;
      background: var(--box-shadow);
      border: 2px solid #fff;
      transition: transform var(--transition),
        opacity calc(var(--transition) * 1.2) linear;
      opacity: 0;
      transform: scale(0);
    }
    
    label .cover-checkbox svg {
      width: 13px;
      height: 11px;
      display: inline-block;
      vertical-align: top;
      fill: none;
      margin: 5px 0 0 3px;
      stroke: #fff;
      stroke-width: 2;
      stroke-linecap: round;
      stroke-linejoin: round;
      stroke-dasharray: 16px;
      transition: stroke-dashoffset 0.4s ease var(--transition);
      stroke-dashoffset: 16px;
    }
    
    label .info {
      text-align: center;
      margin-top: 0.2rem;
      font-weight: 600;
      font-size: 0.8rem;
    }
    <div class="cont-bg">
      <div class="cont-title">Checkbox</div>
      <div class="cont-main">
        <div class="cont-checkbox">
          <input type="checkbox" id="myCheckbox-1" />
          <label for="myCheckbox-1">
            <img
              src="https://hips.hearstapps.com/hmg-prod.s3.amazonaws.com/images/2021-mazda-mx-5-miata-mmp-1-1593459650.jpg?crop=0.781xw:0.739xh;0.109xw,0.0968xh&resize=480:*"
            />
            <span class="cover-checkbox">
              <svg viewBox="0 0 12 10">
                <polyline points="1.5 6 4.5 9 10.5 1"></polyline>
              </svg>
            </span>
            <div class="info">Mazda MX-5 Miata</div>
          </label>
        </div>
        <div class="cont-checkbox">
          <input type="checkbox" id="myCheckbox-2" />
          <label for="myCheckbox-2">
            <img
              src="https://hips.hearstapps.com/hmg-prod.s3.amazonaws.com/images/2020-chevrolet-corvette-c8-102-1571146873.jpg?crop=0.548xw:0.411xh;0.255xw,0.321xh&resize=980:*"
            />
            <span class="cover-checkbox">
              <svg viewBox="0 0 12 10">
                <polyline points="1.5 6 4.5 9 10.5 1"></polyline>
              </svg>
            </span>
            <div class="info">Toyota Supra</div>
          </label>
        </div>
      </div>
      <div class="cont-title">Radio</div>
      <div class="cont-main">
        <div class="cont-checkbox">
          <input type="radio" name="myRadio" id="myRadio-1" />
          <label for="myRadio-1">
            <img
              src="https://hips.hearstapps.com/hmg-prod.s3.amazonaws.com/images/2021-mazda-mx-5-miata-mmp-1-1593459650.jpg?crop=0.781xw:0.739xh;0.109xw,0.0968xh&resize=480:*"
            />
            <span class="cover-checkbox">
              <svg viewBox="0 0 12 10">
                <polyline points="1.5 6 4.5 9 10.5 1"></polyline>
              </svg>
            </span>
            <div class="info">Mazda MX-5 Miata</div>
          </label>
        </div>
        <div class="cont-checkbox">
          <input type="radio" name="myRadio" id="myRadio-2" />
          <label for="myRadio-2">
            <img
              src="https://hips.hearstapps.com/hmg-prod.s3.amazonaws.com/images/2020-chevrolet-corvette-c8-102-1571146873.jpg?crop=0.548xw:0.411xh;0.255xw,0.321xh&resize=980:*"
            />
            <span class="cover-checkbox">
              <svg viewBox="0 0 12 10">
                <polyline points="1.5 6 4.5 9 10.5 1"></polyline>
              </svg>
            </span>
            <div class="info">Toyota Supra</div>
          </label>
        </div>
      </div>
    </div>

    【讨论】:

      【解决方案3】:

      如果您只要求选择/取消选择复选框,那么我的建议如下:

      第 1 步: 将图像和复选框放在一个块内(可以是 div 或表格)
      第 2 步:为该块提供具有特定高度和宽度的相对位置。
      第 3 步: 对于复选框,为其指定绝对位置并设置右侧和底部间隙(根据您的要求)。


      例如,代码应该如下所示

          <div class="img_block">
                      <img src="image-path" alt="" />
              <input type="checkbox" class="chkbox" />
          </div>
          <div class="img_block">
              <img src="image-path" alt="" />
              <input type="checkbox" class="chkbox" />
          </div> 
      

      同样的 css

      .img_block {位置:相对;宽度:230px;边距右:20px;边距底部:10px;高度:30px;}
      .chkbox {位置:绝对;右:5px;底部:3px;}

      我希望这符合您的要求。

      【讨论】:

        【解决方案4】:

        如果是我,我会尝试为此使用 jQuery。让我们假设每个图像都在每个自己的 div 中,并且 checkbo 的 id 为 check:

          $('div').click(function()
        
            { 
                 //you select the checkbox and ad the code to do what you want to it
        
               ('#check').
        
            });
        

        【讨论】:

        • 你想说什么?不完整的答案。问题是关于在图像上显示一个复选框。
        猜你喜欢
        • 2021-05-19
        • 1970-01-01
        • 1970-01-01
        • 2020-04-02
        • 1970-01-01
        • 2019-08-28
        • 2013-04-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多