热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

在打字时如何将占位符移动到焦点顶部?-HowtomoveplaceholdertotoponfocusANDwhiletyping?

Iwanttheplaceholdertomovetothetopwhenthetextboxisonfocusandalsowhiletheuseristy

I want the placeholder to move to the top when the textbox is on focus and also while the user is typing.

我希望占位符在文本框处于焦点时以及用户键入时移动到顶部。

I'm not sure if this is just html/css or any Javascript too.

我不确定这只是html / css还是任何Javascript。

My current css looks like this, and I have no js code yet:

我当前的CSS看起来像这样,我还没有js代码:

input:focus::-webkit-input-placeholder {
    font-size: .75em;
    position: relative;
    top: -15px; 
    transition: 0.2s ease-out;
}

input::-webkit-input-placeholder {
    transition: 0.2s ease-in;
}

input[type="text"]:focus, input[type="password"]:focus {
    height: 50px;
    padding-bottom: 0px;
    transition: 0.2s ease-in;
}

input[type="text"], input[type="password"] {
    height: 50px;
    transition: 0.2s ease-in;
}

It almost does the work (isn't as perfect as the link), but the placeholder disappears when I start typing. I'm using twitter-bootstrap, if that makes anything easier!

它几乎完成了工作(不如链接那么完美),但是当我开始输入时占位符消失了。我正在使用twitter-bootstrap,如果这更容易!

Thanks.

谢谢。

4 个解决方案

#1


42  

You could do it like this

你可以这样做

HTML:

HTML:

Your email address

CSS:

CSS:

input:focus ~ .floating-label,
input:not(:focus):valid ~ .floating-label{
  top: 8px;
  bottom: 10px;
  left: 20px;
  font-size: 11px;
  opacity: 1;
}

.inputText {
  font-size: 14px;
  width: 200px;
  height: 35px;
}

.floating-label {
  position: absolute;
  pointer-events: none;
  left: 20px;
  top: 18px;
  transition: 0.2s ease all;
}

Working JSFiddle here https://jsfiddle.net/273ntk5s/2/

在这里工作JSFiddle https://jsfiddle.net/273ntk5s/2/

#2


3  

.user-input-wrp {
	position: relative;
	width: 50%;
}
.user-input-wrp .inputText{
	width: 100%;
	outline: none;
	border:none;
	border-bottom: 1px solid #777;
}
.user-input-wrp .inputText:invalid {
	box-shadow: none !important;
}
.user-input-wrp .inputText:focus{
	border-color: blue;
	border-width: medium medium 2px;
}
.user-input-wrp .floating-label {
	position: absolute;
	pointer-events: none;
	top: 18px;
	left: 10px;
	transition: 0.2s ease all;
}
.user-input-wrp input:focus ~ .floating-label,
.user-input-wrp input:not(:focus):valid ~ .floating-label{
	top: 0px;
	left: 10px;
	font-size: 13px;
	opacity: 1;
}

The floating label


Your email address

Modified the code from @user1846747 a little.

稍微修改了@ user1846747的代码。

#3


2  

That site isn't moving the placeholder, but placing a div (.floating-label) over the input, so when the input is focused the div just animates to be over the input. The key part here is using pointer-events: none; in the floating div, so when you click it the event goes through it to the input box behind it.

该站点没有移动占位符,而是在输入上放置div(.floating-label),因此当输入被聚焦时,div只是动画来完成输入。这里的关键部分是使用指针事件:无;在浮动div中,所以当你点击它时,事件会通过它进入它后面的输入框。

#4


1  

span{
  display:block;
  }
input:focus::-webkit-input-placeholder { color:transparent; }
input:focus:-moz-placeholder { color:transparent; } /* FF 4-18 */
input:focus::-moz-placeholder { color:transparent; } /* FF 19+ */
input:focus:-ms-input-placeholder { color:transparent; } /* IE 10+ */



推荐阅读
author-avatar
美女牙医--妙菡众
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有