基于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>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值