Seleccionar página

Lately we found ton of new style search form crafted beautifully using CSS3 and JavaScript.Apple’s for example, widen the input field when it receive focus from user. The question is “how far we can go for styling search form?”, in this tutorial we are going to move search form to the next level using jQuery & CSS3.

Demo 

Simple Style

First to go we will create a simple animated search form that show only search button with search image on it. When the button receives a click, the input field will widen while the search image will move to left filling the left blank space before input text.

Based on the picture above, the HTML element will consists of 4 elements, a div for elements wrapper, an input text, an input button for search button and an image search. All of them will be stacked each other where the div wrapper is at the very bottom of the stack and image search at the very top. Here’s the HTML and CSS :

  1. <div class=”wrapper-simple”>
  2.     <input type=”text” value=”Type and enter…”>
  3.     <input type=”submit” value=””>
  4.     <img src=”images/search-icon-big.png”>
  5. </div>
  1. .wrapper-simple {
  2.     text-align: center;
  3.     margin: 0 auto;
  4.     display: block;
  5.     width: 60px;
  6.     height: 45px;
  7.     padding: 10px 5px;
  8.     background: -webkit-gradient(linear, left top, left bottombottom, from(#f5fafe), to(#e2edf4));
  9.     border-radius: 5px;
  10.     box-shadow: inset rgba(255, 254, 255, 1) 0 0.1em 2px,
  11.                     #9bb6c9 0 1px 2px;
  12.     position: relative;
  13. }
  14. .wrapper-simple input[type=submit] {
  15.     margin-top: .2em;
  16.     z-index: 2;
  17.     position: relative;
  18.     vertical-align: top;
  19.     height: 43px;
  20.     min-width: 55px;
  21.     border-radius: 3px;
  22.     border: 1px solid #aa7818;
  23.     background: -webkit-gradient(linear, left top, left bottombottom, from(#ffb700), to(#ff8c00));
  24.     box-shadow: inset rgba(255, 255, 255, .5) 0 0.1em 0.1em;
  25.     cursor: pointer;
  26. }
  27.     .wrapper-simple input[type=submit]:active {
  28.         box-shadow: inset rgba(0,0,0,.4) 0 1px 1px;
  29.     }
  30.     .wrapper-simple input[type=submit]:hover {
  31.         background: -webkit-gradient(linear, left top, left bottombottom, from(#ffcb48), to(#ff9c23));
  32.     }
  33. .wrapper-simple input[type=text] {
  34.     font-family: Arial;
  35.     font-weight: bold;
  36.     color: #1a3d51;
  37.     background: #d8e6ef;
  38.     border-radius:2px;
  39.     padding: 10px 10px 15px 10px;
  40.     width: 250px;
  41.     border: 0;
  42.     font-size: 16px;
  43.     text-shadow: rgba(255, 255, 255, 0.7) 1px 1px 1px;
  44.     box-shadow: inset rgba(0,0,0,.4) 0 1px 1px;
  45.     position: absolute;
  46.     width: 1px;
  47.     z-index: 2;
  48.     padding-left: 2em;
  49.     margin-left: .2em;
  50. }
  51. .wrapper-simple img {
  52.     position: absolute;
  53.     top: 1.5em;
  54.     left: 1.5em;
  55.     z-index: 4;
  56. }

Now we will read the click event on search button and animate the form using jQuery.

  1. $(‘.wrapper-simple input[type=submit]’).toggle(function(){
  2.     $(‘.wrapper-simple’).animate({‘width’:’300px’})
  3.       .end().find(‘.wrapper-simple input[type=text]’).animate({‘width’: ‘250px’})
  4.       .end().find(‘.wrapper-simple img’).animate({‘marginLeft’: ‘-5px’})
  5.       .end().find(this).animate({‘marginLeft’:’22em’}).attr(‘value’, ‘CANCEL’);
  6. }, function() {
  7.     $(‘.wrapper-simple’).animate({‘width’:’60px’})
  8.       .end().find(‘.wrapper-simple input[type=text]’).animate({‘width’: ‘1px’})
  9.       .end().find(‘.wrapper-simple img’).animate({‘marginLeft’: ‘0’})
  10.       .end().find(this).animate({‘marginLeft’:’0′}).attr(‘value’, ”);
  11. });

The script toggling user’s click. First click will widen the div wrapper and input text, move the search button to the edge of input text and move image search to fill the left blank space before input text. Second click will revert to normal.

Cube Style

After making simple animated search form, actually we aren’t moving too far. So we will make another experiment, how about show the search form rotating like a cube? Well, CSS3 properties like transform, perspective and animation seems very helpful. The thing that we must give more attention that the following code and demo currently work only on Safari 5+ and Firefox 11.0 (Mac).

Making a cube is a bit tricky, for vertical rotate and one direction (up to down) we can create a cube by making 2 div, first div as a front side of the cube and the second div as a top side. The top side div must be rotated so it will be like coming from the top when the cube rotating.

The structure of elements will be :

  1. <div class=”wrapper1″>
  2.     <div class=”content-wrapper1″>
  3.         <div class=”search-button1″>
  4.             <span><img src=”images/search-icon.png”></span>
  5.         </div>
  6.         <div class=”search-box1″>
  7.             <input type=”text” value=”Type your keyword…”>
  8.             <img src=”images/close.png”>
  9.         </div>
  10.     </div>
  11. </div>

wrapper1 acting as wrapper for all elements inside, content-wrapper1 also as a wrapper that will be rotated, search button-1 as top side and search-box1 as front side. The styling :

  1. .wrapper1 {
  2.     display: block;
  3.     width: 300px;
  4.     margin: 0 auto;
  5.     -webkit-perspective : 1200px;
  6. }
  7.     .search-button1 span {
  8.         display: block;
  9.         margin: 0 auto;
  10.         background: #643904;
  11.         border-radius: 30px;
  12.         width: 30px;
  13.         height: 30px;
  14.         box-shadow: rgba(255,255,255,.3) 0 1px 0px;
  15.     }
  16.         .search-button1 span img {
  17.             vertical-align: middle;
  18.             padding-top: 7px;
  19.         }
  20.     .search-button1:hover {
  21.         background: -webkit-gradient(linear, left top, left bottombottom, from(#ffcb48), to(#ff9c23));
  22.     }
  23.     .search-button1:active {
  24.         margin-top: 0.2em;
  25.         box-shadow: inset rgba(255, 254, 255, 0.2) 0 0.3em .3em,
  26.                         #8e620e 0 0.3em 0,
  27.                         rgba(0, 0, 0, 0.2) 0 0.5em 3px;
  28.     }
  29. .search-box1 {
  30.     margin-top: -.6em;
  31.     display: none;
  32.     position: absolute;
  33.     width: 300px;
  34.     height: 50px;
  35.     padding: 10px 6px;
  36.     background: -webkit-gradient(linear, left top, left bottombottom, from(#f5fafe), to(#e2edf4));
  37.     border: 1px solid #9bb6c9;
  38.     border-bottom: 1px solid rgba(255,255,255,0.2);
  39.     border-radius: 5px;
  40.     box-shadow: inset rgba(255, 254, 255, 0.2) 0 0.3em .3em,
  41.                     #899faf 0 .5em 0px,
  42.                     rgba(0, 0, 0, 0.2) 0 .9em 3px;
  43.     -webkit-transform:  rotate3d(1,0,0,90deg)
  44.                                 translateZ(20px);
  45. }
  46.     .search-box1 input {
  47.         font-family: Arial;
  48.         font-weight: bold;
  49.         color: #1a3d51;
  50.         background: #d8e6ef;
  51.         border-radius:2px;
  52.         padding: 10px 10px 15px 10px;
  53.         width: 250px;
  54.         border: 0;
  55.         font-size: 16px;
  56.         text-shadow: rgba(255, 255, 255, 0.7) 1px 1px 1px;
  57.         box-shadow: inset rgba(0,0,0,.4) 0 1px 1px;
  58.     }
  59.         .search-box1 input:focus {
  60.             outline: none;
  61.         }
  62.     .search-box1 img {
  63.         opacity: .5;
  64.         position: absolute;
  65.         margin: .6em 0 0 -2em;
  66.         cursor: pointer;
  67.         -webkit-transition: 0.5s linear;
  68.     }
  69.         .search-box1 img:hover {
  70.             opacity: 1;
  71.         }
  72. .hide-search-button {
  73.     display: none;
  74. }
  75. .show-search-button {
  76.     display: block;
  77. }
  78. .show-search-box {
  79.     display: block;
  80. }
  81. .showed-search-box {
  82.     display: block;
  83.     -webkit-transform: rotate3d(1,0,0,0deg);
  84. }
  85. .hidden-search-box {
  86.     -webkit-transform:  rotate3d(1,0,0,90deg)
  87.                                 translateZ(25px);
  88. }
  89. .switch-show {
  90.     height: 50px;
  91.     -webkit-transform-style: preserve-3d;
  92.     -webkit-animation: showBox 0.5s ease-in-out;
  93. }
  94. .switch-hide {
  95.     height: 50px;
  96.     -webkit-transform-style: preserve-3d;
  97.     -webkit-animation: hideBox 0.5s ease-in-out;
  98. }
  99. .switch-show {
  100.     height: 50px;
  101.     -webkit-transform-style: preserve-3d;
  102.     -webkit-animation: showBox 0.5s ease-in-out;
  103. }
  104. .switch-hide {
  105.     height: 50px;
  106.     -webkit-transform-style: preserve-3d;
  107.     -webkit-animation: hideBox 0.5s ease-in-out;
  108. }
  109. @-webkit-keyframes showBox{
  110.     0% { -webkit-transform: rotate3d(1,0,0,0); }
  111.     100% { -webkit-transform: rotate3d(1,0,0,-90deg); }
  112. }
  113. @-webkit-keyframes hideBox{
  114.     0% { -webkit-transform: rotate3d(1,0,0,-90deg); }
  115.     100% { -webkit-transform: rotate3d(1,0,0,0); }
  116. }

We’re a half set up, now we will read the click event from the user and rotate the cube.

  1. $(‘.search-button1’).click(function() {
  2.     $(‘.content-wrapper1’).addClass(‘switch-show’);
  3.     $(‘.search-box1’).addClass(‘show-search-box’);
  4.     setTimeout(function(){
  5.         $(‘.content-wrapper1’).removeClass(‘switch-show’);
  6.         $(‘.search-button1’).removeClass(‘show-search-button’).addClass(‘hide-search-button’);
  7.         $(‘.search-box1’).addClass(‘showed-search-box’);
  8.     },480);
  9. });
  10. $(‘.search-box1 img’).click(function() {
  11.     $(‘.search-button1’).removeClass(‘hide-search-button’);
  12.     $(‘.search-box1’).addClass(‘hidden-search-box’);
  13.     $(‘.content-wrapper1’).addClass(‘switch-hide’);
  14.     setTimeout(function(){
  15.         $(‘.content-wrapper1’).removeClass(‘switch-hide’);
  16.         $(‘.search-button1’).removeClass(‘show-search-button’);
  17.         $(‘.search-box1’).removeClass(‘show-search-box showed-search-box hidden-search-box’);
  18.     },480);
  19. });

The script above act as an animation delegator, all animation handled by CSS. Since the CSS animation we describe before only works once and revert to the normal state, we force the current showed side to visible and other side to hidden until the animation works before revert back, this is do the trick.

Cube Style With Animate

At some point we might animate the front side before rotate the cube. Then we reduce the front side’s width and widen it before rotate.

  1. $(‘.search-button2’).click(function() {
  2.     $(‘.arrow’).hide();
  3.     $(this).stop().animate({‘width’:’300px’}, 500, function() {
  4.         $(‘.content-wrapper2’).addClass(‘switch-show’);
  5.         $(‘.search-box2’).addClass(‘show-search-box’);
  6.         setTimeout(function(){
  7.             $(‘.content-wrapper2’).removeClass(‘switch-show’);
  8.             $(‘.search-button2’).removeClass(‘show-search-button’).addClass(‘hide-search-button’);
  9.             $(‘.search-box2’).addClass(‘showed-search-box’);
  10.         },480);
  11.     });
  12. });
  13. $(‘.search-box2 img’).click(function() {
  14.     $(‘.search-button2’).removeClass(‘hide-search-button’);
  15.     $(‘.search-box2’).addClass(‘hidden-search-box’);
  16.     $(‘.content-wrapper2’).addClass(‘switch-hide’);
  17.     $(‘.search-button2’).addClass(‘show-search-button’).stop().delay(500).animate({‘width’:’50px’}, 500, function() {
  18.         $(‘.content-wrapper2’).removeClass(‘switch-hide’);
  19.         $(‘.search-button2’).removeClass(‘show-search-button’);
  20.         $(‘.search-box2’).removeClass(‘show-search-box showed-search-box hidden-search-box’);
  21.         $(‘.arrow’).show();
  22.     });
  23. });

We use jQuery’s animate() function to widen the front side’s width and reduce it back when user close the form.

Conclusion

CSS3 and jQuery combination help us improve current web user interface into unlimited state, depend on our imagination. Did I missed something? I’d love to hear.

posted by Hidayat Sagita

 


Powered with Custom Code Adder