【问题标题】:How do I style radio buttons with images - laughing smiley for good, sad smiley for bad?我如何为带有图像的单选按钮设置样式 - 笑的笑脸是好的,悲伤的笑脸是坏的?
【发布时间】:2011-04-23 05:00:03
【问题描述】:

我想为用户反馈创建一个 HTML 表单。 如果总体反馈良好,用户应该点击笑脸,如果总体反馈不好,用户应该选择悲伤的笑脸。

我认为这应该使用单选按钮来完成,用笑脸代替单选按钮。也许我错了……

你知道我是怎么做到的吗?

谢谢!

【问题讨论】:

标签: html css forms


【解决方案1】:

让我们保持简单,好吗?首先,使用纯 HTML + CSS:

<div id="emotion">
    <input type="radio" name="emotion" id="sad" />
        <label for="sad"><img src="sad_image.png" alt="I'm sad" /></label>

    <input type="radio" name="emotion" id="happy" />
        <label for="happy"><img src="happy_image.png" alt="I'm happy" /></label>
</div>

如果没有 JavaScript,这会很好地降级。使用id 和for 属性将标签和单选按钮链接起来,这样当图像被选中时,相应的单选按钮就会被填充。这很重要,因为我们需要使用 JavaScript 隐藏实际的单选按钮。现在来看一些 jQuery 的优点。首先,创建我们需要的 CSS:

.input_hidden {
    position: absolute;
    left: -9999px;
}

.selected {
    background-color: #ccc;
}

#emotion label {
    display: inline-block;
    cursor: pointer;
}

#emotion label img {
    padding: 3px;
}

现在是 JavaScript:

$('#emotion input:radio').addClass('input_hidden');
$('#emotion label').click(function(){
    $(this).addClass('selected').siblings().removeClass('selected');
});

我们在这里不使用display: none 的原因是出于可访问性的原因。请参阅:http://www.jsfiddle.net/yijiang/Zgh24/1 进行现场演示,其中包含更多花哨的东西。

【讨论】:

  • 您似乎在这里唯一缺少的是在单击相应图像时设置相应输入的 checked 值。
  • @Rob 点击labels 将检查适当的inputs - 不需要JavaScript。你可以注释掉第一行 JavaScript 代码来查看。
  • 啊,你是对的!谢谢。我稍微修改了您的示例以使图像成为可点击的东西,这就是我看到这种行为的原因。我的错。
  • JavaScript 不再需要。 input[type=radio]:checked + label&gt;img 将选择图像就好了。见:codepen.io/DavidBradbury/pen/HuCqx
【解决方案2】:

您可以利用 CSS3 来做到这一点,通过使用 CSS3 规则隐藏 默认 输入单选按钮:

.class-selector input{
    margin:0;padding:0;
    -webkit-appearance:none;
       -moz-appearance:none;
            appearance:none;
}

然后使用图像标签作为以下演示:

JSFiddle Demo 1

JSFiddle Demo 2

Gist - How to use images for radio-buttons

【讨论】:

  • 很好的解决方案,但是如何使它与响应式设计一起工作。在 css 中我看到 .drinkcard-cc - 宽度和高度固定..
  • 抱歉,回答延迟。只需将它们包装到响应式容器中,然后将宽度或高度设置为 100%。
  • 嗨 @RichardCotrina 有没有办法让它与 IE 兼容?
  • @Raulnd 可能将display:none; 设置为&lt;input&gt;。我现在无法测试 IE 兼容性。请让我知道它是否有效:)
  • @RichardCotrina 找到了!不透明度:0.4;过滤器:alpha(opacity=40);
【解决方案3】:

这是一个纯 HTML+CSS 的解决方案。

HTML:

<div class="image-radio">
  <input type="radio" value="true" checked="checked" name="ice_cream" id="ice_cream_vanilla">
  <label for="ice_cream_vanilla">Vanilla</label>
  <input type="radio" value="true" name="ice_cream" id="ice_cream_chocolate">
  <label for="ice_cream_chocolate">Chocolate</label>
</div>

SCSS:

  // use an image instead of the native radio widget
.image-radio {   
  input[type=radio] {
    display: none;
  }
  input[type=radio] + label {
    background: asset-url('icons/choice-unchecked.svg') no-repeat left;
    padding-left: 2rem;
  }
  input[type=radio]:checked + label {
    background: asset-url('icons/choice-checked.svg') no-repeat left;
  }
}

