首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

使用 jQuery,如何实现“页面加载”动画?

在使用 jQuery 实现“页面加载”动画时,可以使用以下步骤:

  1. 首先,确保已经在页面中引入了 jQuery 库。
  2. 创建一个包含动画内容的 HTML 元素,例如:
代码语言:html
复制
<div id="loading-animation">
  <img src="loading.gif" alt="Loading..." />
</div>
  1. 使用 CSS 设置动画元素的样式,例如:
代码语言:css
复制
#loading-animation {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(255, 255, 255, 0.8);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 9999;
}
  1. 使用 jQuery 的 $(document).ready() 方法在页面加载完成时隐藏动画元素:
代码语言:javascript
复制
$(document).ready(function() {
  $('#loading-animation').fadeOut('slow');
});
  1. 使用 jQuery 的 $(window).on('load', function() {...}) 方法在页面所有资源加载完成时隐藏动画元素:
代码语言:javascript
复制
$(window).on('load', function() {
  $('#loading-animation').fadeOut('slow');
});

完整的代码示例如下:

代码语言:html
复制
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <title>页面加载动画</title>
 <style>
    #loading-animation {
      position: fixed;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      background-color: rgba(255, 255, 255, 0.8);
      display: flex;
      justify-content: center;
      align-items: center;
      z-index: 9999;
    }
  </style>
 <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
  <div id="loading-animation">
    <img src="loading.gif" alt="Loading..." />
  </div>
  <!-- 页面其他内容 -->
 <script>
    $(document).ready(function() {
      $('#loading-animation').fadeOut('slow');
    });

    $(window).on('load', function() {
      $('#loading-animation').fadeOut('slow');
    });
  </script>
</body>
</html>

这样,在页面加载时,就会显示指定的动画元素,当页面加载完成后,动画元素会逐渐消失。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

领券