【问题标题】:How can I add an SVG to a Toggle Button?如何将 SVG 添加到切换按钮?
【发布时间】:2020-02-08 11:34:43
【问题描述】:

我有一个在多个地方使用的 ToggleButton 组件。对于它的一种用途,我希望将 SVG 呈现为滑动的切换按钮的一部分(如果您查看位于 cmets 链接中的示例,我指的是圆形部分)。

这是我的组件代码:

const ToggleButton = ({
  label,
  value,
  name,
  onChange,
}) => (
    <div className={styles.toggleButton}>
      <input
        id={name}
        type="checkbox"
        className={styles.checkbox}
        value={value}
        checked={value ? 'checked' : null}
        onChange={onChange}
      />
      <label htmlFor={name} className={styles.switch} />
    </div>
  );

export default ToggleButton;

这是 CSS 文件:

.toggleButton {
  align-items: center;
}

.switch {
  position: relative;
  display: inline-block;
  width: 35px;
  height: 12px;
  background-color: gray;
  border-radius: 20px;
  transition: all 0.3s;
}

.switch::after {
  content: '';
  position: absolute;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background-color: gray;
  top: -4px;
  transition: all 0.3s;
  cursor: pointer;
  box-shadow: 0 4px 2px -2px rgba(0, 0, 0, 0.25);
}

.checkbox {
  display: none;
}

.checkbox:checked + .switch::after {
  left: 15px;
  background-color: blue;
}

.checkbox:checked + .switch {
  background-color: blue;
}

【问题讨论】:

  • 只需为一个 div 的背景创建一个 css 规则,缩放它并将 div 添加到你的按钮。与任何其他图像完全一样。 Fontawesome 将 scg 转换为 base64 并将其用作字符。去那里看看 :)

标签: html css reactjs svg toggleswitch


【解决方案1】:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width,initial-scale=1.0" />
    <title>demo</title>
    <style>
    .ease {
  transition: all 300ms ease;
}
.switch {
  position: relative;
  display: inline-block;
  width: 60px;
  height: 28px;
}

.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: #8a8a8a;
  -webkit-transition: 0.4s;
  transition: 0.4s;
}
.switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.slider:before {
  position: absolute;
  content: "";
  height: 24px;
  width: 24px;
  left: 4px;
  bottom: 2px;
  background-color: #2c2c2c;
  -webkit-transition: 0.4s;
  transition: 0.4s;
  
}

input:checked + .slider {
  background-color: #8a8a8a;  
}

input:checked + .slider:before {
  background-color: #f3c93c; 
  content: url("data:image/svg+xml; utf8, <svg xmlns='http://www.w3.org/2000/svg' width='15' height='15' viewBox='0 0 15 15'><path fill-rule='evenodd' d='M7.5.469c.9 0 1.629.754 1.629 1.675 0 .22-.048.443-.138.654l-.028.059.114.04c.765.278 1.456.76 1.992 1.387l.13.16.116.153c.56.784.885 1.732.926 2.7l.004.207v3.685h.451c.277 0 .51.199.57.462l.011.067.004.07c0 .305-.226.56-.517.594l-.068.004H9.641v.06c-.058 1.08-.87 1.957-1.907 2.072l-.116.01-.118.003c-1.141 0-2.079-.927-2.14-2.084l-.002-.06H2.311c-.278 0-.514-.2-.576-.463l-.012-.067-.004-.07c0-.304.231-.559.524-.594l.068-.004h.45l.001-3.685c0-.9.248-1.792.705-2.562l.109-.175.116-.17c.562-.78 1.35-1.378 2.234-1.7l.113-.038-.03-.066c-.05-.127-.087-.257-.109-.388l-.016-.13-.005-.131c0-.92.727-1.675 1.621-1.675zm.97 11.917H6.53v.031c.043.482.416.862.882.906l.088.004c.261 0 .506-.103.688-.292.157-.162.257-.383.28-.611l.002-.038zm-2.748-8.08c-1.06.63-1.734 1.785-1.786 3.04l-.004.158v3.685h7.136V7.504c0-1.266-.628-2.444-1.652-3.117l-.13-.081c-1.106-.652-2.474-.654-3.564 0zm1.614-2.603c-.177.08-.287.248-.287.44 0 .262.202.471.451.471.188 0 .35-.11.42-.284.07-.18.032-.388-.096-.52-.133-.137-.322-.176-.488-.107z'/></svg>");
  margin:auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-items: center;
}


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

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

