效果: 有一个长方形border框,用css3实现鼠标移动到上面时两边同时往中间收缩成圆形。主要问题是不懂怎么实现两边同时往中间缩?请大神们指教!!
这样么?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
div {
width: 100px;
height: 50px;
border: 1px solid #000000;
position: absolute;
top: 0;
left: 0;
right: 0;
margin: 0 auto;
}
div:hover {
transition: 1s;
width: 50px;
border-radius: 50%;
}
</style>
</head>
<body>
<div></div>
</body>
</html>
// html
<div class="wrapper">
<div class="box3"></div>
</div>
// css
.wrapper{
width: 200px;
height: 50px;
}
.box3{
width: 200px;
height: 50px;
background: green;
transition: 1s;
margin:0 auto;
border-radius: 30px;
}
.box3:hover{
border-radius: 50%;
width: 50px;
height: 50px;
}
刚动手写一下:
.borderRadiusAnimation {
width: 400px;
height: 200px;
border: 1px solid #000;
transition: all 2s;
margin: 0 auto;
outline: none;
}
.borderRadiusAnimation:hover {
width: 200px;
border-radius: 50%;
}
<div class="borderRadiusAnimation">
</div>
居中的话, 看起来就是同时向中间收缩了。 居中我这里用的margin:0 auto
需求:让长方形变成圆,且应有过渡效果
1:先画个长方形
2:再在hover的时候将长方形的宽高设成一致,再画成圆
3:加上过渡效果(transition)
html
<div class="tra"></div>
css
.tra {
width: 90px;
height: 50px;
border: 1px solid #368;
transition: all 1s;
}
.tra:hover {
width: 50px;
height: 50px;
border-radius: 50%;
margin-left: 18px;
transition: all 1s;
}
<div id="div1"></div>
#div1{
width: 200px;
height: 100px;
border: 1px solid #000;
position: absolute;
top: 50%;
left: 50%;
-webkit-transform: translate(-50%, -50%);
-moz-transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
-o-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
-webkit-transition: width .5s;
-moz-transition: width .5s;
-ms-transition: width .5s;
-o-transition: width .5s;
transition: width .5s;
}
#div1:hover{
width: 100px;
border-radius: 50%;
}
10 回答11.2k 阅读
5 回答4.8k 阅读✓ 已解决
4 回答3.1k 阅读✓ 已解决
2 回答2.7k 阅读✓ 已解决
3 回答4.9k 阅读✓ 已解决
1 回答3.2k 阅读✓ 已解决
3 回答2.3k 阅读✓ 已解决
你需要用position把它先定位一下,之后就能两边同时缩进了。具体代码(在你的代码基础上改的):