@@ -74,32 +74,26 @@ <h1>Welcome to New York</h1>
7474 </ a >
7575 </ div >
7676
77- <!-- 嵌入外部 SVG 文件 -->
7877 < div class ="map-container ">
7978 < object id ="usaMap " data ="Map_of_New_York_County_Outlines.svg " type ="image/svg+xml " style ="width: 60%; max-width: auto; height: auto; border: 1px solid #ddd; "> </ object >
8079 < div id ="stateInfo "> </ div >
8180 </ div >
8281
8382
8483 < script >
85- // 定义特定的州 ID 列表
86- // const targetStates = ["AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "FL", "GA", "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MD", "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH", "NJ", "NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", "RI", "SC", "SD", "TN", "TX", "UT", "VT", "VA", "WA", "WV", "WI", "WY"]; // 替换为你需要的州 ID
87- const targetRegions = [ "Bronx" , "Suffolk" , "Westchester" , "New_York" , ] ; // 替换为你需要的州 ID
88- // 添加鼠标交互
84+ const targetRegions = [ "Bronx" , "Suffolk" , "Westchester" , "New_York" , ] ;
8985 const svgObject = document . getElementById ( 'usaMap' ) ;
9086 svgObject . addEventListener ( 'load' , ( ) => {
91- const svgDoc = svgObject . contentDocument ; // 获取嵌入的 SVG 文档
92- const paths = svgDoc . querySelectorAll ( 'path' ) ; // 假设地图中的州使用 <path> 标签
93- const regions = svgDoc . querySelectorAll ( 'polygon' ) ; // 假设地图中的州使用 <g> 标签
87+ const svgDoc = svgObject . contentDocument ;
88+ const paths = svgDoc . querySelectorAll ( 'path' ) ;
89+ const regions = svgDoc . querySelectorAll ( 'polygon' ) ;
9490
95-
96- // 遍历每个州,并添加交互
9791 regions . forEach ( region => {
98- if ( region . id && ! region . id . includes ( "path" ) ) { // 只处理特定 ID 的州
92+ if ( region . id && ! region . id . includes ( "path" ) ) {
9993 region . addEventListener ( 'mouseover' , ( ) => {
100- region . style . fill = '#FF0000' ; // 鼠标悬停时高亮
94+ region . style . fill = '#FF0000' ;
10195
102- // 显示州名
96+
10397 const stateInfo = document . getElementById ( 'stateInfo' ) ;
10498 stateInfo . style . display = 'block' ;
10599 const processedId = region . id . substring ( region . id . lastIndexOf ( '_' ) + 1 ) ;
@@ -109,21 +103,21 @@ <h3>${processedId}</h3>
109103 ` ;
110104 } ) ;
111105 region . addEventListener ( 'mouseout' , ( ) => {
112- region . style . fill = '' ; // 恢复原样
106+ region . style . fill = '' ;
113107 } ) ;
114108 region . addEventListener ( 'click' , ( ) => {
115- const targetUrl = `https://danielchen3.github.io/usa_map/${ region . id } ` ; // 替换为你需要跳转的网址
116- window . location . href = targetUrl ; // 跳转到指定网页
109+ const targetUrl = `https://danielchen3.github.io/usa_map/${ region . id } ` ;
110+ window . location . href = targetUrl ;
117111 } ) ;
118112 }
119113 } ) ;
120114
121115 paths . forEach ( path => {
122- if ( path . id && ! path . id . includes ( "path" ) && targetRegions . includes ( path . id ) ) { // 只处理特定 ID 的州
116+ if ( path . id && ! path . id . includes ( "path" ) && targetRegions . includes ( path . id ) ) {
123117 path . addEventListener ( 'mouseover' , ( ) => {
124- path . style . fill = '#FF0000' ; // 鼠标悬停时高亮
118+ path . style . fill = '#FF0000' ;
125119
126- // 显示州名
120+
127121 const stateInfo = document . getElementById ( 'stateInfo' ) ;
128122 stateInfo . style . display = 'block' ;
129123 const processedId = path . id ;
@@ -133,11 +127,11 @@ <h3>${processedId}</h3>
133127 ` ;
134128 } ) ;
135129 path . addEventListener ( 'mouseout' , ( ) => {
136- path . style . fill = '' ; // 恢复原样
130+ path . style . fill = '' ;
137131 } ) ;
138132 path . addEventListener ( 'click' , ( ) => {
139- const targetUrl = `https://danielchen3.github.io/usa_map/${ path . id } ` ; // 替换为你需要跳转的网址
140- window . location . href = targetUrl ; // 跳转到指定网页
133+ const targetUrl = `https://danielchen3.github.io/usa_map/${ path . id } ` ;
134+ window . location . href = targetUrl ;
141135 } ) ;
142136 }
143137 } ) ;
0 commit comments