Jeremy Lynch
Jeremy Lynch

Reputation: 7210

Expand text form on hover with jquery

I have a bootstrap text input and with jquery I would like to:

  1. Expand textform on hover and retract on hover out. ✓ (already done in this jsfiddle)

  2. When textform is focused/selected (insertion point is inside text box), the form is expanded and does not retract on cursor hover out. ✗ (to be compelted)

Html

<div class="col-md-3 col-sm-3">
  <div class="input-group">
    <input class="form-control" placeholder="Search"></input>
  </div>
</div>

Javascript

$(function(){
  var form = $('.input-group');
  form.mouseenter(function(){
    form.animate({ "width": "+=" + form.width() * 1.4}, "slow");
  }).mouseout(function(){
    form.animate({ "width": '100%'}, "slow");
  });
});

Upvotes: 3

Views: 2444

Answers (5)

rafaelcastrocouto
rafaelcastrocouto

Reputation: 12161

Solved http://jsfiddle.net/mY8uU/6/

$(function(){
  var form = $('.input-group'), w = form.width();
  form.mouseenter(function(){
    form.animate({'width': '100%'}, 'slow');
  }).mouseout(function(){         
    if(!$('.form-control:focus').length) form.animate({'width': w}, 'slow');
  })
  $('.form-control').on('blur', function(){
    form.animate({'width': w}, 'slow');
  });
});

Upvotes: 2

Neto Braghetto
Neto Braghetto

Reputation: 1381

try this

$(function(){
  var form = $('.input-group');
  var start_width = form.width();

  form.find('#input-target').mouseenter(function(){
    form.animate({ "width": "+=" + start_width * 1.4}, "slow");
  }).mouseout(function(){
    form.animate({ "width": start_width+'px'}, "slow");
  }).focus(function(){
      $(this).unbind('mouseout');
      $(this).unbind('mouseenter');
  }).blur(function(){
      form.animate({ "width": start_width+'px'}, "slow");
      $(this).mouseout(function(){
        form.animate({ "width": start_width+'px'}, "slow");
      }).mouseenter(function(){
    form.animate({ "width": "+=" + start_width * 1.4}, "slow");
      });
  });
});

see at this http://jsfiddle.net/ZQ3nZ/

Upvotes: 3

PatrikS
PatrikS

Reputation: 63

Is CSS3 out of the question?

In that case you can do without Javascript and use

.textbox{
   width: 200px;
   transition: width 1S;
}

.textbox:hover
{
    width:250px;
    transition: width 1S;
}

This is just an example, but you can change it to your liking.

Upvotes: 2

Kippie
Kippie

Reputation: 3820

You could always employ some CSS3:

input#search {
  transition: width .5s;
  -webkit-transition: width .5s;
}
input#search:hover, input#search:focus { width:300px; }

Upvotes: 1

Black Ops
Black Ops

Reputation: 374

this one worked for me:

$(function(){
  var form = $('.input-group');
  form.mouseenter(function(){
    form.animate({ "width":  form.width() * 1.8}, "slow");
  }).mouseout(function(){
    form.animate({ "width":  form.width() / 1.8}, "slow");
  });
});

jsfiddle: http://jsfiddle.net/mY8uU/2/

Upvotes: 0

Related Questions