Atlaslog
Atlaslog / Examples / OpenLayers / Tile Transition
OpenLayers · Runnable Example

Tile Transition

Runnable OpenLayers example: Tile Transition. Open the live demo, read the complete source, and copy the file into your project. No build step or API key.

Open standalone → Chinese notes All OpenLayers examples
/demos/openlayers-tile-transition.htmlLIVE
single HTML file · CDN dependencies onlyinterface may contain Chinese labels

Source

demos/openlayers-tile-transition.html
<!--
  示例:瓦片过渡效果
  要点:两个并排地图对比 transition 0 与默认 250ms 的瓦片淡入
  依赖:OpenLayers 10.3.1 · jsDelivr CDN
-->
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta name="robots" content="noindex,follow">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>瓦片过渡效果</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/ol@10.3.1/ol.css">
<style>
  html,body{height:100%;margin:0}
  body{display:flex;flex-direction:column;overflow:hidden;
       font:13px/1.6 -apple-system,"Segoe UI","PingFang SC",sans-serif}
  .bar{flex:0 0 auto;padding:8px 12px;background:#f6f7f9;border-bottom:1px solid #e3e6ea;
       display:flex;gap:12px;align-items:center;font-size:12px;color:#58606e;flex-wrap:wrap}
  .bar button{font:inherit;font-size:12px;padding:4px 10px;border:1px solid #ccd1d9;
       border-radius:4px;background:#fff;color:#16181d;cursor:pointer}
  .panes{flex:1 1 auto;display:flex;min-height:0}
  .pane{flex:1 1 50%;position:relative;min-width:0}
  .pane + .pane{border-left:1px solid #e3e6ea}
  .pane .map{position:absolute;inset:0;background:#eef0f3}
  .cap{position:absolute;bottom:10px;left:10px;z-index:2;background:rgba(255,255,255,0.92);
       border:1px solid #e3e6ea;border-radius:4px;padding:3px 8px;font-size:12px;color:#58606e}
</style>
</head>
<body>
<div class="bar">
  <button id="replay">重新缩放</button>
  <span>观察缩放瞬间两张图的新瓦片出现方式</span>
</div>
<div class="panes">
  <div class="pane">
    <div class="cap">transition: 0(直接替换)</div>
    <div id="mapA" class="map"></div>
  </div>
  <div class="pane">
    <div class="cap">transition: 250(淡入)</div>
    <div id="mapB" class="map"></div>
  </div>
</div>
<script src="https://cdn.jsdelivr.net/npm/ol@10.3.1/dist/ol.js"></script>
<script>
(function () {
  'use strict';

  if (typeof ol === 'undefined') {
    document.body.innerHTML =
      '<div style="padding:20px;color:#b42318">OpenLayers 加载失败,请检查能否访问 cdn.jsdelivr.net</div>';
    return;
  }

  var fromLonLat = ol.proj.fromLonLat;
  var center = fromLonLat([116.391263, 39.907359]);

  function buildMap(target, transition) {
    return new ol.Map({
      target: target,
      layers: [
        new ol.layer.Tile({
          source: new ol.source.XYZ({url: 'https://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer/tile/{z}/{y}/{x}', attributions: 'Tiles © Esri'}),
          transition: transition
        })
      ],
      view: new ol.View({
        center: center,
        zoom: 10,
        minZoom: 2,
        maxZoom: 19
      }),
      controls: ol.control.defaults.defaults({
        attribution: false,
        rotate: false
      })
    });
  }

  var mapA = buildMap('mapA', 0);
  var mapB = buildMap('mapB', 250);

  /* 两个视图同步,避免对比时位置不一致 */
  mapA.getView().on('change:center', function () {
    mapB.getView().setCenter(mapA.getView().getCenter());
  });
  mapA.getView().on('change:resolution', function () {
    mapB.getView().setResolution(mapA.getView().getResolution());
  });

  var z = 10;

  document.getElementById('replay').addEventListener('click', function () {
    z = z === 10 ? 14 : 10;

    /* 只驱动左图,右图通过视图事件同步,避免两套动画互相覆盖 */
    mapA.getView().animate({
      zoom: z,
      duration: 1400,
      easing: ol.easing.inAndOut
    });
  });
})();
</script>
</body>
</html>
Language: the code and this page are in English; the full tutorial and API notes for this example are in Chinese. The demo itself is the authoritative version — it runs exactly as shown.

More OpenLayers examples

OpenLayers · EXAMPLETile Transitions OpenLayers · EXAMPLETissot OpenLayers · EXAMPLETooltip on Hover