简体   繁体   English

如何使用 CSS 转换将加号变成减号?

[英]How to morph a plus sign to a minus sign using CSS transition?

I want to create a toggle button that morphs its shape from a plus sign to a minus sign.我想创建一个切换button ,将其形状从加号变为减号。

Using CSS only , without the use of pseudo-elements .仅使用 CSS使用伪元素

My desired effect is to have the vertical line in the "+" sign to shrink into the horizontal line.想要的效果是让“+”号中的垂直线收缩成水平线。

I know it's possible but I'm not sure which is the best route to take.我知道这是可能的,但我不确定哪条路是最好的。 I was thinking of doing something with the height but I'm worried about the line-height of browsers changing its position in the element.我正在考虑对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>

Because of the simplicity of the shapes, the easiest way is just to make the + and - with elements.由于形状简单,最简单的方法就是用元素制作+- Using pseudo elements would be the cleanest solution, but you can also just use a DOM element and have a slightly messier document structure.使用伪元素将是最干净的解决方案,但您也可以只使用 DOM 元素并拥有稍微混乱的文档结构。

With that in mind, the actual solution is straightforward.考虑到这一点,实际的解决方案很简单。 We use CSS to position elements to resemble the desired characters, and then "morph" between them by animating that position.我们使用 CSS 来定位元素以类似于所需的字符,然后通过为该位置设置动画来在它们之间“变形”。

Take a look over the following code, and try to understand what each rule is accomplishing.查看以下代码,并尝试了解每个规则的作用。

 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>

Note : please stop editing the question making the answers incorrect注意:请停止编辑问题,使答案不正确

CSS solution 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>

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

Using pseudo element ::before with content property使用伪元素::beforecontent属性

 $('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>

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

no need for span , you can do this using text() with a if statement in 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>

Ah my bad I've overlooked that OP doesn't want to use any pseudo elements.啊,糟糕,我忽略了 OP 不想使用任何伪元素。 But the big advantage with pseudo elements would be that you have less HTML Code and a cleaner structure.但是伪元素的最大优势在于您拥有更少的 HTML 代码和更清晰的结构。

It's also a different morphing animation as OP wants but maybe someone else can use this.它也是 OP 想要的不同变形动画,但也许其他人可以使用它。

So if you don't mind I'll let my suggestion there.所以如果你不介意的话,我会把我的建议放在那里。

Maybe something like this?也许是这样的?

HTML HTML

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

CSS 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 jQuery

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

And here a working example http://codepen.io/svelts/pen/LkyZoZ这里有一个工作示例http://codepen.io/svelts/pen/LkyZoZ

try this试试这个

 $('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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM