html - 将鼠标悬停在文本上以使用 CSS 更改大小

标签 html css

我试图在将鼠标悬停在 Internet 一词上时更改文本的大小,而不更改其任何其他属性。当结束单词时,我试图减小单词的大小

这是我尝试实现此功能的代码

h1:hover::before,
h1:hover::after {
  width: 100%;
}

h1::before {
  top: 0;
  left: 0;
}

h1::after {
  top: 100%;
  right: 0;

}
#text:hover{
color:white;

-webkit-转换:规模(1.2);

<body>
  <div class="text-wrapper">


    <h1 id='text'>
      Hot</h1>


  </div>
</body>

最佳答案

像建议的评论一样,您需要向 h1 css 添加过渡:

h1 {
    text-align: center;
    color: #6849e3;
    font-family: Merriweather;
    display: inline-block;
    position: relative;
    // Set transition...
    transition: transform .7s ease;
}

并设置低尺度变换:

#text:hover {
    color: white;
    // Scale transform...
    -webkit-transform: scale(0.5);
    transform: scale(0.5);
}

调整比例设置和过渡持续时间以分别调整文本大小和过渡速度。

编辑:

如果我没有正确理解您的评论,您希望悬停线为其宽度设置动画,然后“消失”——为此我们可以将动画从全宽反转为无。为此,您需要使用 animation 属性以及 @keyframes

根据您的评论,我已将行反过来,因此顶行从右到左,底行从左到右,方法是将 h1::before left 属性更改为right:0; h1::after 反之亦然。

然后需要在悬停规则上指定动画:

h1:hover::before,
h1:hover::after {
   animation-name: slide;
   animation-duration: .5s;
   animation-iteration-count: 2;
   animation-direction: alternate;
}

有关动画 属性的更多信息:w3schoolsMozilla docs .

并通过@keyframes指定动画本身:

@keyframes slide {
  from {width:0%}
  to {width:100%}
}

关于@keyframes的信息:Mozilla docs

body {
  Background: #7a86cb;
  text-align: center;
}

h1 {
  text-align: center;
  color: #6849e3;
  font-family: Merriweather;
  display: inline-block;
  position: relative;
  transition: transform .7s ease;
}


h1::before,
h1::after {
  content: "";
  position: absolute;
  height: 2px;
  background: #6849e3;
}

h1:hover::before,
h1:hover::after {
 animation-name: slide;
 animation-duration: .5s;
 animation-iteration-count: 2;
 animation-direction: alternate;
}

h1::before {
  top: 0;
  right: 0;
}

h1::after {
  top: 100%;
  left: 0;
 
}
#text:hover{
  color:white;
  -webkit-transform: scale(0.5);
  transform: scale(0.5);
}
@keyframes slide {
  from {width:0%}
  to {width:100%}
}
<body>
  <div class="text-wrapper">


    <h1 id='text'>
      INTERNET </h1>


  </div>
</body>

关于html - 将鼠标悬停在文本上以使用 CSS 更改大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56693904/

相关文章:

html - 设置当前页面的文本

css - Bootstrap 列未内联显示

PHP 示例 AJAX 实时搜索

html - 使上传文件在 div 上不可见

javascript - 触发焦点操作,但仅当不是由于单击而触发时

html - 如何在页面顶部创建一个倾斜的 div 标题

html - 向下滚动时使侧边 block 保持在同一水平

javascript - 为什么保存输入值的变量总是记录为空?

css - scss按不同条件导入变量

javascript - 仅当不靠近 messages div 的底部时,保持滚动位置才有效