原文链接及内容

你可以监听地图的loadstart
和loadend
事件,在地图的顶部显示一个加载旋转器(加载动效)。
main.js
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| import Map from 'ol/Map.js'; import TileLayer from 'ol/layer/Tile.js'; import View from 'ol/View.js'; import XYZ from 'ol/source/XYZ.js';
const key = 'get_your_own_D6rA4zTHduk6KOKTXzGB'; const attributions = '<a href="https://www.maptiler.com/copyright/" target="_blank">© MapTiler</a> ' + '<a href="https://www.openstreetmap.org/copyright" target="_blank">© OpenStreetMap contributors</a>';
const source = new XYZ({ attributions: attributions, url: 'https://api.maptiler.com/maps/streets/{z}/{x}/{y}.png?key=' + key, tileSize: 512, });
const map = new Map({ layers: [new TileLayer({source: source})], target: 'map', view: new View({ center: [0, 0], zoom: 2, }), });
map.on('loadstart', function () { map.getTargetElement().classList.add('spinner'); }); map.on('loadend', function () { map.getTargetElement().classList.remove('spinner'); });
|
界面布局文件index.html
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Loading Spinner</title> <link rel="stylesheet" href="node_modules/ol/ol.css"> <style> .map { width: 100%; height: 400px; } .map { background: #85ccf9; position: relative; } #map { height: 400px; position: relative; }
@keyframes spinner { to { transform: rotate(360deg); } }
.spinner:after { content: ""; box-sizing: border-box; position: absolute; top: 50%; left: 50%; width: 40px; height: 40px; margin-top: -20px; margin-left: -20px; border-radius: 50%; border: 5px solid rgba(180, 180, 180, 0.6); border-top-color: rgba(0, 0, 0, 0.6); animation: spinner 0.6s linear infinite; } </style> </head> <body> <div id="map" class="map"></div> <script src="https://cdn.jsdelivr.net/npm/elm-pep@1.0.6/dist/elm-pep.js"></script> <script type="module" src="main.js"></script> </body> </html>
|