html - 如何在保持中心锚定在一个点的同时放大 HTML 文本?

标签 html css

如何使网页上的文本具有动画效果并增加其大小,同时保持中心锚定在一个点上?这是我尝试过的:

http://jsfiddle.net/wnn2v023/1/

但可以看出, anchor 位于 (0, 0) 而不是文本中心。我尝试使用 text-align,但没有得到想要的结果。

HTML:

<button id="button">
  Begin
</button>
<div id="main">
<div id="div">
  Hello World!
</div>  
</div>

CSS:

#main
{
  position: relative; 
}

#div
{
  position:absolute;
  font-size:0em; 
}

@-webkit-keyframes my-animation {
  0%   { font-size: 0em; }
  100% { font-size: 5em; }
}

.effect {
  -webkit-animation: my-animation 1s; /* Safari 4+ */  
  animation: my-animation 1s;
  animation-fill-mode: forwards;  
}

JS:

var e = document.getElementById("button");
var div = document.getElementById("div");
e.addEventListener("click", function()
                  {  
  div.className = "effect";
});

请注意,我无法对 main div 进行任何更改。我还发现使用 font-size 来制作动画效果不是很流畅。

最佳答案

不是为 font-size 属性设置动画,而是为 transform: scale() 设置动画.

在文本上设置所需的 font-size 以及 transform: scale(0) 然后将其动画设置为默认比例 transform: scale( 1):

document.getElementById('button').addEventListener("click", function() {
  document.querySelector('.text').classList.toggle("effect");
});
.container {
  position: relative;
}

.text {
  position: absolute;
  transform: scale(0);
  font-size: 5em;
}


@-webkit-keyframes grow {
  100% { transform: scale(1); }
}

@keyframes grow {
  100% { transform: scale(1); }
}

.effect {
  -webkit-animation: grow 1s forwards;
  animation: grow 1s forwards;
}
<button id="button">Begin</button>

<div class="container">
  <span class="text">Hello World!</span>
</div>

关于html - 如何在保持中心锚定在一个点的同时放大 HTML 文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34113381/

相关文章:

html - Bootstrap 3.0 模态分隔宽度布局

html - 我们怎样才能使固定左侧导航栏旁边的容器响应?

css - 顶部边框出现在 css 的左边框上方

javascript - iOS Safari : Prevent (or Control) Scroll on Input Focus

html - 使 div 可扩展而不指定任何高度

jquery 设置 html 效果

html - 调整大小后 Safari 13 不应用 CSS

css - 在页脚上方和下方添加空间

html - 背景图像未以完整 Div 显示

html - 悬停时如何使用标题标签而不弹出标题?