使用javascript實(shí)現(xiàn)頁面加載loading動(dòng)畫(附完整源碼)
1.創(chuàng)建一個(gè)CSS樣式,用于顯示loading動(dòng)畫。
<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中存儲(chǔ)的是數(shù)據(jù)(狀態(tài)管理,使用的是pinia)
import { defineStore } from "pinia"; const useMainStore = defineStore("mian", { state: () => ({ isLoading: true, )}, }); export default useMainStore;
5.在request.js接口請求和響應(yīng)攔截時(shí)做處理
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實(shí)現(xiàn)頁面加載時(shí)帶loading進(jìn)度條
<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實(shí)現(xiàn)頁面加載時(shí)帶loading進(jìn)度條" > </div> <div id="divPageBody" style="display:none;"> …… </div> </body> </html>
總結(jié)
到此這篇關(guān)于使用javascript實(shí)現(xiàn)頁面加載loading動(dòng)畫的文章就介紹到這了,更多相關(guān)js頁面加載loading動(dòng)畫內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JavaScript接口實(shí)現(xiàn)方法實(shí)例分析
這篇文章主要介紹了JavaScript接口實(shí)現(xiàn)方法,結(jié)合實(shí)例形式詳細(xì)分析了JavaScript接口實(shí)現(xiàn)原理、操作步驟與相關(guān)注意事項(xiàng),需要的朋友可以參考下2020-05-05JS仿京東移動(dòng)端手指撥動(dòng)切換輪播圖效果
這篇文章主要為大家詳細(xì)介紹了JS仿京東移動(dòng)端手指撥動(dòng)切換輪播圖效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12uniapp實(shí)現(xiàn)app檢查更新與升級(jí)詳解(uni-upgrade-center)
UniApp是一個(gè)跨平臺(tái)的開發(fā)框架,可以同時(shí)開發(fā)iOS和Android應(yīng)用,下面這篇文章主要給大家介紹了關(guān)于uniapp實(shí)現(xiàn)app檢查更新與升級(jí)(uni-upgrade-center)的相關(guān)資料,需要的朋友可以參考下2023-11-11微信小程序?qū)崿F(xiàn)可拖動(dòng)懸浮圖標(biāo)的示例代碼
這篇文章主要介紹了微信小程序?qū)崿F(xiàn)可拖動(dòng)懸浮圖標(biāo)的示例代碼,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2023-12-12原生javascript實(shí)現(xiàn)Tab選項(xiàng)卡切換功能
本文主要介紹了使用原生javascript實(shí)現(xiàn)Tab選項(xiàng)卡切換的功能,雖然jQuery有很多類似的插件,單jQuery庫著實(shí)有點(diǎn)龐大,這種小功能還是直接用javascript來做就好了。2015-01-01