HTML常用动画

本文介绍了如何使用CSS为HTML元素添加鼠标悬停时的多种动画效果,包括文字加载动画、缩放、旋转、边框变换、透明度改变以及布局技巧如底部通栏和清除浮动。
摘要由CSDN通过智能技术生成

鼠标移入文字加载动画效果

HTML

<!DOCTYPE html>

<html>

<head>

  <link rel="stylesheet" type="text/css" href="style.css">

</head>

<body>

  <h1 class="animated-text">Hello, World!</h1>

</body>

</html>

CSS:

.animated-text {

  display: inline-block;

  font-size: 30px;

  transition: transform 0.5s ease; /* 设置动画过渡时间和缓动效果 */

}

.animated-text:hover {

  transform: rotateY(180deg); /* 鼠标移入时应用动画效果 */

}

鼠标悬停缩放效果

HTML

<!DOCTYPE html>

<html>

<head>

  <link rel="stylesheet" type="text/css" href="style.css">

</head>

<body>

  <h1 class="zoom-text">Hover over me</h1>

</body>

</html>

CSS:

.zoom-text {

  font-size: 24px; /* 设置初始字体大小 */

  transition: font-size 0.3s ease;  /* 设置动画过渡时间和缓动效果 */

}

.zoom-text:hover {

  font-size: 30px; /* 鼠标悬停时应用的字体大小 */

}

鼠标移入旋转动画

HTML

<!DOCTYPE html>

<html>

<head>

  <link rel="stylesheet" type="text/css" href="style.css">

</head>

<body>

  <div class="rotate-box">

    <img src="your-image.jpg" alt="Your Image">

  </div>

</body>

</html>

CSS:

.rotate-box {

  display: inline-block;

  transition: transform 0.5s ease; /* 设置动画过渡时间和缓动效果 */

}

.rotate-box:hover {

  transform: rotate(360deg); /* 鼠标移入时应用的旋转角度 */

}

loding加载动画

HTML

<!DOCTYPE html>

<html>

<head>

  <link rel="stylesheet" type="text/css" href="style.css">

</head>

<body>

  <div class="loader"></div>

</body>

</html>

CSS:

.loader {

  border: 10px solid #f3f3f3; /* 设置加载动画的边框样式 */

  border-top: 10px solid #3498db; /* 设置加载动画边框的颜色 */

  border-radius: 50%; /* 设置加载动画为圆形 */

  width: 60px; /* 设置加载动画的宽度 */

  height: 60px; /* 设置加载动画的高度 */

  animation: spin 2s linear infinite; /* 设置旋转动画的属性和持续时间 */

  /* 中心对齐加载动画 */

  position: absolute;

  top: 50%;

  left: 50%;

  transform: translate(-50%, -50%);

}

@keyframes spin {

  0% {

    transform: rotate(0deg); /* 起始角度 */

  }

  100% {

    transform: rotate(360deg); /* 终止角度 */

  }

}

当鼠标移上第一张图像时,产生直角边框过渡为圆角边框的效果

HTML

<!DOCTYPE html>

<html>

<head>

  <link rel="stylesheet" type="text/css" href="style.css">

</head>

<body>

  <div class="image-container">

    <img src="image-1.jpg" alt="Image 1" class="image">

  </div>

</body>

</html>

CSS:

.image-container {

  display: inline-block;

  overflow: hidden;

  position: relative;

}

.image {

  width: 300px;

  height: 200px;

  transition: border-radius 0.5s ease; /* 设置边框过渡效果 */

}

.image-container:hover .image {

  border-radius: 50%; /* 鼠标悬停时边框过渡为圆角边框 */

  cursor: pointer;

}

当鼠标移上第二张图像时,产生图片逐渐放大的过渡效果

HTML

<!DOCTYPE html>

<html>

<head>

  <link rel="stylesheet" type="text/css" href="style.css">

</head>

<body>

  <div class="image-container">

    <img src="image-2.jpg" alt="Image 2" class="image">

  </div>

</body>

</html>

CSS:

.image-container {

  display: inline-block;

  overflow: hidden;

  position: relative;

}

.image {

  width: 300px;

  height: 200px;

  transition: transform 0.5s ease; /* 设置变换过渡效果 */

}

.image-container:hover .image {

  transform: scale(1.2); /* 鼠标悬停时图片逐渐放大 */

  cursor: pointer;

}

