基于多选框 动画标记输入

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

简介

在本教程中,我们将制作一个jQuery脚本,将常规多选框转换为动画标记输入字段。

当您在下拉框中选择选项时,这些选项将以标记的样式显示在输入字段中。要取消选择某个选项,只需单击标记中的X图标即可。

如何使用它:

1.创建倍数选择页面上的框,然后在中定义占位符文本数据占位符属性:

  1. <select multiple data-placeholder="Select Languages">
  2. <option>Python</option>
  3. <option selected>JavaScript</option>
  4. <option>C/C++</option>
  5. <option>Java</option>
  6. <option>Kotlin</option>
  7. <option>PHP</option>
  8. <option>Scala</option>
  9. <option>Vue.js</option>
  10. <option>Angular</option>
  11. <option>React</option>
  12. </select>

2.在文档中加载所需的jQuery库。

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

3.将选择框转换为标签输入。

  1. $(document).ready(function () {
  2.  
  3. var select = $('select[multiple]');
  4. var options = select.find('option');
  5.  
  6. var div = $('<div />').addClass('selectMultiple');
  7. var active = $('<div />');
  8. var list = $('<ul />');
  9. var placeholder = select.data('placeholder');
  10.  
  11. var span = $('<span />').text(placeholder).appendTo(active);
  12.  
  13. options.each(function () {
  14. var text = $(this).text();
  15. if ($(this).is(':selected')) {
  16. active.append($('<a />').html('<em>' + text + '</em><i></i>'));
  17. span.addClass('hide');
  18. } else {
  19. list.append($('<li />').html(text));
  20. }
  21. });
  22.  
  23. active.append($('<div />').addClass('arrow'));
  24. div.append(active).append(list);
  25.  
  26. select.wrap(div);
  27.  
  28. $(document).on('click', '.selectMultiple ul li', function (e) {
  29. var select = $(this).parent().parent();
  30. var li = $(this);
  31. if (!select.hasClass('clicked')) {
  32. select.addClass('clicked');
  33. li.prev().addClass('beforeRemove');
  34. li.next().addClass('afterRemove');
  35. li.addClass('remove');
  36. var a = $('<a />').addClass('notShown').html('<em>' + li.text() + '</em><i></i>').hide().appendTo(select.children('div'));
  37. a.slideDown(400, function () {
  38. setTimeout(function () {
  39. a.addClass('shown');
  40. select.children('div').children('span').addClass('hide');
  41. select.find('option:contains(' + li.text() + ')').prop('selected', true);
  42. }, 500);
  43. });
  44. setTimeout(function () {
  45. if (li.prev().is(':last-child')) {
  46. li.prev().removeClass('beforeRemove');
  47. }
  48. if (li.next().is(':first-child')) {
  49. li.next().removeClass('afterRemove');
  50. }
  51. setTimeout(function () {
  52. li.prev().removeClass('beforeRemove');
  53. li.next().removeClass('afterRemove');
  54. }, 200);
  55.  
  56. li.slideUp(400, function () {
  57. li.remove();
  58. select.removeClass('clicked');
  59. });
  60. }, 600);
  61. }
  62. });
  63.  
  64. $(document).on('click', '.selectMultiple > div a', function (e) {
  65. var select = $(this).parent().parent();
  66. var self = $(this);
  67. self.removeClass().addClass('remove');
  68. select.addClass('open');
  69. setTimeout(function () {
  70. self.addClass('disappear');
  71. setTimeout(function () {
  72. self.animate({
  73. width: 0,
  74. height: 0,
  75. padding: 0,
  76. margin: 0
  77. }, 300, function () {
  78. var li = $('<li />').text(self.children('em').text()).addClass('notShown').appendTo(select.find('ul'));
  79. li.slideDown(400, function () {
  80. li.addClass('show');
  81. setTimeout(function () {
  82. select.find('option:contains(' + self.children('em').text() + ')').prop('selected', false);
  83. if (!select.find('option:selected').length) {
  84. select.children('div').children('span').removeClass('hide');
  85. }
  86. li.removeClass();
  87. }, 400);
  88. });
  89. self.remove();
  90. })
  91. }, 300);
  92. }, 400);
  93. });
  94.  
  95. $(document).on('click', '.selectMultiple > div .arrow, .selectMultiple > div span', function (e) {
  96. $(this).parent().parent().toggleClass('open');
  97. });
  98.  
  99. });

