过渡结束后将焦点设置为textinput

时间:2018-08-29 07:45:01

标签: javascript html css css3 css-transitions

在过渡结束后,是否可以设置焦点-以便在文本输入字段中自动输入文字?然后,我希望用户键入键盘输入以自动进入textinput字段。如果可能,只能使用CSS吗?还是需要JavaScript(不需要jQuery!)? 像

document.getElementByClassName('text-input').focus();

不这样做。

<div class="wrapper">
  <div class="element"></div>
  <input class="text-input" type="text" name="search" />
</div>


.wrapper {
  height: 40px;
  width: 75px;
  border-style: solid;
  border-width: 2px;
  border-radius: 40px;
  border-color: red;
  display: flex;
  align-items: center;
  padding-left:30px;
  -webkit-transition: width 0.4s ease-in-out;
  box-sizing:border-box;
}

.element {
  background-color: hotpink;
  width: 10px;
  height: 10px;
}

.wrapper:hover {
  width: 100%;
}

.text-input {
  max-width: 0;
  float: left;
  padding: 0px;
  border: 0px transparent;
  -webkit-transition:max-width 0.6s ease-in-out;
  transition:max-width 0.6s ease-in-out;
}

.wrapper:hover .text-input {
  max-width: 50%;
  padding: 2px;
  border: 1px solid #d4d4d4;
}

这是一个Codepen

https://codepen.io/anon/pen/MqjmQz

3 个答案:

答案 0 :(得分:2)

我看到了其他解决方案,最好使用transitionend事件监听器。只需添加另一种方法即可。请看一看。

JS:

var txtInput = document.querySelector('.text-input');

txtInput.addEventListener("transitionend", function(e){
  document.getElementById('inputField').focus();
}, false)

https://codepen.io/anon/pen/MqjmQz

答案 1 :(得分:1)

您在这里。纯JS解决方案

document.getElementById("wrapper").onmouseover = function()
{
setTimeout(function(){mouseOver()},600)
};


function mouseOver(){
document.getElementById('inputField').focus();
}
.wrapper {
  height: 40px;
  width: 75px;
  border-style: solid;
  border-width: 2px;
  border-radius: 40px;
  border-color: red;
  display: flex;
  align-items: center;
  padding-left:30px;
  -webkit-transition: width 0.4s ease-in-out;
  box-sizing:border-box;
}

.element {
  background-color: hotpink;
  width: 10px;
  height: 10px;
}

.wrapper:hover {
  width: 100%;
}

.text-input {
  max-width: 0;
  float: left;
  padding: 0px;
  border: 0px transparent;
  -webkit-transition:max-width 0.6s ease-in-out;
  transition:max-width 0.6s ease-in-out;
}

.wrapper:hover .text-input {
  max-width: 50%;
  padding: 2px;
  border: 1px solid #d4d4d4;
}
<div class="wrapper" id="wrapper">
  <div class="element"></div>
  <input class="text-input" id="inputField" type="text" name="search" />
</div>

修改
您还可以调用mouseout函数,以便每当光标移出时,场就会变得模糊。

document.getElementById("wrapper").onmouseover = function()
{
setTimeout(function(){mouseOver()},600)
};
document.getElementById("wrapper").onmouseleave = function()
{
mouseOut()
};


function mouseOver(){
document.getElementById('inputField').focus();
}
function mouseOut(){
document.getElementById('inputField').blur();
}
.wrapper {
  height: 40px;
  width: 75px;
  border-style: solid;
  border-width: 2px;
  border-radius: 40px;
  border-color: red;
  display: flex;
  align-items: center;
  padding-left:30px;
  -webkit-transition: width 0.4s ease-in-out;
  box-sizing:border-box;
}

.element {
  background-color: hotpink;
  width: 10px;
  height: 10px;
}

.wrapper:hover {
  width: 100%;
}

.text-input {
  max-width: 0;
  float: left;
  padding: 0px;
  border: 0px transparent;
  -webkit-transition:max-width 0.6s ease-in-out;
  transition:max-width 0.6s ease-in-out;
}

.wrapper:hover .text-input {
  max-width: 50%;
  padding: 2px;
  border: 1px solid #d4d4d4;
}
<div class="wrapper" id="wrapper">
  <div class="element"></div>
  <input class="text-input" id="inputField" type="text" name="search" />
</div>

答案 2 :(得分:-3)

我不担心这种功能,但是您知道过渡需要多长时间(0.6s),因此您可以使用setTimeout

var wrapper = document.querySelector('.wrapper');
wrapper.addEventListener('mouseover', function() {
  setTimeout(function() {
    wrapper.classList.add('focus');
    document.querySelector('.text-input').focus();
  }, 600)
});

然后,您需要对CSS进行一些更改,以防止鼠标伸出时缩小。

.wrapper {
  height: 40px;
  width: 75px;
  border-style: solid;
  border-width: 2px;
  border-radius: 40px;
  border-color: red;
  display: flex;
  align-items: center;
  padding-left:30px;
  -webkit-transition: width 0.4s ease-in-out;
  box-sizing:border-box;
}

.element {
  background-color: hotpink;
  width: 10px;
  height: 10px;
}

.wrapper:hover,
.wrapper.focus {
  width: 100%;
}

.text-input {
  max-width: 0;
  float: left;
  padding: 0px;
  border: 0px transparent;
  -webkit-transition:max-width 0.6s ease-in-out;
  transition:max-width 0.6s ease-in-out;
}

.wrapper:hover .text-input,
.wrapper.focus .text-input{
  max-width: 50%;
  padding: 2px;
  border: 1px solid #d4d4d4;
}

https://codepen.io/lucafbb/pen/Eegmrx?editors=1111