繁体   English   中英

使用jQuery扩展onClick的输入框

[英]Input boxes that expand onClick with jQuery

我在我的页面上有这个input[type="text"] ,我已经工作了一段时间。 我想做的是当用户点击它时创建它扩展的效果。 此外,我希望它在鼠标熄灭时恢复正常宽度。

我的代码到目前为止:

 $(document).ready(function() { $("input").click(function() { var i; for (i = 250; i < 501; i++) { $(this).css("width", (i.toString + "px")) } }) }) 
 input[type="text"] { background-color: #000; color: #fff; border-radius: 10px; border-style: none; height: 50px; width: 250px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text"> 

我在这里使用了.click() jQuery函数。 是否还有其他可以检测元素中光标的函数? .cursorIn()这样的东西,如果存在的话。

您可以使用transition: width ease 1s (并根据需要使用:hover psuedo元素)

见下面的演示:

 $(document).ready(function() { $("input").click(function() { $(this).css("width", "500px") }) }) 
 input[type="text"] { background-color: #000; color: #fff; border-radius: 10px; border-style: none; height: 50px; width: 250px; transition: width ease 1s; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text"> 

使用纯CSS,您可以使用focus psuedo元素,如下所示:

 input[type="text"] { background-color: #000; color: #fff; border-radius: 10px; border-style: none; height: 50px; width: 250px; transition: width ease 1s; } input[type="text"]:focus { width : 500px; } 
 <input type="text"> 

有一个CSS解决方案,使用transitiontoggling.on("click")小提琴

CSS

input[type="text"] {
  background-color: #000;
  color: #fff;
  border-radius: 10px;
  border-style: none;
  height: 50px;
  width: 250px;
  transition: width .3s linear;
  -webkit-transition: width .3s linear;
}
.expand {
  width:500px;
}

HTML

<input type="text" />

jQuery的

$("input[type='text']").on("click", function(e){
  $(this).toggleClass("expand");
}

您可以使用mouseenter悬停

 //Mouseenter
 $(document).ready(function() {
  $("input").on("mouseenter",function() {
    var i;
    for (i = 250; i < 501; i++) {
      $(this).css("width", (i.toString + "px"))
    }
  });
 });


 //Hover
 $(document).ready(function() {
      $("input").hover(function() {
        var i;
        for (i = 250; i < 501; i++) {
          $(this).css("width", (i.toString + "px"))
        }
    });
  });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM