在没有悬停的情况下,CSS按钮可以平滑地变为正常大小

时间:2017-01-05 10:43:02

标签: html css

我是css的新手,我不知道当用户取消按钮时如何让按钮变得更小,我已经成功地使按钮变得更平滑但是它在瞬间变成正常大小,这是我的代码:

<html>

<head>

<style>
body {
background-image:url("http://cdn.mysitemyway.com/etc-mysitemyway/webtreats/assets/posts/976/full/tileable-light-blurs-and-abstract-circles-18.png");
}
.title {
position:relative;
text-align:center;
border : 5px solid rgba(255,255,255,0.9);
background-color:rgba(0, 199, 255, 0.7);
font-size:100%;
color:white;
box-shadow: 10px 10px 5px #888888;
}
.firstcontainer {
position:relative;
margin-top:3%;
}
.menubutton {
box-shadow: 10px 10px 5px #888888;
font-size:200%;
margin-left:1%;
text-align:center;
background-color: rgba(0,157,255,0.5);
display:inline-block;
color:white;
border:3px solid white;
padding:1% 1% 1% 1%;
}
.menubutton:hover {
background-color:rgba(0,199,255,0.5);
padding: 1.1% 1.1% 1.1% 1.1%;
transition-duration:0.5s;
}
.menutext {
color:white;
}
</style>

</head>

<body>

<div class="title"> <h1 > English Lesson </h1> </div>
<div class="firstcontainer">

<div class="menubar"></div>

<a href="#" class="menutext"><div class="menubutton"><strong><i><u>Home</u></i></strong></div></a>
<a href="#" clas="menutext"><div class="menubutton"><strong><i><u>Contacts</u></i></strong></div></a>


</div>

</body>

</html>
   

我很抱歉,如果已经问到某个地方,但我无法在google等搜索引擎上找到它。

有没有办法制作像.menubutton:unhover这样的东西?

6 个答案:

答案 0 :(得分:2)

也将转换添加到menubutton,转换属性将应用于悬停状态,因此在更改为特定状态时,动画需要0.5秒。将其添加到常规状态也将在从特殊状态更改时应用它

body {
	background-image: url("http://cdn.mysitemyway.com/etc-mysitemyway/webtreats/assets/posts/976/full/tileable-light-blurs-and-abstract-circles-18.png");
}
.title {
	position: relative;
	text-align: center;
	border : 5px solid rgba(255,255,255,0.9);
	background-color: rgba(0, 199, 255, 0.7);
	font-size: 100%;
	color: white;
	box-shadow: 10px 10px 5px #888888;
}
.firstcontainer {
	position: relative;
	margin-top: 3%;
}
.menubutton {
	box-shadow: 10px 10px 5px #888888;
	font-size: 200%;
	margin-left: 1%;
	text-align: center;
	background-color: rgba(0,157,255,0.5);
	display: inline-block;
	color: white;
	border: 3px solid white;
	padding: 1% 1% 1% 1%;
	transition-duration: 0.5s;
	-webkit-transition-duration: 0.5s;
	-ms-transition-duration: 0.5s;
	-moz-transition-duration: 0.5s;
}
.menubutton:hover {
	background-color: rgba(0,199,255,0.5);
	padding: 1.1% 1.1% 1.1% 1.1%;
	transition-duration: 0.5s;
	-webkit-transition-duration: 0.5s;
	-ms-transition-duration: 0.5s;
	-moz-transition-duration: 0.5s;
}
.menutext {
	color: white;
}
<div class="title">
  <h1 > English Lesson </h1>
</div>
<div class="firstcontainer">
  <div class="menubar"></div>
  <a href="#" class="menutext">
  <div class="menubutton"><strong><i><u>Home</u></i></strong></div>
  </a> <a href="#" clas="menutext">
  <div class="menubutton"><strong><i><u>Contacts</u></i></strong></div>
  </a> 
</div>

答案 1 :(得分:1)

您的transition媒体资源需要在默认的.menubutton课程中。这意味着当您将鼠标悬停在按钮上时,它将转换为新样式,然后在您停止悬停时再次返回默认值。

.menubutton {
  box-shadow: 10px 10px 5px #888888;
  font-size:200%;
  margin-left:1%;
  text-align:center;
  background-color: rgba(0,157,255,0.5);
  display:inline-block;
  color:white;
  border:3px solid white;
  padding:1% 1% 1% 1%;
  transition-duration:0.5s;
}
.menubutton:hover {
  background-color:rgba(0,199,255,0.5);
  padding: 1.1% 1.1% 1.1% 1.1%;
}

当您停止悬停时动画不起作用的原因是transition属性不再应用于该元素,因为它仅在悬停时存在。

答案 2 :(得分:0)

尝试使用 - 在可能的情况下 - transform属性。它非常流畅地处理动画。

在你的情况下,我建议你使用transform: scale()代替,动画效果很好,看起来比以前更好。

顺便说一下,如果你想申请一个值,例如5px填充或边距,您不必为所有方向单独编写,只需使用:

