html - 从中心展开绝对定位的圆

标签 html css css-transitions

我有一个绝对定位的 div,它代表一个通过更圆的边界的圆。当 div 发生悬停事件时,我需要扩展这个圆圈,问题是圆圈向右/底部方向扩展,我需要它从中心扩展。这种偏离当然发生是因为顶部/左侧参数具有固定值,我设法使用负顶部/左侧边距克服了这个问题。最后一个问题是我想添加从正常状态到扩展状态的平滑过渡,这会产生不好的结果。 HTML代码:

<div class="circle"></div>

CSS 代码:

.circle {

  position: absolute;
  left: 150px;
  top: 150px;
  border: 5px solid #000;
  -webkit-border-radius: 50%;
  -moz-border-radius: 50%;
  border-radius: 50%;
  width: 40px;
  height: 40px;
  margin-left: -20px;
  margin-top: -20px;
  -webkit-transition: width 0.5s, height 0.5s;
  -moz-transition: width 0.5s, height 0.5s;
  -o-transition: width 0.5s, height 0.5s;
  transition: width 0.5s, height 0.5s;
}
.circle:hover
{
  width: 60px;
  height: 60px;
   margin-left: -30px;
  margin-top: -30px;
}

工作示例:https://jsfiddle.net/ArturoO/4qLr0tc2/5/

如果您删除过渡规则,那么它会起作用,但我需要动画出现。有什么想法吗?

最佳答案

使用平移变换代替边距

.circle {
  position: absolute;
  left: 150px;
  top: 150px;
  border: 5px solid #000;
  border-radius: 50%;
  width: 40px;
  height: 40px;
  transform: translate(-50%, -50%);
  transition: width 0.5s, height 0.5s;
}
.circle:hover {
  width: 60px;
  height: 60px;
}
<div class="circle"></div>

关于html - 从中心展开绝对定位的圆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38484438/

相关文章:

html - 去除左右两侧的白色间隙

css - CSS 显示属性的过渡

css - 导航悬停时的动画

html - 使用 CSS3 动画加速闪烁计时器

python - 属性错误 : 'InstrumentedList' object has no attribute 'get'

css - 使用 css 选择器更改表格行颜色

JavaScript 运行完整的 A href 命令

html - 什么是下载 HTML 页面的好网络爬虫?

javascript - 如何使用 jquery 和 css 创建粘性 header

javascript - div 元素高度自动设置为零