/* Rounded sliders */
.slider.round {
  border-radius: 30px;
}

.slider.round:before {
  border-radius: 50%;
}

    </style>
  </head>
  <body>
   <label class="switch" v-if="option.icon && option.icon === 'mute'">
          <input
            type="checkbox"
            checked="true"            
          />      
          <span class="slider round"></span>
        </label>       
  </body>
</html>

【讨论】:

    【解决方案2】:

    一些例子:

             <style>
                .icon {
                    background-color: orange;
                    border: 1px solid blue;
                    color: rgb(255, 0, 0) !important;
                    background-image: linear-gradient(orange, orange, orange) !important;
                    opacity: 1;
                }
           </style> 
    

    [data] 表示 svg 源的 base64 编码字符串 您可以将它们嵌入到 css 中

     <style>
     .logo {
          background: url("data:image/svg+xml;base64,[data]");
          width:20px;
          height:20px;
     }
     </style>
    

    作为css中的svg源

     <style>
    
         .svgbg { 
            width:100%; height:150px; 
           background: url("data:image/svg+xml;utf8,<svg 
           xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 44' height='44' 
            width='100'><path d='m 0,11 100,0' style='stroke:#efefef;stroke- 
          width:21.5px' /></svg>")
    
           /* your case hope i adopt it right in this editbox*/ 
    
           .checkmark{ 
             width = 14px;
             height = 11px;
             color = '#ffffff';
    
              background: url("data:image/svg+xml;utf8,<svg x="0px" y="0px" width= 
                   {width} height={height} viewBox="0 0 38.826 29.541" > <g> 
                    <polygon fill={color} points="32.194,0 15.194,17 6.445,8.217 
                    0,14.771 
                    15.15,29.541 38.826,6.541" /> </g> </svg>");
           }
           </style>
    

    或直接使用

     <IMG class="icon" src="data:image/svg+xml;base64,[data]">
    
     <div class="logo"></div>  
    

    当然,所有这些也可以在 javascript 中完成。如何在按钮上显示 div 或图像取决于您的设计 :)

    <div className={styles.toggleButton}>
     <!-- sure src="url(..images/image.svg)" works as well -->
     <IMG class="icon" src="data:image/svg+xml;base64,[data]"> 
     <input
        id={name}
        type="checkbox"
        className={styles.checkbox}
        value={value}
        checked={value ? 'checked' : null}
        onChange={onChange}
      />
      <label htmlFor={name} className={styles.switch} />
    </div>
    

    【讨论】:

    • 我编辑我的答案。我会把它放在一个CSS规则中。这样可以使标记保持可读性,并且以后易于维护 - 如果您想更改所有复选框。
    • 顺便说一句:如果你想要一个漂亮的复选框 - 你可以使用 UTF 字符(有一个复选标记)和一个 div 更容易。将 div 设置为框并将复选标记放置/堆叠在其上。您可以将 z-index 放在丑陋的浏览器框上。 Fontawesome 也有这样的角色——他们也用 svg 来做到这一点——有很多方法可以去除丑陋的复选框;)
    • mediaevent.de/svg-in-html-seiten 放置 svg 的 7 种方法 - 抱歉,它是德国页面 :)
    • 你拥有我的啤酒;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-12
    • 1970-01-01
    • 2018-08-02
    • 2015-09-28
    • 2018-01-23
    • 1970-01-01
    • 2017-01-28
    相关资源
    最近更新 更多