【讨论】:

    【解决方案4】:

    使用纯 html(无 JS),您不能真正用单选按钮代替图像(至少,我认为您不能)。您可以使用以下方法与用户建立相同的连接:

    <form action="" method="post">
        <fieldset>
           <input type="radio" name="feeling" id="feelingSad" value="sad" /><label for="feelingSad"><img src="path/to/sad.png" /></label>
           <label for="feelingHappy"><input type="radio" name="feeling" id="feelingHappy" value="happy" /><img src="path/to/happy.png" /></label>
        </fieldset>
    </form>
    

    【讨论】:

    • 将图像包裹在label 元素中以获得更多语义上的好处,并自动制作按钮。此外,alt 属性是强制,因此您也需要在img 标签上使用这些属性。
    • 这就是我匆匆忙忙的结果……感谢@Yi 和@cherouvim,你们说的都对。
    【解决方案5】:

    我已经编辑了之前的一篇文章。现在,它变得更加简单并且完美运行。

    <input style="position: absolute;left:-9999px;" type="radio" name="emotion" id="sad" />
    <label for="sad"><img src="red.gif" style="display: inline-block;cursor: pointer;padding: 3px;" alt="I'm sad" /></label>
    
    <input  style="position: absolute;left:-9999px;" type="radio" name="emotion" id="happy" />
    <label for="happy"><img src="blue.gif" style="display: inline-block;cursor: pointer;padding: 3px;" alt="I'm happy" /></label>
    

    【讨论】:

    • 在其周围放置一个 div,您就可以摆脱“标签位置无效”的消息。短而甜。
    【解决方案6】:

    您根本无法设置诸如单选按钮、复选框、滚动条(等等)之类的样式。这些是操作系统和浏览器原生的,不是您可以操纵的。

    您可以通过隐藏单选按钮并仅显示图像来模拟这一点。

    <input type="radio" style="display: none;" id="sad" /><label for="sad"><img class="sad_image" /></label>
    

    【讨论】:

      【解决方案7】:

      使用 jQuery。隐藏您的复选框元素并创建如下列表:

      <ul id="list">
          <li><a href="javascript:void(0)" id="link1">Happy face</a></li>
          <li><a href="javascript:void(0)" id="link2">Sad face</a></li>
      </ul>
      
      <form action="file.php" method="post">
          <!-- More code -->
          <input type="radio" id="option1" name="radio1" value="happy" style="display:none"/>
          <input type="radio" id="option2" name="radio1" value="sad" style="display:none"/>
          <!-- More code -->
      </form>
      
      <script type="text/javascript">
      $("#list li a").click(function() {
          $('#list .active').removeClass("active");
          var id = this.id;
          var newselect = id.replace('link', 'option');
          $('#'+newselect).attr('checked', true);
          $(this).addClass("active").parent().addClass("active");
          return false;
      });
      </script>
      

      此代码将在后台将 checked 属性添加到您的无线电输入,并将类 active 分配给您的列表元素。当然不要使用内联样式,不要忘记包含 jQuery,自定义后一切都应该立即可用。

      干杯!

      【讨论】:

      • 对我来说很好 - 谢谢你,加一个。
      【解决方案8】:

      另一种选择是使用表单替换脚本/库。 它们通常隐藏原始元素并用 div 或 span 替换它们,您可以按照自己喜欢的方式设置样式。

      例如:

      http://customformelements.net(基于 mootools) http://www.htmldrive.net/items/show/481/jQuery-UI-Radiobutton-und-Checkbox-Replacement.html

      【讨论】:

        【解决方案9】:

        面对同样的问题我创建了一个简单的jQuery插件http://xypaul.github.io/radioimg.js/

        它使用隐藏的单选按钮和包含图像的标签,如下所示。

        <input type="radio" style="display: none;" id="a" />
        <label for="a">
           <img class="" />
        </label>
        

        【讨论】:

          【解决方案10】:

          可以使用 label 和 span 元素放置图像来代替单选按钮。

             <div class="customize-radio">
                  <label>Favourite Smiley</label>
                  <br>
                  <label for="hahaha">
                    <input type="radio" name="smiley" id="hahaha">
                    <span class="haha-img"></span>
                    HAHAHA
                  </label>
                  <label for="kiss">
                    <input type="radio" name="smiley" id="kiss">
                    <span class="kiss-img"></span>
                    Kiss
                  </label>
                  <label for="tongueOut">
                    <input type="radio" name="smiley" id="tongueOut">
                    <span class="tongueout-img"></span>
                    TongueOut
                  </label>
              </div>
          

          单选按钮应该被隐藏,

          .customize-radio label > input[type = 'radio'] {
              visibility: hidden;
              position: absolute;
          }
          

          图片可以在span标签中给出,

          .customize-radio label > input[type = 'radio'] ~ span{
              cursor: pointer;
              width: 27px;
              height: 24px;
              display: inline-block;
              background-size: 27px 24px;
              background-repeat: no-repeat;
          }
          .haha-img {
              background-image: url('hahabefore.png');
          }
          
          .kiss-img{
              background-image: url('kissbefore.png');
          }
          .tongueout-img{
              background-image: url('tongueoutbefore.png');
          }
          

          要在单击单选按钮时更改图像,请将选中状态添加到输入标签,

          .customize-radio label > input[type = 'radio']:checked ~ span.haha-img{
               background-image: url('haha.png');
          }
          .customize-radio label > input[type = 'radio']:checked ~ span.kiss-img{
              background-image: url('kiss.png');
          }
          .customize-radio label > input[type = 'radio']:checked ~ span.tongueout-img{
                  background-image: url('tongueout.png');
          }
          

          如果您有任何疑问,请参阅以下链接,因为我已从以下博客中获取解决方案, http://frontendsupport.blogspot.com/2018/06/cool-radio-buttons-with-images.html

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2019-04-14
            • 2015-12-31
            • 1970-01-01
            • 2013-02-19
            • 2018-12-13
            • 2014-10-16
            • 2013-03-22
            相关资源
            最近更新 更多