当鼠标移上第三张图像时,产生图片旋转的过渡效果

HTML

<!DOCTYPE html>

<html>

<head>

  <link rel="stylesheet" type="text/css" href="style.css">

</head>

<body>

  <div class="image-container">

    <img src="image-3.jpg" alt="Image 3" class="image">

  </div>

</body>

</html>

CSS:

.image-container {

  display: inline-block;

  overflow: hidden;

  position: relative;

}

.image {

  width: 300px;

  height: 200px;

  transition: transform 0.5s ease; /* 设置变换过渡效果 */

}

.image-container:hover .image {

  transform: rotate(360deg); /* 鼠标悬停时图片旋转 */

  cursor: pointer;

}

当鼠标移上第四张图像时,产生图片透明度逐渐变暗的过渡效果

HTML

<!DOCTYPE html>

<html>

<head>

  <link rel="stylesheet" type="text/css" href="style.css">

</head>

<body>

  <div class="image-container">

    <img src="image-4.jpg" alt="Image 4" class="image">

  </div>

</body>

</html>

CSS:

.image-container {

  display: inline-block;

  overflow: hidden;

  position: relative;

}

.image {

  width: 300px;

  height: 200px;

  transition: opacity 0.5s ease; /* 设置透明度过渡效果 */

}

.image-container:hover .image {

  opacity: 0.7; /* 鼠标悬停时图片透明度逐渐变暗 */

  cursor: pointer;

}

设置背景颜色通栏显示

HTML

<!DOCTYPE html>

<html>

<head>

  <link rel="stylesheet" type="text/css" href="style.css">

</head>

<body>

  <div class="background-color"></div>

  <div class="content">

    <!-- 这里是页面内容 -->

  </div>

</body>

</html>

CSS:

body {

  margin: 0;

  padding: 0;

}

.background-color {

  width: 100%;

  height: 100%;

  position: fixed;

  top: 0;

  left: 0;

  background-color: #f1f1f1; /* 设置背景颜色 */

  z-index: -1;

}

.content {

  /* 这里是设置页面内容的样式 */

}

清除浮动

HTML

clearfix类

<!DOCTYPE html>

<html>

<head>

  <link rel="stylesheet" type="text/css" href="style.css">

</head>

<body>

  <div class="clearfix">

    <div class="float-left">浮动元素 1</div>

    <div class="float-left">浮动元素 2</div>

    <!-- 其他内容 -->

  </div>

</body>

</html>

CSS:

.clearfix::after {

  content: "";

  display: table;

  clear: both;

}

.float-left {

  float: left;

}

HTML

after伪元素

<!DOCTYPE html>

<html>

<head>

  <link rel="stylesheet" type="text/css" href="style.css">

</head>

<body>

  <div class="float-container">

    <div class="float-left">浮动元素 1</div>

    <div class="float-left">浮动元素 2</div>

    <!-- 其他内容 -->

    <div class="clearfix"></div>

  </div>

</body>

</html>

CSS:

.float-container::after {

  content: "";

  display: table;

  clear: both;

}

.float-left {

  float: left;

}

设置banner图片溢出隐藏

HTML

<!DOCTYPE html>

<html>

<head>

  <link rel="stylesheet" type="text/css" href="style.css">

</head>

<body>

  <div class="banner-container">

    <img src="banner.jpg" alt="Banner Image" class="banner-image">

  </div>

</body>

</html>

CSS:

.banner-container {

  width: 100%;

  height: 200px; /* 假设高度为200px,请根据实际情况调整 */

  overflow: hidden; /* 设置溢出隐藏 */

}

.banner-image {

  width: 100%;

  height: auto;

  object-fit: cover;

}

通过相对定位设置图片在页面居中显示

HTML

<!DOCTYPE html>

<html lang="en">

<head>

    <link rel="stylesheet" type="text/css" href="style.css">

</head>

<body>

    <div class="container">

        <img src="your-image.jpg" alt="Your Image" class="centered-image">

    </div>

</body>

</html>

CSS:

.container {

  position: relative;

  width: 100%; /* 可根据实际需要调整 */

  height: 400px; /* 可根据实际需要调整 */

}

.centered-image {

  position: absolute;

  top: 50%;

  left: 50%;

  transform: translate(-50%, -50%);

  max-width: 100%; /* 图片最大宽度 */

  max-height: 100%; /* 图片最大高度 */

}