margin: 5pxpadding: 5px。它会同时影响顶部,底部,左侧和右侧方向。

body {
  background-image: url("http://cdn.mysitemyway.com/etc-mysitemyway/webtreats/assets/posts/976/full/tileable-light-blurs-and-abstract-circles-18.png");
}
.title {
  position: relative;
  text-align: center;
  border: 5px solid rgba(255, 255, 255, 0.9);
  background-color: rgba(0, 199, 255, 0.7);
  font-size: 100%;
  color: white;
  box-shadow: 10px 10px 5px #888888;
}
.firstcontainer {
  position: relative;
  margin-top: 3%;
}
.menubutton {
  box-shadow: 5px 5px 2px #888888;
  font-size: 200%;
  text-align: center;
  background-color: rgba(0, 157, 255, 0.5);
  display: inline-block;
  color: white;
  border: 3px solid white;
  padding: 4px;
  margin: 4px;
  transition: all .5s ease;
}
.menubutton:hover {
  background-color: rgba(0, 199, 255, 0.5);
  transform: scale(1.1);
  transition: all .5s ease;
}
.menutext {
  color: white;
}
<div class="title">
  <h1> English Lesson </h1> 
</div>
<div class="firstcontainer">

  <div class="menubar"></div>

  <a href="#" class="menutext">
    <div class="menubutton"><strong><i><u>Home</u></i></strong>
    </div>
  </a>
  <a href="#" clas="menutext">
    <div class="menubutton"><strong><i><u>Contacts</u></i></strong>
    </div>
  </a>


</div>

答案 3 :(得分:0)

添加到课程

.menubutton {
box-shadow: 10px 10px 5px #888888;
font-size:200%;
margin-left:1%;
text-align:center;
background-color: rgba(0,157,255,0.5);
display:inline-block;
color:white;
border:3px solid white;
padding:1% 1% 1% 1%;
transition:all 1s ease-in-out;
-webkit-transition:all 1s ease-in-out;
-moz-transition:all 1s ease-in-out;
-ms-transition:all 1s ease-in-out;
}

答案 4 :(得分:0)

您需要使用scale()效果并对未覆盖和悬停按钮应用transition-duraion

看一下片段:

body {
background-image:url("http://cdn.mysitemyway.com/etc-mysitemyway/webtreats/assets/posts/976/full/tileable-light-blurs-and-abstract-circles-18.png");
}
.title {
position:relative;
text-align:center;
border : 5px solid rgba(255,255,255,0.9);
background-color:rgba(0, 199, 255, 0.7);
font-size:100%;
color:white;
box-shadow: 10px 10px 5px #888888;
}
.firstcontainer {
position:relative;
margin-top:3%;
}
.menubutton {
box-shadow: 10px 10px 5px #888888;
font-size:200%;
margin-left:1%;
text-align:center;
background-color: rgba(0,157,255,0.5);
display:inline-block;
color:white;
border:3px solid white;
padding:1% 1% 1% 1%;
transition-duration: 0.5s;
}
.menubutton:hover {
transform: scale(1.2);
transition-duration: 0.5s;
}
.menutext {
color:white;
}
<div class="title"> <h1 > English Lesson </h1> </div>
<div class="firstcontainer">

<div class="menubar"></div>

<a href="#" class="menutext"><div class="menubutton"><strong><i><u>Home</u></i></strong></div></a>
<a href="#" clas="menutext"><div class="menubutton"><strong><i><u>Contacts</u></i></strong></div></a>


</div>

答案 5 :(得分:0)

<html>

<head>

<style>
body {
background-image:url("http://cdn.mysitemyway.com/etc-mysitemyway/webtreats/assets/posts/976/full/tileable-light-blurs-and-abstract-circles-18.png");
}
.title {
position:relative;
text-align:center;
border : 5px solid rgba(255,255,255,0.9);
background-color:rgba(0, 199, 255, 0.7);
font-size:100%;
color:white;
box-shadow: 10px 10px 5px #888888;
}
.firstcontainer {
position:relative;
margin-top:3%;
}
.menubutton {
box-shadow: 10px 10px 5px #888888;
font-size:200%;
margin-left:1%;
text-align:center;
background-color: rgba(0,157,255,0.5);
display:inline-block;
color:white;
border:3px solid white;
padding:1% 1% 1% 1%;
transition: padding 0.1s;
}
.menubutton:hover {
background-color:rgba(0,199,255,0.5);
padding: 1.1% 1.1% 1.1% 1.1%;
}
.menutext {
color:white;
}
</style>

</head>

<body>

<div class="title"> <h1 > English Lesson </h1> </div>
<div class="firstcontainer">

<div class="menubar"></div>

<a href="#" class="menutext"><div class="menubutton"><strong><i><u>Home</u></i></strong></div></a>
<a href="#" clas="menutext"><div class="menubutton"><strong><i><u>Contacts</u></i></strong></div></a>


</div>

</body>

</html>

只需从悬停元素中删除 transition-duration ,然后在按钮类中添加过渡:padding 0.1s 。由于填充变化较小,动画看起来并不那么平滑。