茄:修订间差异
来自潮语辞书
更多操作
页面内容被替换为“<html> <html lang="zh"> <head> <meta charset="UTF-8"> <title>交互 SVG 示例</title> <style> #tooltip { position: absolute; background: #fff; border: 1px solid #aaa; padding: 4px 8px; border-radius: 4px; display: none; pointer-events: none; font-size: 14px; } svg circle { cursor: pointer; transition: fill 0.2s; } svg circle:hover { fill: orange; } </style> </head> <body> <svg width="400" height="300" st…” 标签:替换 |
无编辑摘要 |
||
第3行: | 第3行: | ||
<head> | <head> | ||
<meta charset="UTF-8"> | <meta charset="UTF-8"> | ||
<title> | <title>SVG GeoJSON 示例</title> | ||
<style> | <style> | ||
svg { | |||
border: 1px solid #ccc; | |||
width: 600px; | |||
height: 400px; | |||
} | |||
#tooltip { | #tooltip { | ||
position: absolute; | position: absolute; | ||
第15行: | 第20行: | ||
font-size: 14px; | font-size: 14px; | ||
} | } | ||
svg | svg path { | ||
fill: #cce5ff; | |||
stroke: #333; | |||
cursor: pointer; | cursor: pointer; | ||
transition: fill 0.2s; | transition: fill 0.2s; | ||
} | } | ||
svg | svg path:hover { | ||
fill: orange; | fill: orange; | ||
} | } | ||
第26行: | 第33行: | ||
<body> | <body> | ||
<svg | <svg id="map"></svg> | ||
</svg> | |||
<div id="tooltip"></div> | <div id="tooltip"></div> | ||
<script> | <script> | ||
// 模拟 GeoJSON 数据(两块行政区) | |||
const geojson = { | |||
"type": "FeatureCollection", | |||
"features": [ | |||
{ "type": "Feature", "properties": { "Name": "区A" }, | |||
"geometry": { "type": "Polygon", "coordinates": [[[0,0],[0,50],[50,50],[50,0],[0,0]]] } | |||
}, | |||
{ "type": "Feature", "properties": { "Name": "区B" }, | |||
"geometry": { "type": "Polygon", "coordinates": [[[60,10],[60,60],[110,60],[110,10],[60,10]]] } | |||
} | |||
] | |||
}; | |||
const svg = document.getElementById('map'); | |||
const tooltip = document.getElementById('tooltip'); | const tooltip = document.getElementById('tooltip'); | ||
// 简单投影函数:将 GeoJSON 坐标映射到 SVG 像素 | |||
function project(coord) { | |||
const scale = 5; // 放大倍数 | |||
const xOffset = 50; | |||
const yOffset = 50; | |||
const [x, y] = coord; | |||
return [(x*scale + xOffset), (400 - (y*scale + yOffset))]; // SVG y 轴向下 | |||
} | |||
geojson.features.forEach(f => { | |||
const path = document.createElementNS('http://www.w3.org/2000/svg','path'); | |||
const coords = f.geometry.coordinates[0].map(c => project(c).join(',')).join('L'); | |||
path.setAttribute('d', 'M' + coords + 'Z'); | |||
path.setAttribute('data-name', f.properties.Name); | |||
svg.appendChild(path); | |||
// 鼠标事件 | |||
path.addEventListener('mousemove', e => { | |||
tooltip.style.left = (e.pageX + 10) + 'px'; | tooltip.style.left = (e.pageX + 10) + 'px'; | ||
tooltip.style.top = (e.pageY + 10) + 'px'; | tooltip.style.top = (e.pageY + 10) + 'px'; | ||
tooltip.innerHTML = | tooltip.innerHTML = f.properties.Name; | ||
tooltip.style.display = 'block'; | tooltip.style.display = 'block'; | ||
}); | }); | ||
path.addEventListener('mouseleave', () => { | |||
tooltip.style.display = 'none'; | tooltip.style.display = 'none'; | ||
}); | |||
path.addEventListener('click', () => { | |||
alert('你点击了 ' + f.properties.Name); | |||
}); | }); | ||
}); | }); |
2025年8月13日 (三) 19:29的最新版本