定义其内部的图片和文字左浮动

HTML

<!DOCTYPE html>

<html lang="en">

<head>

    <link rel="stylesheet" type="text/css" href="style.css">

</head>

<body>

    <div class="container">

        <img src="your-image.jpg" alt="Your Image" class="float-left">

        <p class="float-left">Your text goes here.</p>

        <div class="clearfix"></div>

    </div>

</body>

</html>

CSS:

.float-left {

  float: left;

}

.clearfix::after {

  content: "";

  display: table;

  clear: both;

}

定义鼠标悬浮时的背景样式

HTML

<!DOCTYPE html>

<html>

<head>

  <link rel="stylesheet" type="text/css" href="style.css">

</head>

<body>

  <div class="container">

    <p class="hover-background">鼠标悬浮时背景样式变化。</p>

  </div>

</body>

</html>

CSS:

.hover-background:hover {

  background-color: yellow;

}

设置绝对定位

HTML

<!DOCTYPE html>

<html lang="en">

<head>

    <link rel="stylesheet" type="text/css" href="style.css">

</head>

<body>

    <div class="container">

        <div class="absolute-element">

            <!-- 内容 -->

        </div>

    </div>

</body>

</html>

CSS:

.container {

  position: relative;

}

.absolute-element {

  position: absolute;

  top: 50px; /* 设置上边距 */

  left: 50px; /* 设置左边距 */

  width: 200px; /* 设置宽度 */

  height: 100px; /* 设置高度 */

  background-color: #ccc; /* 设置背景颜色 */

}

设置相对定位

HTML

<!DOCTYPE html>

<html lang="en">

<head>

    <link rel="stylesheet" type="text/css" href="style.css">

</head>

<body>

    <div class="container">

        <div class="relative-element">

            <!-- 内容 -->

        </div>

    </div>

</body>

</html>

CSS:

.relative-element {

  position: relative;

  top: 20px; /* 设置相对于初始位置的上边距 */

  left: 20px; /* 设置相对于初始位置的左边距 */

  width: 200px; /* 设置宽度 */

  height: 100px; /* 设置高度 */

  background-color: #ccc; /* 设置背景颜色 */

}

定义表格及表单

HTML

<!DOCTYPE html>

<html>

<head>

  <link rel="stylesheet" type="text/css" href="style.css">

</head>

<body>

  <h2>表单示例</h2>

  

  <form>

    <label for="name">姓名:</label>

    <input type="text" id="name" name="name"><br><br>

    <label for="email">邮箱:</label>

    <input type="email" id="email" name="email"><br><br>

    <label for="message">留言:</label><br>

    <textarea id="message" name="message"></textarea><br><br>

    <input type="submit" value="提交">

  </form>

  <h2>表格示例</h2>

  <table>

    <tr>

      <th>姓名</th>

      <th>年龄</th>

    </tr>

    <tr>

      <td>张三</td>

      <td>25</td>

    </tr>

    <tr>

      <td>李四</td>

      <td>30</td>

    </tr>

  </table>

</body>

</html>

CSS:

label {

  display: inline-block;

  width: 100px;

}

input[type="text"],

input[type="email"],

textarea {

  width: 200px;

}

table {

  border-collapse: collapse;

  width: 100%;

}

th, td {

  border: 1px solid #dddddd;

  padding: 8px;

  text-align: left;

}

th {

  background-color: #f2f2f2;

}

设置多行文本输入框禁止被拉伸

HTML

<!DOCTYPE html>

<html>

<head>

  <link rel="stylesheet" type="text/css" href="style.css">

</head>

<body>

  <h2>多行文本输入框示例</h2>

  <textarea id="message" name="message"></textarea>

</body>

</html>

CSS:

textarea {

  width: 300px;

  height: 150px;

  resize: none; /* 禁止垂直和水平拉伸 */

}

底部通栏显示

HTML

<!DOCTYPE html>

<html>

<head>

  <link rel="stylesheet" type="text/css" href="style.css">

</head>

<body>

  <div class="wrapper">

    <p>这里是页面的主要内容</p>

  </div>

  <footer>

    <p>© 2024 我的网站</p>

  </footer>

</body>

</html>

CSS:

footer {

  background-color: #333;

  color: #fff;

  text-align: center;

  padding: 20px 0;

  position: fixed;

  bottom: 0;

  width: 100%;

}

  • 18
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值