Showing posts with label JavaScript. Show all posts
Showing posts with label JavaScript. Show all posts

Saturday, May 24, 2014

Expanding Search Bar Using Only CSS

So I needed a search bar, but I wanted something specific, I wanted something like this:


Which I got from here: http://tympanus.net/codrops/2013/06/26/expanding-search-bar-deconstructed/ - great site by the way!

It really looks and works great, but I just felt that it was way too much for something as simple as a search bar that expands.

At first I thought I could just rework the code I found on Tympanus, but eventually I just re-did it all. This is really not going to be a long post, I'm just going to show you the code.

This is all the HTML you'll need, notice that there's no hint of JavaScript anywhere:

<html>
 <head>
  <title>Expanding Search Bar CSS Only</title>
  <link href="css/style.css" rel="Stylesheet"></link>
 </head>
<body>

 <h1>Expanding Search Bar CSS Only</h1>

<div id="search-bar">
  <form id="search-form">
   <input autocomplete="off" class="search-field" id="search-text" name="search" placeholder="Search…" type="text" />
   <input style="display: none;" type="submit" value="" />
  </form>
</div>

</body>
</html>


And here's the CSS:
body, html {

    background: #285e8e;
    color: #252525;
    font-family: Helvetica, Arial, sans-serif;
    font-weight: bold;
    font-size: 18px;

}

h1 {
    color: #fff;
    margin-left: 20px;
}


input::-webkit-input-placeholder { color:transparent; }
input::-moz-placeholder { color:transparent; }
input:-ms-input-placeholder { color:transparent; }
input::placeholder { color:transparent; }

input:focus::-webkit-input-placeholder { color: #ccc; }
input:focus:-moz-placeholder { color: #ccc; }
input:focus::-moz-placeholder { color: #ccc; }
input:focus:placeholder { color:#ccc; }

input:hover::-webkit-input-placeholder { color: #ccc; }
input:hover:-moz-placeholder { color: #ccc; }
input:hover::-moz-placeholder { color: #ccc; }
input:hover:placeholder { color:#ccc; }

#search-button-1 {
    position: absolute;
    float: right;
    right: 0px;
    display: block;
    width: 52px;
    height: 53px;
    background-color: #eb9316;
}

/** Search Fields **/
.search-field {

    float: right;
    display: block;
    width: 52px;
    padding: 10px 10px;
    background-color: #fff;
    background-image: url('../images/search-icon-box-blue.png');
    background-repeat: no-repeat;
    background-position:right;
    font-size: 22px;
    border: 0px solid #fff;
    color: rgba(255, 255, 255, 0);

    /* CSS3 Box Sizing */
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    -ms-box-sizing: border-box;
    box-sizing: border-box;

    /* CSS3 Rounded Corners */
    border-radius: 2px;

    /* CSS3 Transition (Expanding Effect) */
    -webkit-transition: all 0.4s ease;
    -moz-transition: all 0.4s ease;
    -o-transition: all 0.4s ease;
    -ms-transition: all 0.4s ease;
    transition: all 0.4s ease;

    -webkit-backface-visibility: hidden;
   
}

/** Search Field 1 Focus (Full Width) **/

.search-field:focus, .search-field:hover {

    outline: none;
    min-width: 200px;
    max-width: 500px;
    width: 40%;
    color: #777;

    /* CSS3 Outer Shadow */
    -webkit-box-shadow: 0 0 0 2px #999;
    -moz-box-shadow: 0 0 0 2px #999;
    box-shadow: 0 0 2px #999;

}

/** Don't want the reset auto-button to show up over the button image **/
.search-field::-ms-clear {
  width : 0;
  height: 0;
}

/** Make text visible again on focus or hover **/
#search-text:focus, #search-text:hover {
    color: #777;
}

#search-bar {
    margin-right: 40%;
}
This is what it ended up looking like:





There's a couple of things that are happening in the CSS. The important things to watch out for is that the placeholder text as well as any text you typed in, is set to transparent when the input is not expanded, this is so that the text does not show up over the search icon. The text is made visible on focus or hover.


Pretty cool... There's lots of little things that can be added, but this is a good basis for me to work from.

Here's a CodePen.io link if you want to play around with the code a bit and see how it works. This version has a snippet of plain JavaScript to launch a form submit when the input box is clicked on when there's text present: http://codepen.io/anon/pen/tEBnw



Hope it helps someone!



Sunday, April 28, 2013

iPad & iPhone Viewport Rotate Screw-up

If you've designed a mobile site before (or if you're about to...) you would have seen or used this before:

<meta name="viewport" content="width=device-width, initial-scale=1">
That's all fine & dandy, until someone rotates their screen and then flips it back again. This is one way to solve that little irritating issue:

You can disable scalability with javascript until gesturestart with this script:

if (navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPad/i)) {
    var viewportmeta = document.querySelector('meta[name="viewport"]');
    if (viewportmeta) {
        viewportmeta.content = 'width=device-width, minimum-scale=1.0, maximum-scale=1.0, initial-scale=1.0';
        document.body.addEventListener('gesturestart', function () {
            viewportmeta.content = 'width=device-width, minimum-scale=0.25, maximum-scale=1.6';
        }, false);
    }
}


Saturday, April 27, 2013

Prevent Pinch-zoom on a Div or Whole Body


Sometimes you just don't want people to go pinch-zoom your site into oblivion, luckily, there is a way to do that :-)

     $("body").bind('touchstart', function preventZoom(e) {
        var t2 = e.timeStamp
          , t1 = $(this).data('lastTouch') || t2
          , dt = t2 - t1
          , fingers = e.originalEvent.touches.length;
        $(this).data('lastTouch', t2);
        if (!dt || dt > 500 || fingers > 1) return; // not double-tap
        e.preventDefault(); // double tap - prevent the zoom
        // also synthesize click events we just swallowed up
        $(this).trigger('click').trigger('click');
      });

Mobile Device Detection & Touch Screen Desktops

Not too long ago, I thought that this type of script was a pretty solid way of making sure that you redirect your traffic to a mobile site:

if( ('querySelector' in document && 'localStorage' in window && 'addEventListener' in window && ('ontouchstart' in window || window.DocumentTouch && document instanceof DocumentTouch) ) || navigator.userAgent.indexOf('IEMobile') > 0){

location.replace('YOUR MOBILE SITE'); }

I originally got it from here: http://roughlybrilliant.com/jquery_mobile_best_practices#9

The question I've been asking myself lately is, with touch interfaces coming to desktop screens, this type of code that detects a touch interface will no longer just apply to mobile screens.

Due to this & the fact that mobile browsing growing exponentially, the best approach I believe is to just go for Responsive Design & one of the easier ways to do that is to use something like Twitter Bootstrap - http://twitter.github.io/bootstrap/scaffolding.html#responsive


Happy Coding!