经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » HTML/CSS » CSS3 » 查看文章
纯CSS3实现漂亮的input输入框动画样式库(Text input love)_css3_CSS
来源:jb51  时间:2019/1/2 9:27:03  对本文有异议

分享一个用纯 CSS3 实现的,漂亮的 input 输入框动画样式库-Text input love。

点击每个输入框都用不同的动画效果,始终显示标签label,并显示 placeholder(占位符)文本。

演示地址:https://codepen.io/MichaelArestad/full/ohLIa

HTML 代码:

  1. <div class="row">
  2. <p>Click every input.</p>
  3. </div>
  4. <div class="row">
  5. <span>
  6. <input class="basic-slide" id="name" type="text" placeholder="Your best name" /><label for="name">Name</label>
  7. </span>
  8. <span>
  9. <input class="basic-slide" id="email" type="text" placeholder="Your favorite email" /><label for="email">Email</label>
  10. </span>
  11. <span>
  12. <input class="basic-slide" id="phone" type="text" placeholder="You can trust us" /><label for="phone">Phone</label>
  13. </span>
  14. </div>
  15. <div class="row">
  16. <span>
  17. <input class="clean-slide" id="age" type="text" placeholder="Go for the high score!" /><label for="age">Age</label>
  18. </span>
  19. <span>
  20. <input class="clean-slide" id="height" type="text" placeholder="Heels count" /><label for="height">Height</label>
  21. </span>
  22. <span>
  23. <input class="clean-slide" id="weight" type="text" placeholder="Go ahead and lie" /><label for="weight">Weight</label>
  24. </span>
  25. </div>
  26. <div class="row">
  27. <span>
  28. <input class="gate" id="class" type="text" placeholder="Wizard!" /><label for="class">Class</label>
  29. </span>
  30. <span>
  31. <input class="gate" id="element" type="text" placeholder="Five to choose from" /><label for="element">Element</label>
  32. </span>
  33. <span>
  34. <input class="gate" id="move" type="text" placeholder="Secret book attack!" /><label for="move">Move</label>
  35. </span>
  36. </div>
  37. <div class="row">
  38. <span>
  39. <input class="skinny" id="english" type="text" placeholder="Do you speak it?" /><label for="english">English</label>
  40. </span>
  41. <span>
  42. <input class="skinny" id="burger" type="text" placeholder="A Royale with cheese?" /><label for="burger">Burger</label>
  43. </span>
  44. <span>
  45. <input class="skinny" id="wallet" type="text" placeholder="Bad Mother****er" /><label for="wallet">Wallet</label>
  46. </span>
  47. </div>
  48. <div class="row">
  49. <span>
  50. <input class="slide-up" id="card" type="text" placeholder="Fund me!" /><label for="card">Credit Card</label>
  51. </span>
  52. <span>
  53. <input class="slide-up" id="expires" type="text" placeholder="Month Day, Year" /><label for="expires">Expires</label>
  54. </span>
  55. <span>
  56. <input class="slide-up" id="security" type="text" placeholder="Public" /><label for="security">Security Code</label>
  57. </span>
  58. </div>
  59. <div class="row">
  60. <span>
  61. <input class="card-slide" id="knock" type="text" placeholder="Who's there?" /><label for="knock">Knock knock</label>
  62. </span>
  63. <span>
  64. <input class="card-slide" id="max" type="text" placeholder="Max who?" /><label for="max">Max</label>
  65. </span>
  66. <span>
  67. <input class="card-slide" id="out" type="text" placeholder="Sunuva..." /><label for="out">Maxed out card ;)</label>
  68. </span>
  69. </div>
  70. <div class="row">
  71. <span>
  72. <input class="swing" id="artist" type="text" placeholder="BO$$" /><label for="artist">Artist</label>
  73. </span>
  74. <span>
  75. <input class="swing" id="song" type="text" placeholder="I don't give a ****" /><label for="song">Song</label>
  76. </span>
  77. <span>
  78. <input class="swing" id="eyes" type="text" placeholder="Crazy" /><label for="eyes">Eyes</label>
  79. </span>
  80. </div>
  81. <div class="row">
  82. <span>
  83. <input class="balloon" id="state" type="text" placeholder="Liquid, solid, gaseous..." /><label for="state">State</label>
  84. </span>
  85. <span>
  86. <input class="balloon" id="planet" type="text" placeholder="Probably Earth" /><label for="planet">Planet</label>
  87. </span>
  88. <span>
  89. <input class="balloon" id="galaxy" type="text" placeholder="Milky Way?" /><label for="galaxy">Galaxy</label>
  90. </span>
  91. </div>

