【问题标题】:How to morph a plus sign to a minus sign using CSS transition?如何使用 CSS 过渡将加号变为减号?
【发布时间】:2021-01-21 00:47:41
【问题描述】:

我想创建一个切换button,将其形状从加号变为减号。

仅使用 CSS,不使用伪元素。

我的想要的效果是让“+”号中的垂直线收缩成水平线。

我知道这是可能的,但我不确定哪条是最好的选择。我正在考虑用height 做点什么,但我担心浏览器的line-height 会改变它在元素中的位置。

$('button').on("click", function(){
  $(this).toggleClass('active');
});
button {
color: #ecf0f1;
background:  #e74c3c;
  width: 50px;
  height: 50px;
  border: 0;
  font-size: 1.5em;
  }
button span {
  transition: all .75s ease-in-out;
  }
button.active span {
  /* Code to morph + to - */
  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<button><span>+</span></button>

【问题讨论】:

    标签: javascript html jquery css css-transitions


    【解决方案1】:

    由于形状简单,最简单的方法就是用元素制作+ 和-。使用伪元素将是最干净的解决方案,但您也可以只使用 DOM 元素并拥有稍微混乱的文档结构。

    考虑到这一点,实际的解决方案很简单。我们使用 CSS 来定位元素以类似于所需的字符,然后通过动画该位置在它们之间“变形”。

    查看以下代码,并尝试了解每个规则的作用。

    button {
      color: #ecf0f1;
      background: #e74c3c;
      width: 50px;
      height: 50px;
      border: 0;
      font-size: 1.5em;
      position: relative;
    }
    
    button span {
      position: absolute;
      transition: 300ms;
      background: white;
      border-radius: 2px;
    }
    
    /* Create the "+" shape by positioning the spans absolutely */
    button span:first-child {
      top: 25%;
      bottom: 25%;
      width: 10%;
      left: 45%;
    }
    
    button span:last-child {
      left: 25%;
      right: 25%;
      height: 10%;
      top: 45%;
    }
    
    /* Morph the shape when the button is hovered over */
    button:hover span {
      transform: rotate(90deg);
    }
    
    button:hover span:last-child {
      left: 50%;
      right: 50%;
    }
    <button>
      <span></span>
      <span></span>
    </button>

    【讨论】:

    • 是的,最简单但 OP 不想要伪元素:/
    • @dippas 我会更新答案,但它仍然是相同的概念。
    【解决方案2】:

    注意:请停止编辑使答案不正确的问题

    CSS 解决方案

    $('button').on("click", function(){
      $(this).toggleClass('active');
    });
    button {
      color: #ecf0f1;
      background: #e74c3c;
      width: 70px;
      height: 70px;
      position: relative;
      font-size: 50px;
      cursor: pointer;
      border: 0;
      outline: 0;
      padding: 0
    }
    .plus,
    .minus {
      color: #fff;
      padding: 10px;
      width: 70px;
      height: 70px;
      line-height: 50px;
      display: block;
      position: absolute;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      text-align: center;
      box-sizing: border-box;
      transition: .5s all ease-out;
    }
    .plus {
      opacity: 1;
      transform: rotate(0deg);
    }
    button.active .plus {
      opacity: 0;
      transform: rotate(90deg);
    }
    .minus {
      opacity: 0;
      transform: rotate(-90deg);
    }
    button.active .minus {
      opacity: 1;
      transform: rotate(0deg);
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet" />
    <button>
      <span class="plus"><i class="fa fa-plus"></i></span>
      <span class="minus"><i class="fa fa-minus"></i></span>
    </button>

    一个(旧的)CSS 解决方案:

    使用带有content属性的伪元素::before

    $('button').on("click", function() {
      $(this).toggleClass('active');
    });
    button {
      color: #ecf0f1;
      background: #e74c3c;
      width: 50px;
      height: 50px;
      border: 0;
      font-size: 1.5em;
    }
    button span {
      transition: all .75s ease-in-out;
      position:relative
    }
    button span::before {
      content:"+"
    }
    button.active span::before {
      content:"-"
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <button><span></span></button>

    一个(旧的)jquery 解决方案:

    不需要span,您可以使用text() 和jquery 中的if 语句来做到这一点

    $('button').on("click", function() {
      $(this).toggleClass('active');
      $(this).text() == "+" ? $(this).text("-") : $(this).text("+");
    });
    button {
      color: #ecf0f1;
      background: #e74c3c;
      width: 50px;
      height: 50px;
      border: 0;
      font-size: 1.5em;
      transition: all .75s ease-in-out;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <button>+</button>

    【讨论】:

    • 我希望它变形,而不仅仅是改变。谢谢!
    • 仅供参考,我添加了一个纯 CSS 解决方案,你所说的 morph 是什么意思?
    • 请停止编辑导致答案不正确的问题
    • 我不知道如何更好地解释它。我已经在我的问题描述中提到了它。我希望加号中的垂直线缩小直到消失,变成减号。
    • 优雅方案+1
    【解决方案3】:

    我的错,我忽略了 OP 不想使用任何伪 元素。但使用伪元素的最大优势在于您拥有更少的 HTML 代码和更简洁的结构。

    这也是 OP 想要的不同 morphing 动画,但也许其他人可以使用它。

    所以如果你不介意我会把我的建议放在那里。

    也许是这样的?

    HTML

    <div class="button"></div>
    

    CSS

    * {
      box-sizing: border-box;
    }
    
    html,
    body {
      height: 100%;
    }
    
    body {
      margin: 0;
      background: #343838;
    }
    
    .button {
      position: absolute;
      width: 55px;
      height: 55px;
      background: #70975B;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%) rotate(0deg);
      border-radius: 50%;
      cursor: pointer;
      z-index: 100;
      transition: 0.4s cubic-bezier(0.2, 0.6, 0.3, 1.1);
    }
    
    .button:after {
      content: '';
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      height: 2px;
      width: 50%;
      background: white;
    }
    
    .button:before {
      content: '';
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      height: 50%;
      width: 2px;
      background: white;
    }
    
    .button.clicked {
      transform: translate(-50%, -50%) rotate(360deg);
      background: #CC2A41;
    }
    
    .button.clicked:before {
      width: 0;
    }
    

    jQuery

    $(".button").click(function() {
      $(this).toggleClass("clicked");
    });
    

    这里是一个工作示例 http://codepen.io/svelts/pen/LkyZoZ

    【讨论】:

      【解决方案4】:

      试试这个

      $('button').on("click", function() {
        var $this = $(this);
        $this.toggleClass('toggle');
        if ($this.hasClass('toggle')) {
          $this.text('+');
        } else {
          $this.text('-');
        }
      });
      button {
        color: #ecf0f1;
        background: #e74c3c;
        width: 50px;
        height: 50px;
        border: 0;
        font-size: 1.5em;
        transition: all .75s ease-in-out;
      }
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
      <button class="toggle">+</button>

      【讨论】:

      • 我希望它变形,而不仅仅是改变。谢谢!
      猜你喜欢
      • 1970-01-01
      • 2022-12-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-14
      • 2017-02-02
      • 2012-06-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多