带jQuery和CSS3 变形按钮模式

  • 源码大小:8.2KB
  • 所需积分:1积分
  • 源码编号:19JP-3521
  • 浏览次数:541次
  • 最后更新:2023年06月22日
  • 所属栏目:其他
我要下载
加入收藏
本站默认解压密码:19jp.com 或 19jp_com

简介

一个创造性的按钮组,使用jQuery和CSS3转换和转换将按钮变形为内联模式。灵感来自材料设计。

它不仅能增强用户的视觉体验,还能在与网站互动时改善用户的体验。

如何使用它:

1.创建一个包含两个操作按钮的按钮组,如下所示:

  1. <div class="buttons-ctn">
  2. <a href="" class="button button--left"><span>Left</span></a>
  3. <a href="" class="button button--right"><span>Right</span></a>
  4. </div>

2.为每个按钮创建模态。

  1. <div class="button__content button__content--left">
  2. <h2>You chose left!</h2>
  3. <a href="">Signup for nothing here</a>
  4. </div>
  5. <div class="button__content button__content--right">
  6. <h2>You chose right!</h2>
  7. <a href="">Signup for nothing here</a>
  8. </div>

3.必要的CSS样式。

  1. .buttons-ctn {
  2. will-change: transform;
  3. position: absolute;
  4. top: 50%;
  5. left: 50%;
  6. margin-left: -140px;
  7. margin-top: -30px;
  8. transform-style: preserve-3d;
  9. -webkit-backface-visibility: hidden;
  10. backface-visibility: hidden;
  11. }
  12.  
  13. .button {
  14. will-change: transform;
  15. position: relative;
  16. float: left;
  17. display: inline-block;
  18. padding: 20px;
  19. width: 140px;
  20. text-align: center;
  21. line-height: normal;
  22. transition: all 0.35s cubic-bezier(0.175, 0.885, 0.32, 1.275);
  23. }
  24. .button--left {
  25. background: #141414;
  26. color: white;
  27. }
  28. .button--right {
  29. background: #ebebeb;
  30. color: #141414;
  31. }
  32. .button--active {
  33. cursor: default;
  34. }
  35. .button--active span {
  36. opacity: 0;
  37. }
  38.  
  39. .button__content {
  40. display: block;
  41. position: absolute;
  42. top: 50%;
  43. left: 50%;
  44. transform: translate(-50%, -50%);
  45. padding: 60px 20px;
  46. text-align: center;
  47. width: 600px;
  48. visibility: hidden;
  49. opacity: 0;
  50. z-index: 10;
  51. color: white;
  52. }
  53. .button__content--left {
  54. color: white;
  55. }
  56. .button__content--left a {
  57. color: #141414;
  58. background: white;
  59. }
  60. .button__content--right {
  61. color: #141414;
  62. }
  63. .button__content--right a {
  64. color: white;
  65. background: #141414;
  66. }
  67. .button__content--active {
  68. opacity: 1;
  69. visibility: visible;
  70. }
  71. .button__content a {
  72. display: inline-block;
  73. padding: 10px 20px;
  74. }
  75. .button__content h2 {
  76. font-size: 36px;
  77. text-transform: uppercase;
  78. letter-spacing: 3px;
  79. font-weight: 300;
  80. }
  81. @media (max-width: 650px) {
  82. .button__content {
  83. width: 295px;
  84. }
  85. }

4.在文档末尾加载最新的jQuery库。

  1. <script src="/path/to/cdn/jquery.slim.min.js"></script>

5.激活按钮模态的主脚本。

  1. var button = $('.button');
  2. var content = $('.button__content');
  3. var win = $(window);
  4.  
  5. var expand = function() {
  6. if (button.hasClass('button--active')) {
  7. return false;
  8. } else {
  9. var W = win.width();
  10. var xc = W / 2;
  11.  
  12. var that = $(this);
  13. var thatWidth = that.innerWidth() / 2;
  14. var thatOffset = that.offset();
  15. var thatIndex = that.index();
  16. var other;
  17.  
  18. if (!that.next().is('.button')) {
  19. other = that.prev();
  20. } else {
  21. other = that.next();
  22. }
  23.  
  24. var otherWidth = other.innerWidth() / 2;
  25. var otherOffset = other.offset();
  26.  
  27. // content box stuff
  28. var thatContent = $('.button__content').eq(thatIndex);
  29. var thatContentW = thatContent.innerWidth();
  30. var thatContentH = thatContent.innerHeight();
  31.  
  32. // transform values for button
  33. var thatTransX = xc - thatOffset.left - thatWidth;
  34. var otherTransX = xc - otherOffset.left - otherWidth;
  35. var thatScaleX = thatContentW / that.innerWidth();
  36. var thatScaleY = thatContentH / that.innerHeight();
  37.  
  38. that.css({
  39. 'z-index': '2',
  40. 'transform': 'translateX(' + thatTransX + 'px)'
  41. });
  42.  
  43. other.css({
  44. 'z-index': '0',
  45. 'opacity': '0',
  46. 'transition-delay': '0.05s',
  47. 'transform': 'translateX(' + otherTransX + 'px)'
  48. });
  49.  
  50. that.on('transitionend webkitTransitionEnd', function() {
  51. that.css({
  52. 'transform': 'translateX(' + thatTransX + 'px) scale(' + thatScaleX +',' + thatScaleY + ')',
  53. });
  54.  
  55. that.addClass('button--active');
  56. thatContent.addClass('button__content--active');
  57. thatContent.css('transition', 'all 1s 0.1s cubic-bezier(0.23, 1, 0.32, 1)');
  58. that.off('transitionend webkitTransitionEnd');
  59. });
  60.  
  61. return false;
  62. }
  63. };
  64.  
  65. var hide = function(e) {
  66. var target= $(e.target);
  67. if (target.is(content)) {
  68. return;
  69. } else {
  70. button.removeAttr('style').removeClass('button--active');
  71. content.removeClass('button__content--active').css('transition', 'all 0.2s 0 cubic-bezier(0.23, 1, 0.32, 1)');
  72. }
  73. };
  74.  
  75. var bindActions = function() {
  76. button.on('click', expand);
  77. win.on('click', hide);
  78. };
  79.  
  80. var init = function() {
  81. bindActions();
  82. };
  83.  
  84. init();

预览截图