jquery 如何使用CSS转换将加号变形为减号?

yx2lnoni  于 2024-01-07  发布在  jQuery
关注(0)|答案(5)|浏览(161)

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

只使用CSS,*不使用 * 伪元素

想要的效果是让“+”号中的垂直线缩小为水平线。
我知道这是可能的,但我不确定哪一个是最好的路线,我想做一些与height,但我担心的line-height的浏览器改变其在元素中的位置。

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

n8ghc7c11#

由于形状的简单性,最简单的方法就是使用元素来创建+-。使用伪元素是最干净的解决方案,但您也可以只使用DOM元素,并拥有稍微混乱的文档结构。
考虑到这一点,实际的解决方案是简单的。我们使用CSS来定位元素,使其与所需的字符相似,然后通过动画显示该位置来在它们之间“变形”。
看看下面的代码,并尝试理解每个规则的功能。

  1. button {
  2. color: #ecf0f1;
  3. background: #e74c3c;
  4. width: 50px;
  5. height: 50px;
  6. border: 0;
  7. font-size: 1.5em;
  8. position: relative;
  9. }
  10. button span {
  11. position: absolute;
  12. transition: 300ms;
  13. background: white;
  14. border-radius: 2px;
  15. }
  16. /* Create the "+" shape by positioning the spans absolutely */
  17. button span:first-child {
  18. top: 25%;
  19. bottom: 25%;
  20. width: 10%;
  21. left: 45%;
  22. }
  23. button span:last-child {
  24. left: 25%;
  25. right: 25%;
  26. height: 10%;
  27. top: 45%;
  28. }
  29. /* Morph the shape when the button is hovered over */
  30. button:hover span {
  31. transform: rotate(90deg);
  32. }
  33. button:hover span:last-child {
  34. left: 50%;
  35. right: 50%;
  36. }

个字符

展开查看全部
gj3fmq9x

gj3fmq9x2#

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

CSS解决方案

  1. $('button').on("click", function(){
  2. $(this).toggleClass('active');
  3. });
  1. button {
  2. color: #ecf0f1;
  3. background: #e74c3c;
  4. width: 70px;
  5. height: 70px;
  6. position: relative;
  7. font-size: 50px;
  8. cursor: pointer;
  9. border: 0;
  10. outline: 0;
  11. padding: 0
  12. }
  13. .plus,
  14. .minus {
  15. color: #fff;
  16. padding: 10px;
  17. width: 70px;
  18. height: 70px;
  19. line-height: 50px;
  20. display: block;
  21. position: absolute;
  22. top: 0;
  23. left: 0;
  24. right: 0;
  25. bottom: 0;
  26. text-align: center;
  27. box-sizing: border-box;
  28. transition: .5s all ease-out;
  29. }
  30. .plus {
  31. opacity: 1;
  32. transform: rotate(0deg);
  33. }
  34. button.active .plus {
  35. opacity: 0;
  36. transform: rotate(90deg);
  37. }
  38. .minus {
  39. opacity: 0;
  40. transform: rotate(-90deg);
  41. }
  42. button.active .minus {
  43. opacity: 1;
  44. transform: rotate(0deg);
  45. }
  1. <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
  2. <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet" />
  3. <button>
  4. <span class="plus"><i class="fa fa-plus"></i></span>
  5. <span class="minus"><i class="fa fa-minus"></i></span>
  6. </button>

一个(旧)CSS解决方案:

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

  1. $('button').on("click", function() {
  2. $(this).toggleClass('active');
  3. });

x

  1. button {
  2. color: #ecf0f1;
  3. background: #e74c3c;
  4. width: 50px;
  5. height: 50px;
  6. border: 0;
  7. font-size: 1.5em;
  8. }
  9. button span {
  10. transition: all .75s ease-in-out;
  11. position:relative
  12. }
  13. button span::before {
  14. content:"+"
  15. }
  16. button.active span::before {
  17. content:"-"
  18. }
  1. <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
  2. <button><span></span></button>

的一个字符串

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

