使用jquery在悬停时展开文本表单

Expand text form on hover with jquery

本文关键字:文本 表单 jquery 悬停 使用      更新时间:2024-06-24

我有一个引导程序文本输入,使用jquery我想:

  1. 悬停时展开文本表单,悬停时收回。✓(已经在这个jsfiddle中完成)

  2. 当聚焦/选择文本表单时(插入点在文本框内),当光标悬停在外时,表单将展开且不会缩回。✗

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");
  });
});

尝试这个

$(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");
      });
  });
});

看看这个http://jsfiddle.net/ZQ3nZ/

已解决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');
  });
});

CSS3是不可能的吗?

在这种情况下,您可以不使用Javascript并使用

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

这只是一个例子,但你可以根据自己的喜好进行更改。

您总是可以使用一些CSS3:

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

这个对我有用:

$(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/