4.标记输入所需的CSS/CS3样式。

  1. .selectMultiple {
  2. width: 400px;
  3. position: relative;
  4. }
  5. .selectMultiple select {
  6. display: none;
  7. }
  8. .selectMultiple > div {
  9. position: relative;
  10. z-index: 2;
  11. padding: 8px 12px 2px 12px;
  12. border-radius: 8px;
  13. background: #fff;
  14. font-size: 14px;
  15. min-height: 44px;
  16. box-shadow: 0 4px 16px 0 rgba(22, 42, 90, 0.12);
  17. transition: box-shadow 0.3s ease;
  18. }
  19. .selectMultiple > div:hover {
  20. box-shadow: 0 4px 24px -1px rgba(22, 42, 90, 0.16);
  21. }
  22. .selectMultiple > div .arrow {
  23. right: 1px;
  24. top: 0;
  25. bottom: 0;
  26. cursor: pointer;
  27. width: 28px;
  28. position: absolute;
  29. }
  30. .selectMultiple > div .arrow:before, .selectMultiple > div .arrow:after {
  31. content: "";
  32. position: absolute;
  33. display: block;
  34. width: 2px;
  35. height: 8px;
  36. border-bottom: 8px solid #99A3BA;
  37. top: 43%;
  38. transition: all 0.3s ease;
  39. }
  40. .selectMultiple > div .arrow:before {
  41. right: 12px;
  42. transform: rotate(-130deg);
  43. }
  44. .selectMultiple > div .arrow:after {
  45. left: 9px;
  46. transform: rotate(130deg);
  47. }
  48. .selectMultiple > div span {
  49. color: #99A3BA;
  50. display: block;
  51. position: absolute;
  52. left: 12px;
  53. cursor: pointer;
  54. top: 8px;
  55. line-height: 28px;
  56. transition: all 0.3s ease;
  57. }
  58. .selectMultiple > div span.hide {
  59. opacity: 0;
  60. visibility: hidden;
  61. transform: translate(-4px, 0);
  62. }
  63. .selectMultiple > div a {
  64. position: relative;
  65. padding: 0 24px 6px 8px;
  66. line-height: 28px;
  67. color: #1E2330;
  68. display: inline-block;
  69. vertical-align: top;
  70. margin: 0 6px 0 0;
  71. }
  72. .selectMultiple > div a em {
  73. font-style: normal;
  74. display: block;
  75. white-space: nowrap;
  76. }
  77. .selectMultiple > div a:before {
  78. content: "";
  79. left: 0;
  80. top: 0;
  81. bottom: 6px;
  82. width: 100%;
  83. position: absolute;
  84. display: block;
  85. background: rgba(228, 236, 250, 0.7);
  86. z-index: -1;
  87. border-radius: 4px;
  88. }
  89. .selectMultiple > div a i {
  90. cursor: pointer;
  91. position: absolute;
  92. top: 0;
  93. right: 0;
  94. width: 24px;
  95. height: 28px;
  96. display: block;
  97. }
  98. .selectMultiple > div a i:before, .selectMultiple > div a i:after {
  99. content: "";
  100. display: block;
  101. width: 2px;
  102. height: 10px;
  103. position: absolute;
  104. left: 50%;
  105. top: 50%;
  106. background: #4D18FF;
  107. border-radius: 1px;
  108. }
  109. .selectMultiple > div a i:before {
  110. transform: translate(-50%, -50%) rotate(45deg);
  111. }
  112. .selectMultiple > div a i:after {
  113. transform: translate(-50%, -50%) rotate(-45deg);
  114. }
  115. .selectMultiple > div a.notShown {
  116. opacity: 0;
  117. transition: opacity 0.3s ease;
  118. }
  119. .selectMultiple > div a.notShown:before {
  120. width: 28px;
  121. transition: width 0.45s cubic-bezier(0.87, -0.41, 0.19, 1.44) 0.2s;
  122. }
  123. .selectMultiple > div a.notShown i {
  124. opacity: 0;
  125. transition: all 0.3s ease 0.3s;
  126. }
  127. .selectMultiple > div a.notShown em {
  128. opacity: 0;
  129. transform: translate(-6px, 0);
  130. transition: all 0.4s ease 0.3s;
  131. }
  132. .selectMultiple > div a.notShown.shown {
  133. opacity: 1;
  134. }
  135. .selectMultiple > div a.notShown.shown:before {
  136. width: 100%;
  137. }
  138. .selectMultiple > div a.notShown.shown i {
  139. opacity: 1;
  140. }
  141. .selectMultiple > div a.notShown.shown em {
  142. opacity: 1;
  143. transform: translate(0, 0);
  144. }
  145. .selectMultiple > div a.remove:before {
  146. width: 28px;
  147. transition: width 0.4s cubic-bezier(0.87, -0.41, 0.19, 1.44) 0s;
  148. }
  149. .selectMultiple > div a.remove i {
  150. opacity: 0;
  151. transition: all 0.3s ease 0s;
  152. }
  153. .selectMultiple > div a.remove em {
  154. opacity: 0;
  155. transform: translate(-12px, 0);
  156. transition: all 0.4s ease 0s;
  157. }
  158. .selectMultiple > div a.remove.disappear {
  159. opacity: 0;
  160. transition: opacity 0.5s ease 0s;
  161. }
  162. .selectMultiple > ul {
  163. margin: 0;
  164. padding: 0;
  165. list-style: none;
  166. font-size: 16px;
  167. z-index: 1;
  168. position: absolute;
  169. top: 100%;
  170. left: 0;
  171. right: 0;
  172. visibility: hidden;
  173. opacity: 0;
  174. border-radius: 8px;
  175. transform: translate(0, 20px) scale(0.8);
  176. transform-origin: 0 0;
  177. filter: drop-shadow(0 12px 20px rgba(22, 42, 90, 0.08));
  178. transition: all 0.4s ease, transform 0.4s cubic-bezier(0.87, -0.41, 0.19, 1.44), filter 0.3s ease 0.2s;
  179. }
  180. .selectMultiple > ul li {
  181. color: #1E2330;
  182. background: #fff;
  183. padding: 12px 16px;
  184. cursor: pointer;
  185. overflow: hidden;
  186. position: relative;
  187. transition: background 0.3s ease, color 0.3s ease, transform 0.3s ease 0.3s, opacity 0.5s ease 0.3s, border-radius 0.3s ease 0.3s;
  188. }
  189. .selectMultiple > ul li:first-child {
  190. border-radius: 8px 8px 0 0;
  191. }
  192. .selectMultiple > ul li:first-child:last-child {
  193. border-radius: 8px;
  194. }
  195. .selectMultiple > ul li:last-child {
  196. border-radius: 0 0 8px 8px;
  197. }
  198. .selectMultiple > ul li:last-child:first-child {
  199. border-radius: 8px;
  200. }
  201. .selectMultiple > ul li:hover {
  202. background: #4D18FF;
  203. color: #fff;
  204. }
  205. .selectMultiple > ul li:after {
  206. content: "";
  207. position: absolute;
  208. top: 50%;
  209. left: 50%;
  210. width: 6px;
  211. height: 6px;
  212. background: rgba(0, 0, 0, 0.4);
  213. opacity: 0;
  214. border-radius: 100%;
  215. transform: scale(1, 1) translate(-50%, -50%);
  216. transform-origin: 50% 50%;
  217. }
  218. .selectMultiple > ul li.beforeRemove {
  219. border-radius: 0 0 8px 8px;
  220. }
  221. .selectMultiple > ul li.beforeRemove:first-child {
  222. border-radius: 8px;
  223. }
  224. .selectMultiple > ul li.afterRemove {
  225. border-radius: 8px 8px 0 0;
  226. }
  227. .selectMultiple > ul li.afterRemove:last-child {
  228. border-radius: 8px;
  229. }
  230. .selectMultiple > ul li.remove {
  231. transform: scale(0);
  232. opacity: 0;
  233. }
  234. .selectMultiple > ul li.remove:after {
  235. -webkit-animation: ripple 0.4s ease-out;
  236. animation: ripple 0.4s ease-out;
  237. }
  238. .selectMultiple > ul li.notShown {
  239. display: none;
  240. transform: scale(0);
  241. opacity: 0;
  242. transition: transform 0.35s ease, opacity 0.4s ease;
  243. }
  244. .selectMultiple > ul li.notShown.show {
  245. transform: scale(1);
  246. opacity: 1;
  247. }
  248. .selectMultiple.open > div {
  249. box-shadow: 0 4px 20px -1px rgba(22, 42, 90, 0.12);
  250. }
  251. .selectMultiple.open > div .arrow:before {
  252. transform: rotate(-50deg);
  253. }
  254. .selectMultiple.open > div .arrow:after {
  255. transform: rotate(50deg);
  256. }
  257. .selectMultiple.open > ul {
  258. transform: translate(0, 12px) scale(1);
  259. opacity: 1;
  260. visibility: visible;
  261. filter: drop-shadow(0 16px 24px rgba(22, 42, 90, 0.16));
  262. }
  263.  
  264. @-webkit-keyframes ripple {
  265. 0% {
  266. transform: scale(0, 0);
  267. opacity: 1;
  268. }
  269. 25% {
  270. transform: scale(30, 30);
  271. opacity: 1;
  272. }
  273. 100% {
  274. opacity: 0;
  275. transform: scale(50, 50);
  276. }
  277. }
  278.  
  279. @keyframes ripple {
  280. 0% {
  281. transform: scale(0, 0);
  282. opacity: 1;
  283. }
  284. 25% {
  285. transform: scale(30, 30);
  286. opacity: 1;
  287. }
  288. 100% {
  289. opacity: 0;
  290. transform: scale(50, 50);
  291. }
  292. }

预览截图