不需要span,你可以在jquery中使用text()和if语句来实现这一点。

  1. $('button').on("click", function() {
  2. $(this).toggleClass('active');
  3. $(this).text() == "+" ? $(this).text("-") : $(this).text("+");
  4. });
  1. button {
  2. color: #ecf0f1;
  3. background: #e74c3c;
  4. width: 50px;
  5. height: 50px;
  6. border: 0;
  7. font-size: 1.5em;
  8. transition: all .75s ease-in-out;
  9. }
  1. <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
  2. <button>+</button>
展开查看全部
shyt4zoc

shyt4zoc3#

啊,我的错,我忽略了OP不想使用任何伪元素。但是伪元素的最大好处是你有更少的HTML代码和更干净的结构。
这也是一个不同的 * 变形 * 动画作为OP希望,但也许别人可以使用这个。
所以如果你不介意的话,我就把我的建议放在那里。
像这样的?

超文本标记语言

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

字符串

CSS

  1. * {
  2. box-sizing: border-box;
  3. }
  4. html,
  5. body {
  6. height: 100%;
  7. }
  8. body {
  9. margin: 0;
  10. background: #343838;
  11. }
  12. .button {
  13. position: absolute;
  14. width: 55px;
  15. height: 55px;
  16. background: #70975B;
  17. top: 50%;
  18. left: 50%;
  19. transform: translate(-50%, -50%) rotate(0deg);
  20. border-radius: 50%;
  21. cursor: pointer;
  22. z-index: 100;
  23. transition: 0.4s cubic-bezier(0.2, 0.6, 0.3, 1.1);
  24. }
  25. .button:after {
  26. content: '';
  27. position: absolute;
  28. top: 50%;
  29. left: 50%;
  30. transform: translate(-50%, -50%);
  31. height: 2px;
  32. width: 50%;
  33. background: white;
  34. }
  35. .button:before {
  36. content: '';
  37. position: absolute;
  38. top: 50%;
  39. left: 50%;
  40. transform: translate(-50%, -50%);
  41. height: 50%;
  42. width: 2px;
  43. background: white;
  44. }
  45. .button.clicked {
  46. transform: translate(-50%, -50%) rotate(360deg);
  47. background: #CC2A41;
  48. }
  49. .button.clicked:before {
  50. width: 0;
  51. }

jQuery查询

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


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

展开查看全部
gmxoilav

gmxoilav4#

我想在写完这篇文章后,我会展示一个同时发生多个转换的例子,以给予一个“抛光”的外观。
我还添加了一个“点击”事件,以保持交叉点击。

  1. .toggleIcon {
  2. position: relative;
  3. width: 50px; /* Define the size of your toggle icon */
  4. height: 50px;
  5. background: #ffffff; /* Set the background color */
  6. border-radius: 50%; /* Make it round */
  7. display: inline-flex;
  8. align-items: center;
  9. justify-content: center;
  10. cursor: pointer;
  11. }
  12. .toggleIcon::after,
  13. .toggleIcon::before {
  14. position: absolute;
  15. content: '';
  16. height: 30px;
  17. width: 2px;
  18. background: #1A0D07;
  19. left: 50%;
  20. top: 50%;
  21. transition: transform 0.4s ease;
  22. transform: translate(-50%, -50%);
  23. }
  24. .toggleIcon.toggleActive::before {
  25. transition: transform 0.2s ease;
  26. }
  27. .toggleIcon::before {
  28. transform: translate(-50%, -50%) rotate(90deg);
  29. }
  30. .toggleIconParent:hover .toggleIcon::before,
  31. .toggleIcon:hover::before,
  32. .toggleIcon:active::before{
  33. transform: translate(-50%, -50%) rotate(0deg) scale(1.2);
  34. }
  35. .toggleIcon:hover::after,
  36. .toggleIconParent:hover .toggleIcon::after,
  37. .toggleIcon:active::after,
  38. .toggleIcon.toggleActive::after {
  39. transform: translate(-50%, -50%) rotate(-90deg) scale(1.2);
  40. }
  41. .toggleIcon.toggleActive::before {
  42. transform: translate(-50%, -50%) rotate(90deg) scale(1.2);
  43. }
  44. .toggleIcon.toggleActive::after,
  45. .toggleIcon.toggleActive::before {
  46. width: 1px;
  47. }

个字符

展开查看全部
tjjdgumg

tjjdgumg5#

试试这个

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

相关问题