用 jQuery 扩展 onClick 的输入框
Input boxes that expand onClick with jQuery
我的页面上有这个 input[type="text"]
,我已经为此工作了一段时间。我想做的是在用户点击它时创建它扩展的效果。另外,我希望它在鼠标离开时 return 达到正常宽度。
到目前为止我的代码:
$(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()
,如果它存在的话。
有 CSS 解决方法,使用 transition
和 toggling
class .on("click")
: fiddle
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 or hover
//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"))
}
});
});
您可以使用 transition: width ease 1s
(如果需要,也可以使用 :hover
伪元素)
参见下面的演示:
$(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
伪元素:
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">
我的页面上有这个 input[type="text"]
,我已经为此工作了一段时间。我想做的是在用户点击它时创建它扩展的效果。另外,我希望它在鼠标离开时 return 达到正常宽度。
到目前为止我的代码:
$(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()
,如果它存在的话。
有 CSS 解决方法,使用 transition
和 toggling
class .on("click")
: fiddle
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 or hover
//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"))
}
});
});
您可以使用 transition: width ease 1s
(如果需要,也可以使用 :hover
伪元素)
参见下面的演示:
$(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
伪元素:
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">