SCSS 代码:

Sass 代码:

  1. @import "compass/css3";
  2. @import url(https://fonts.googleapis.com/css?family=Open+Sans:400,700,600,300,800);
  3. * {
  4. box-sizing: border-box;
  5. }
  6. html,
  7. body {
  8. overflow-x: hidden;
  9. font-family: "Open Sans", sans-serif;
  10. font-weight: 300;
  11. color: #fff;
  12. background: #efefef;
  13. }
  14. @mixin epic-sides() { // https://codepen.io/MichaelArestad/pen/qltuk
  15. position: relative;
  16. z-index: 1;
  17. &:before {
  18. position: absolute;
  19. content: "";
  20. display: block;
  21. top: 0;
  22. left: -5000px;
  23. height: 100%;
  24. width: 15000px;
  25. z-index: -1;
  26. @content;
  27. }
  28. }
  29. .row {
  30. max-width: 800px;
  31. margin: 0 auto;
  32. padding: 60px 30px;
  33. background: #032429;
  34. @include epic-sides() {background: inherit;}
  35. text-align: center;
  36. &:first-child {
  37. padding: 40px 30px;
  38. }
  39. &:nth-child(2),
  40. &:nth-child(8),
  41. &:nth-child(10){
  42. background: #134A46;
  43. }
  44. &:nth-child(3),
  45. &:nth-child(7) {
  46. background: #377D6A;
  47. }
  48. &:nth-child(4),
  49. &:nth-child(6) {
  50. background: #7AB893;
  51. }
  52. &:nth-child(5) {
  53. background: #B2E3AF;
  54. }
  55. span {
  56. position: relative;
  57. display: inline-block;
  58. margin: 30px 10px;
  59. }
  60. }
  61. .basic-slide {
  62. display: inline-block;
  63. width: 215px;
  64. padding: 10px 0 10px 15px;
  65. font-family: "Open Sans", sans;
  66. font-weight: 400;
  67. color: #377D6A;
  68. background: #efefef;
  69. border: 0;
  70. border-radius: 3px;
  71. outline: 0;
  72. text-indent: 70px; // Arbitrary.
  73. transition: all .3s ease-in-out;
  74. &::-webkit-input-placeholder {
  75. color: #efefef;
  76. text-indent: 0;
  77. font-weight: 300;
  78. }
  79. + label {
  80. display: inline-block;
  81. position: absolute;
  82. top: 0;
  83. left: 0;
  84. padding: 10px 15px;
  85. text-shadow: 0 1px 0 rgba(19,74,70,.4);
  86. background: #7AB893;
  87. transition: all .3s ease-in-out;
  88. border-top-left-radius: 3px;
  89. border-bottom-left-radius: 3px;
  90. }
  91. }
  92. .basic-slide:focus,
  93. .basic-slide:active {
  94. color: #377D6A;
  95. text-indent: 0;
  96. background: #fff;
  97. border-top-left-radius: 0;
  98. border-bottom-left-radius: 0;
  99. &::-webkit-input-placeholder {
  100. color: #aaa;
  101. }
  102. + label {
  103. transform: translateX(-100%);
  104. }
  105. }
  106. .clean-slide {
  107. position: relative;
  108. display: inline-block;
  109. width: 215px;
  110. padding: 10px 0 10px 15px;
  111. font-family: "Open Sans", sans;
  112. font-weight: 400;
  113. color: #377D6A;
  114. background: #efefef;
  115. border: 0;
  116. border-radius: 3px;
  117. outline: 0;
  118. text-indent: 60px; // Arbitrary.
  119. transition: all .3s ease-in-out;
  120. &::-webkit-input-placeholder {
  121. color: #efefef;
  122. text-indent: 0;
  123. font-weight: 300;
  124. }
  125. + label {
  126. display: inline-block;
  127. position: absolute;
  128. transform: translateX(0);
  129. top: 0;
  130. left: 0;
  131. bottom: 0;
  132. padding: 13px 15px;
  133. font-size: 11px;
  134. font-weight: 700;
  135. text-transform: uppercase;
  136. color: #032429;
  137. text-align: left;
  138. text-shadow: 0 1px 0 rgba(255,255,255,.4);
  139. transition: all .3s ease-in-out, color .3s ease-out;
  140. border-top-left-radius: 3px;
  141. border-bottom-left-radius: 3px;
  142. overflow: hidden;
  143. &:after {
  144. content: "";
  145. position: absolute;
  146. top: 0;
  147. right: 100%;
  148. bottom: 0;
  149. width: 100%;
  150. background: #7AB893;
  151. z-index: -1;
  152. transform: translate(0);
  153. transition: all .3s ease-in-out;
  154. border-top-left-radius: 3px;
  155. border-bottom-left-radius: 3px;
  156. }
  157. }
  158. }
  159. .clean-slide:focus,
  160. .clean-slide:active {
  161. color: #377D6A;
  162. text-indent: 0;
  163. background: #fff;
  164. border-top-left-radius: 0;
  165. border-bottom-left-radius: 0;
  166. &::-webkit-input-placeholder {
  167. color: #aaa;
  168. }
  169. + label {
  170. color: #fff;
  171. text-shadow: 0 1px 0 rgba(19,74,70,.4);
  172. transform: translateX(-100%);
  173. &:after {
  174. transform: translate(100%);
  175. }
  176. }
  177. }
  178. .gate {
  179. display: inline-block;
  180. width: 215px;
  181. padding: 10px 0 10px 15px;
  182. font-family: "Open Sans", sans;
  183. font-weight: 400;
  184. color: #377D6A;
  185. background: #efefef;
  186. border: 0;
  187. border-radius: 3px;
  188. outline: 0;
  189. text-indent: 65px; // Arbitrary.
  190. transition: all .3s ease-in-out;
  191. &::-webkit-input-placeholder {
  192. color: #efefef;
  193. text-indent: 0;
  194. font-weight: 300;
  195. }
  196. + label {
  197. display: inline-block;
  198. position: absolute;
  199. top: 0;
  200. left: 0;
  201. padding: 10px 15px;
  202. text-shadow: 0 1px 0 rgba(19,74,70,.4);
  203. background: #7AB893;
  204. transition: all .4s ease-in-out;
  205. border-top-left-radius: 3px;
  206. border-bottom-left-radius: 3px;
  207. transform-origin: left bottom;
  208. z-index: 99;
  209. &:before,
  210. &:after {
  211. content: "";
  212. position: absolute;
  213. top: 0;
  214. right: 0;
  215. bottom: 0;
  216. left: 0;
  217. border-radius: 3px;
  218. background: #377D6A;
  219. transform-origin: left bottom;
  220. transition: all .4s ease-in-out;
  221. pointer-events: none;
  222. z-index: -1;
  223. }
  224. &:before {
  225. background: rgba(3,36,41,.2);
  226. z-index: -2;
  227. right: 20%;
  228. }
  229. }
  230. }
  231. span:nth-child(2) .gate {
  232. text-indent: 85px;
  233. }
  234. span:nth-child(2) .gate:focus,
  235. span:nth-child(2) .gate:active{
  236. text-indent: 0;
  237. }
  238. .gate:focus,
  239. .gate:active {
  240. color: #377D6A;
  241. text-indent: 0;
  242. background: #fff;
  243. border-top-right-radius: 3px;
  244. border-bottom-right-radius: 3px;
  245. &::-webkit-input-placeholder {
  246. color: #aaa;
  247. }
  248. + label {
  249. transform: rotate(-66deg);
  250. border-radius: 3px;
  251. &:before {
  252. transform: rotate(10deg);
  253. }
  254. }
  255. }
  256. .skinny {
  257. display: inline-block;
  258. width: 215px;
  259. padding: 10px 0 10px 15px;
  260. font-family: "Open Sans", sans;
  261. font-weight: 400;
  262. color: #377D6A;
  263. background: #efefef;
  264. border: 0;
  265. border-radius: 3px;
  266. outline: 0;
  267. text-indent: 75px; // Arbitrary.
  268. transition: all .3s ease-in-out;
  269. &::-webkit-input-placeholder {
  270. color: #efefef;
  271. text-indent: 0;
  272. font-weight: 300;
  273. }
  274. + label {
  275. display: inline-block;
  276. position: absolute;
  277. transform: translateX(0);
  278. top: 0;
  279. left: 0;
  280. padding: 10px 15px;
  281. text-shadow: 0 1px 0 rgba(19,74,70,.4);
  282. transition: all .3s ease-in-out;
  283. border-top-left-radius: 3px;
  284. border-bottom-left-radius: 3px;
  285. overflow: hidden;
  286. &:before,
  287. &:after {
  288. content: "";
  289. position: absolute;
  290. right: 0;
  291. left: 0;
  292. z-index: -1;
  293. transition: all .3s ease-in-out;
  294. }
  295. &:before {
  296. // Skinny bit here
  297. top: 5px;
  298. bottom: 5px;
  299. background: #377D6A; // change this to #134A46
  300. border-top-left-radius: 3px;
  301. border-bottom-left-radius: 3px;
  302. }
  303. &:after {
  304. top: 0;
  305. bottom: 0;
  306. background: #377D6A;
  307. }
  308. }
  309. }
  310. .skinny:focus,
  311. .skinny:active {
  312. color: #377D6A;
  313. text-indent: 0;
  314. background: #fff;
  315. &::-webkit-input-placeholder {
  316. color: #aaa;
  317. }
  318. + label {
  319. transform: translateX(-100%);
  320. &:after {
  321. transform: translateX(100%);
  322. }
  323. }
  324. }
  325. .slide-up {
  326. display: inline-block;
  327. width: 215px;
  328. padding: 10px 0 10px 15px;
  329. font-family: "Open Sans", sans;
  330. font-weight: 400;
  331. color: #377D6A;
  332. background: #efefef;
  333. border: 0;
  334. border-radius: 3px;
  335. outline: 0;
  336. text-indent: 80px; // Arbitrary.
  337. transition: all .3s ease-in-out;
  338. &::-webkit-input-placeholder {
  339. color: #efefef;
  340. text-indent: 0;
  341. font-weight: 300;
  342. }
  343. + label {
  344. display: inline-block;
  345. position: absolute;
  346. transform: translateX(0);
  347. top: 0;
  348. left: 0;
  349. padding: 10px 15px;
  350. text-shadow: 0 1px 0 rgba(19,74,70,.4);
  351. transition: all .3s ease-in-out;
  352. border-top-left-radius: 3px;
  353. border-bottom-left-radius: 3px;
  354. overflow: hidden;
  355. &:before,
  356. &:after {
  357. content: "";
  358. position: absolute;
  359. right: 0;
  360. left: 0;
  361. z-index: -1;
  362. transition: all .3s ease-in-out;
  363. }
  364. &:before {
  365. // Skinny bit here
  366. top: 6px;
  367. left: 5px;
  368. right: 5px;
  369. bottom: 6px;
  370. background: #377D6A; // change this to #134A46
  371. }
  372. &:after {
  373. top: 0;
  374. bottom: 0;
  375. background: #377D6A;
  376. }
  377. }
  378. }
  379. span:nth-child(1) .slide-up {
  380. text-indent: 105px;
  381. }
  382. span:nth-child(3) .slide-up {
  383. text-indent: 125px;
  384. }
  385. span:nth-child(1) .slide-up:focus,
  386. span:nth-child(1) .slide-up:active,
  387. span:nth-child(3) .slide-up:focus,
  388. span:nth-child(3) .slide-up:active {
  389. text-indent: 0;
  390. }
  391. .slide-up:focus,
  392. .slide-up:active {
  393. color: #377D6A;
  394. text-indent: 0;
  395. background: #fff;
  396. &::-webkit-input-placeholder {
  397. color: #aaa;
  398. }
  399. + label {
  400. transform: translateY(-100%);
  401. &:before {
  402. border-radius: 5px;
  403. }
  404. &:after {
  405. transform: translateY(100%);
  406. }
  407. }
  408. }
  409. .card-slide {
  410. display: inline-block;
  411. width: 215px;
  412. padding: 10px 0 10px 15px;
  413. font-family: "Open Sans", sans;
  414. font-weight: 400;
  415. color: #377D6A;
  416. background: #efefef;
  417. border: 0;
  418. border-radius: 3px;
  419. outline: 0;
  420. text-indent: 115px; // Arbitrary.
  421. transition: all .3s ease-in-out;
  422. &::-webkit-input-placeholder {
  423. color: #efefef;
  424. text-indent: 0;
  425. font-weight: 300;
  426. }
  427. + label {
  428. display: block;
  429. position: absolute;
  430. top: 0;
  431. left: 0;
  432. padding: 10px 15px;
  433. text-shadow: 0 1px 0 rgba(19,74,70,.4);
  434. background: #7AB893;
  435. transition: all .3s ease-in-out;
  436. border-top-left-radius: 3px;
  437. border-bottom-left-radius: 3px;
  438. transform-origin: right center;
  439. transform: perspective(300px) scaleX(1) rotateY(0deg);
  440. }
  441. }
  442. span:nth-child(2) .card-slide {
  443. text-indent: 55px;
  444. }
  445. span:nth-child(3) .card-slide {
  446. text-indent: 150px;
  447. }
  448. span:nth-child(2) .card-slide:focus,
  449. span:nth-child(2) .card-slide:active,
  450. span:nth-child(3) .card-slide:focus,
  451. span:nth-child(3) .card-slide:active {
  452. text-indent: 0;
  453. }
  454. .card-slide:focus,
  455. .card-slide:active {
  456. color: #377D6A;
  457. text-indent: 0;
  458. background: #fff;
  459. border-top-left-radius: 0;
  460. border-bottom-left-radius: 0;
  461. &::-webkit-input-placeholder {
  462. color: #aaa;
  463. }
  464. + label {
  465. transform: perspective(600px) translateX(-100%) rotateY(80deg);
  466. }
  467. }
  468. .swing {
  469. display: inline-block;
  470. width: 215px;
  471. padding: 10px 0 10px 15px;
  472. font-family: "Open Sans", sans;
  473. font-weight: 400;
  474. color: #377D6A;
  475. background: #efefef;
  476. border: 0;
  477. border-radius: 3px;
  478. outline: 0;
  479. text-indent: 60px; // Arbitrary.
  480. transition: all .3s ease-in-out;
  481. &::-webkit-input-placeholder {
  482. color: #efefef;
  483. text-indent: 0;
  484. font-weight: 300;
  485. }
  486. + label {
  487. display: inline-block;
  488. position: absolute;
  489. top: 0;
  490. left: 0;
  491. padding: 10px 15px;
  492. text-shadow: 0 1px 0 rgba(19,74,70,.4);
  493. background: #7AB893;
  494. border-top-left-radius: 3px;
  495. border-bottom-left-radius: 3px;
  496. transform-origin: 2px 2px;
  497. transform: rotate(0);
  498. // There should be a better way
  499. animation: swing-back .4s 1 ease-in-out;
  500. }
  501. }
  502. @keyframes swing {
  503. 0% {
  504. transform: rotate(0);
  505. }
  506. 20% {
  507. transform: rotate(116deg);
  508. }
  509. 40% {
  510. transform: rotate(60deg);
  511. }
  512. 60% {
  513. transform: rotate(98deg);
  514. }
  515. 80% {
  516. transform: rotate(76deg);
  517. }
  518. 100% {
  519. transform: rotate(82deg);
  520. }
  521. }
  522. @keyframes swing-back {
  523. 0% {
  524. transform: rotate(82deg);
  525. }
  526. 100% {
  527. transform: rotate(0);
  528. }
  529. }
  530. .swing:focus,
  531. .swing:active {
  532. color: #377D6A;
  533. text-indent: 0;
  534. background: #fff;
  535. border-top-left-radius: 0;
  536. border-bottom-left-radius: 0;
  537. &::-webkit-input-placeholder {
  538. color: #aaa;
  539. }
  540. + label {
  541. animation: swing 1.4s 1 ease-in-out;
  542. transform: rotate(82deg);
  543. }
  544. }
  545. .balloon {
  546. // As suggested by https://twitter.com/dbox/status/365888496486985728
  547. display: inline-block;
  548. width: 215px;
  549. padding: 10px 0 10px 15px;
  550. font-family: "Open Sans", sans;
  551. font-weight: 400;
  552. color: #377D6A;
  553. background: #efefef;
  554. border: 0;
  555. border-radius: 3px;
  556. outline: 0;
  557. text-indent: 60px; // Arbitrary.
  558. transition: all .3s ease-in-out;
  559. &::-webkit-input-placeholder {
  560. color: #efefef;
  561. text-indent: 0;
  562. font-weight: 300;
  563. }
  564. + label {
  565. display: inline-block;
  566. position: absolute;
  567. top: 8px;
  568. left: 0;
  569. bottom: 8px;
  570. padding: 5px 15px;
  571. color: #032429;
  572. font-size: 11px;
  573. font-weight: 700;
  574. text-transform: uppercase;
  575. text-shadow: 0 1px 0 rgba(19,74,70,0);
  576. transition: all .3s ease-in-out;
  577. border-radius: 3px;
  578. background: rgba(122,184,147,0);
  579. &:after {
  580. position: absolute;
  581. content: "";
  582. width: 0;
  583. height: 0;
  584. top: 100%;
  585. left: 50%;
  586. margin-left: -3px;
  587. border-left: 3px solid transparent;
  588. border-right: 3px solid transparent;
  589. border-top: 3px solid rgba(122,184,147,0);
  590. transition: all .3s ease-in-out;
  591. }
  592. }
  593. }
  594. .balloon:focus,
  595. .balloon:active {
  596. color: #377D6A;
  597. text-indent: 0;
  598. background: #fff;
  599. &::-webkit-input-placeholder {
  600. color: #aaa;
  601. }
  602. + label {
  603. color: #fff;
  604. text-shadow: 0 1px 0 rgba(19,74,70,.4);
  605. background: rgba(122,184,147,1);
  606. transform: translateY(-40px);
  607. &:after {
  608. border-top: 4px solid rgba(122,184,147,1);
  609. }
  610. }
  611. }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号