如何通过CSS过渡从中心向外制作背景比例

时间:2016-11-30 01:44:47

标签: css css3 css-transitions

当您将鼠标悬停在具有材质用户界面的日期选择器here的日期上时(我点击任何文本输入以触发选择器,然后将鼠标悬停一天),我将尝试复制效果,其中背景向外扩展中心。

我试图从这里复制CSS,但我只是设法让相反的工作。请参阅:https://jsfiddle.net/2zkofa0x/3/

我的CSS:

span:hover {
  color: #fff;
  background: rgba(0, 151, 167, 0.5);
  border-radius: 50%;
  transform: scale(1);
  transition: all 450ms cubic-bezier(0.23, 1, 0.32, 1) 0ms;
}

有人知道如何将彩色背景展开并从元素的中心填充(与我上面的相反)?

4 个答案:

答案 0 :(得分:3)

你可以这样试试。它将悬停效果放在父div上,因此命中目标始终存在。

此外,圆圈需要以0的比例开始,以便在过渡期间扩展到完整尺寸。

HTML:

<div class='container'>
  <div class='circle'>
  </div>
  <span>42</span>
</div>

CSS:

div.container {
  position: relative;
  height: 30px;
  width: 30px;
}

div.container > div.circle {    
  position: absolute;
  top: 0;
  left: 0;
  border-radius: 50%;
  width: 30px;
  height: 30px;
  transform: scale(0);
  transition: all 450ms ease 0ms;
}

div.container:hover > div.circle {
  background: rgba(0, 151, 167, .5);
  border-radius: 50%;
  transform: scale(1);
  transition: all 450ms cubic-bezier(0.23, 1, 0.32, 1) 0ms;
}

div.container span {    
  position: relative;
  padding: 7px;
  line-height: 30px;
}

div.container:hover span {
  color: rgb(255, 255, 255);
}

https://jsfiddle.net/2zkofa0x/18/

答案 1 :(得分:2)

使用box-shadow

li { position: relative; display: inline-block; padding: 10px; }
li:before {
  content: '';
  height: 1px;
  width: 1px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  box-shadow: 0 0 0 0px #18b;
  border-radius: 50%;
  background: #18b;
  transition: all .3s;
  opacity: 0;
  z-index: -1;
}
li:hover:before {  
  box-shadow: 0 0 0 15px #18b;
  opacity: 1;
}
<ul>
	<li>1</li>
	<li>2</li>
</ul>

答案 2 :(得分:0)

你可以尝试这样的事情。您也可以使用jquery来获得相同的效果。

&#13;
&#13;
/* Material style */
button {
  height: 200px;
  width: 200px;
  border: none;
  cursor: pointer;
  color: white;
  padding: 15px;
  border-radius: 360px;
  font-size: 22px;
  box-shadow: 2px 2px 4px rgba(0, 0, 0, .4);
  background: #2196F3;
}

/* Ripple magic */
button{
  position: relative;
  overflow: hidden;
}

button:after {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  width: 5px;
  height: 5px;
  background: rgba(255, 255, 255, .5);
  opacity: 0;
  border-radius: 50%;
  transform: scale(1, 1) translate(-50%);
  transform-origin: 50% 50%;
}

@keyframes ripple {
  0% {
    transform: scale(0, 0);
    opacity: 1;
  }
  20% {
    transform: scale(25, 25);
    opacity: 1;
  }
  100% {
    opacity: 0;
    transform: scale(40, 40);
  }
}

button:focus:not(:active)::after {
  animation: ripple 1s ease-out;
}


/* On Hover */
.ripple-button1{
	position:relative;
	width:200px;
	height:200px;
	background-color:#99C;
	color:#FFF;
	border-radius:360px;
	text-decoration:none;
	text-align:center;
	vertical-align:middle;
	display:table-cell;
    box-shadow: 2px 2px 4px rgba(0, 0, 0, .4);
}

.wave1{
	position:absolute;
	width:200px;
	height:200px;
	background-color:#FFF;
	top:0;
	left:0;
	transform: scale(0);
	opacity:0.5;
	border-radius:300px;
}

.ripple-button1:hover > .wave1{
	animation: ripple-in1 2s;
}
 
@keyframes ripple-in1 {
 0% {transform: scale(0);}
 20%{transform: scale(1);opacity:0.3;}
 100%{transform: scale(1);opacity:0;}
}
&#13;
<h3>Ripple on Click</h3>
<button>Click !</button>
<h3>Ripple on Hover</h3>
<a href="#" class="ripple-button1"><div class="wave1"></div>Hover !</a>
&#13;
&#13;
&#13;

答案 3 :(得分:0)

我一直在寻找同样的东西。谢谢你的加薪。虽然您已经选择了答案,但我会分享我的hack,它看起来与引用页面上的类似。

&#13;
&#13;
div {
  margin: 25px;
  height: 100px;
  width: 100px;
  text-align: center; //Fix the element to center
}

span {
  width: 50px;
  height: 50px;
  padding: 5px;
  border-radius: 50%;
}

span:hover {
  color: #fff;
  background: rgba(0, 151, 167, 0.5);
  padding: 15px; //Transform padding, width and height instead of border-radius
  width: 20px;
  height: 20px;
  transform: scale(1);
  transition: all 450ms cubic-bezier(0.23, 1, 0.32, 1) 1ms;
}
&#13;
<div>
  <span>42</span>
</div>
&#13;
&#13;
&#13;