使用javascript實現(xiàn)頁面加載loading動畫(附完整源碼)
1.創(chuàng)建一個CSS樣式,用于顯示loading動畫。
<style lang="less" scoped> .loading { position: fixed; z-index: 999; left: 0; right: 0; top: 0; bottom: 0; display: flex; justify-content: center; align-items: center; background-color: rgba(0, 0, 0, 0.5); } .bg { display: flex; justify-content: center; align-items: center; width: 104px; height: 104px; background: url(@/assets/img/home/loading-bg.png) 0 0 / 100% 100%; img { width: 70px; height: 70px; margin-bottom: 8px; } } </style>
2.在HTML文件中添加loading元素:
<template> <div class="loading" v-if="mainStore.isLoading" @click="loadingClick"> <div class="bg"> <img src="@/assets/img/home/full-screen-loading.gif" alt="" /> </div> </div> </template>
3.使用JavaScript,在頁面加載完成后,隱藏loading元素:
<script setup> //1.引入main.js import useMainStore from "@/stores/modules/main"; const mainStore = useMainStore(); const loadingClick = () => { mainStore.isLoading = false; };
4.開發(fā)利用的是組件化開發(fā),在main.js中存儲的是數(shù)據(jù)(狀態(tài)管理,使用的是pinia)
import { defineStore } from "pinia"; const useMainStore = defineStore("mian", { state: () => ({ isLoading: true, )}, }); export default useMainStore;
5.在request.js接口請求和響應(yīng)攔截時做處理
import axios from "axios"; import { BASE_URL, TIMEOUT } from "./config"; import useMainStore from "@/stores/modules/main"; const mainStore = useMainStore(); class HYRequest { constructor(baseURL, timeout = 10000) { this.instance = axios.create({ baseURL, timeout, }); // Loading加載顯示與隱藏 第二種寫法攔截請求 //請求之前攔截 this.instance.interceptors.request.use( (config) => { mainStore.isLoading = true; return config; }, (err) => { return err; } ); // 響應(yīng)之前攔截 this.instance.interceptors.response.use( (res) => { mainStore.isLoading = false; return res; }, (err) => { mainStore.isLoading = false; return err; } ); } request(config) { mainStore.isLoading = true; return new Promise((resolve, reject) => { this.instance .request(config) .then((res) => { resolve(res.data); // 第一種寫法:Loading加載顯示與隱藏 // 請求結(jié)束后設(shè)置為false mainStore.isLoading = false; }) .catch((err) => { reject(err); mainStore.isLoading = false; }); }); } get(config) { return this.request({ ...config, method: "get" }); } post(config) { return this.request({ ...config, method: "post" }); } } export default new HYRequest(BASE_URL, TIMEOUT);
附:js實現(xiàn)頁面加載時帶loading進度條
<html> <head> <script language="javascript" type="text/javascript" src="jquery-1.6.1.js"> </script> /** * 頁面加載完成后顯示頁面 */ function showPage(){ $('#divPageLoading').remove(); $('#divPageBody').show(); } </head> <body onload="showPage();"> <div id="divPageLoading"> <img src="pageloading.gif" style="max-width:90%"/ alt="JS實現(xiàn)頁面加載時帶loading進度條" > </div> <div id="divPageBody" style="display:none;"> …… </div> </body> </html>
總結(jié)
到此這篇關(guān)于使用javascript實現(xiàn)頁面加載loading動畫的文章就介紹到這了,更多相關(guān)js頁面加載loading動畫內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
uniapp實現(xiàn)app檢查更新與升級詳解(uni-upgrade-center)
UniApp是一個跨平臺的開發(fā)框架,可以同時開發(fā)iOS和Android應(yīng)用,下面這篇文章主要給大家介紹了關(guān)于uniapp實現(xiàn)app檢查更新與升級(uni-upgrade-center)的相關(guān)資料,需要的朋友可以參考下2023-11-11原生javascript實現(xiàn)Tab選項卡切換功能
本文主要介紹了使用原生javascript實現(xiàn)Tab選項卡切換的功能,雖然jQuery有很多類似的插件,單jQuery庫著實有點龐大,這種小功能還是直接用javascript來做就好了。2015-01-01