CSS 揭秘(8)--弹性过渡

Posted by liveipool on February 9, 2017

弹性过渡

假设有一个文本输入框,每当它被聚焦时,都需要展示一个提示框。这个提示框可用来向用户提供帮助信息等,基本代码结构如下:

label Your username:
	input#usernmae
	span.callout Only letters, numbers, underscores(_) and hyphens(-) allowed!

然后我们向其中添加动画,看懂代码:

input:not(:focus) + .callout {
	transform: scale(0);
	transition: .25s transform;  /*如果不指定这个transform,.25s会应用到所有可以参与过渡的属性。*/
}

.callout{
	transform-origin: 1.4em -.4em;
	transition: .5s cubic-bezier(.25,.1,.3,1.5) transform;
}

/* Styling */
body {
	padding: 1.5em !important;
	font: 200%/1.6 Baskerville;
}

input {
	display: block;
	padding: 0 .4em;
	font: inherit;
}

.callout {	
	position: absolute;
	max-width: 14em;
	padding: .6em .8em;
	border-radius: .3em;
	margin: .3em 0 0 -.2em;
	background: #fed;
	border: 1px solid rgba(0,0,0,.3);
	box-shadow: .05em .2em .6em rgba(0,0,0,.2);
	font-size: 75%;
}

.callout::before {
	content: '';
	position: absolute;
	border: inherit;
	background: inherit;
	padding: .35em;
	top: -.4em;
	left: 1em;
	border-left: 0;
	border-bottom: 0;
	transform: rotate(-45deg);
}

42.4.png