原文地址:http://openlayers.org/en/v3.12.1/examples/accessible.html
This page’s map element has its tabindex attribute set to “0”, that makes it focusable. To focus the map element you can either navigate to it using the “tab” key or use the skip link. When the map element is focused the + and - keys can be used to zoom in and out and the arrow keys can be used to pan.
Clicking on the “Zoom in” and “Zoom out” buttons below the map zooms the map in and out. You can navigate to the buttons using the “tab” key, and press the “enter” key to trigger the zooming action.
Related API documentation: ol.Map,ol.View,ol.control,ol.layer.Tile,ol.source.OSM
翻译:
这个页面上的map元素的tabindex为0,默认聚焦。可是使用tab键或者使用ship link,来聚焦到map的。当map元素被聚焦的时候,可以使用+和-来进行地图缩放,可以使用方向键来平移地图。
也可以单击下方的zoom in 和zoom out来进行地图的缩放,或者使用tab切换到这2个按钮的时候,敲击回车键来触发缩放效果。
相关的api文档包括:
ol.Map,ol.View,ol.control,ol.layer.Tile,ol.source.OSM
<!DOCTYPE html>
<html>
<head>
<title>Accessible Map</title>
<link rel="stylesheet" href="http://openlayers.org/en/v3.12.1/css/ol.css" type="text/css">
<script src="http://openlayers.org/en/v3.12.1/build/ol.js"></script>
<style>
a.skiplink {
position: absolute;
clip: rect(1px, 1px, 1px, 1px);
padding: 0;
border: 0;
height: 1px;
width: 1px;
overflow: hidden;
}
a.skiplink:focus {
clip: auto;
height: auto;
width: auto;
background-color: #fff;
padding: 0.3em;
}
</style>
</head>
<body>
<a class="skiplink" href="#map">Go to map</a>
<div id="map" class="map" tabindex="0"></div>
//添加缩放按钮
<button id="zoom-out">Zoom out</button>
<button id="zoom-in">Zoom in</button>
<script>
//添加地图
var map = new ol.Map({
//设定地图图层
layers: [
new ol.layer.Tile({
//添加ol.layer,并设定source来源,来自osm
source: new ol.source.OSM()
})
],
//设定加载的div容器
target: 'map',
//添加控制条
controls: ol.control.defaults({
attributionOptions: /** @type {olx.control.AttributionOptions} */ ({
collapsible: false
})
}),
//设定地图中心点以及缩放级别
view: new ol.View({
center: [0, 0],
zoom: 2
})
});
document.getElementById('zoom-out').onclick = function() {
//获得view
var view = map.getView();
//通过view获得缩放级别
var zoom = view.getZoom();
//设定新的缩放级别
view.setZoom(zoom - 1);
};
document.getElementById('zoom-in').onclick = function() {
var view = map.getView();
var zoom = view.getZoom();
view.setZoom(zoom + 1);
};
</script>
</body>
</html>