基于openlayer4的地图卷帘效果


1.原理说明

基于openlayer4实现地图卷帘效果,说白了,就是创建两个地图容器,通过控制最上面的dom的样式中的rect(裁剪)来实现。

2.代码说明

<!DOCTYPE html>
<html>
<head>
    <title>地图卷帘效果</title>
    <link rel="stylesheet" href="https://openlayers.org/en/v4.6.4/css/ol.css" type="text/css">
    <script src="https://openlayers.org/en/v4.6.4/build/ol.js"></script>
    <style>
        body{
            overflow: hidden;
        }
        html,body,.map{
            width: 100%;
            height: 100%;
            position:absolute;
            box-sizing: border-box;
            padding: 0;
            margin: 0;
        }

    </style>
</head>
<body>
<div id="map_1" class="map"></div>
<div id="map_2" class="map "></div>
<script>
    var map_1 =  map = new ol.Map({
        target:"map_1",
        view:new ol.View({
            projection: "EPSG:4326",
            center:[115.7555794477557, 22.6070466884657],
            zoom:7
        }),
        layers:[
            new ol.layer.Tile({source:new ol.source.OSM()})
        ]
    });
    var map_2 =  map = new ol.Map({
        target:"map_2",
        view:map_1.getView(),
        layers:[
            new ol.layer.Tile({
                source: new ol.source.XYZ({
                    url:'http://t3.tianditu.cn/DataServer?T=img_w&X={x}&Y={y}&L={z}'
                }),
                projection: "EPSG:4326",
                id:"影像"
            })
        ]
    });
    document.getElementById('map_2').onmousemove=function(e){
        console.log(e);
        e.stopPropagation();
        var  offsetX=e.x,offsetY=e.y,width=document.body.clientWidth,height=document.body.clientHeight;
        document.getElementById('map_2').style.clip='rect(0px,'+offsetX+'px,'+height+'px,0px)';
    }
    document.getElementById('map_1').onmousemove=function(e){
        console.log(e);
        e.stopPropagation();
        var  offsetX=e.x,offsetY=e.y,width=document.body.clientWidth,height=document.body.clientHeight;
        document.getElementById('map_2').style.clip='rect(0px,'+offsetX+'px,'+height+'px,0px)';
    }
</script>
</body>
</html>

转载自:https://blog.csdn.net/YH20090580118/article/details/79666387

You may also like...