• 利用a标签锚点定位实现切换页面的部分内容


            最近在做一个数据可视化大屏的作业,其中需要实现点击不同的按钮,大屏中间内容呈现不同的数据分析图表,页面其他部分不发生改变。之前考虑过复制多个页面然后改变中间的页面,但是这样会导致文件冗余,而且由于静态文件放在不同的文件夹,所以每个复制的页面都要调整文件路径!这将极大地增加工作量。

            后面在网上查了好久,终于发现一个可以用a标签的锚点定位来实现这一效果。(当然也有一些用js实现的,但是我还么学,所以只能用这个了,而且这个也是非常方便相较于js)

            废话不多说先看代码。因为我这里嵌入了echarts代码,可能看上去代码量大了一些。不过核心就是下面这句话:把所有要切换的内容放在一个大的div里面,然后用对应的div放对应的要切换的页面,每个div设置一个id。我这里先展示了2个div,分别是map和goal。

            然后再把a标签中的href设置为#加上div的id名即可

    1. <div class="show_state">
    2. <div id="map">div>
    3. <script>
    4. var chart_a89deb8db1fb42ce8c00f23e9a3bb018 = echarts.init(
    5. document.getElementById('map'), 'white', {renderer: 'canvas'});
    6. var option_a89deb8db1fb42ce8c00f23e9a3bb018 = {
    7. "animation": true,
    8. "animationThreshold": 2000,
    9. "animationDuration": 1000,
    10. "animationEasing": "cubicOut",
    11. "animationDelay": 0,
    12. "animationDurationUpdate": 300,
    13. "animationEasingUpdate": "cubicOut",
    14. "animationDelayUpdate": 0,
    15. "aria": {
    16. "enabled": false
    17. },
    18. "color": [
    19. "#5470c6",
    20. "#91cc75",
    21. "#fac858",
    22. "#ee6666",
    23. "#73c0de",
    24. "#3ba272",
    25. "#fc8452",
    26. "#9a60b4",
    27. "#ea7ccc"
    28. ],
    29. "series": [
    30. {
    31. "type": "map3D",
    32. "map": "china",
    33. "coordinateSystem": "geo3D",
    34. "label": {
    35. "show": false,
    36. "margin": 8,
    37. "formatter": function(data){return data.name + ' ' + data.value[2];}
    38. },
    39. "data": [],
    40. "showLegendSymbol": true,
    41. "emphasis": {}
    42. }
    43. ],
    44. "legend": [
    45. {
    46. "data": [
    47. ""
    48. ],
    49. "selected": {},
    50. "show": false,
    51. "padding": 5,
    52. "itemGap": 10,
    53. "itemWidth": 25,
    54. "itemHeight": 14,
    55. "backgroundColor": "transparent",
    56. "borderColor": "#ccc",
    57. "borderWidth": 1,
    58. "borderRadius": 0,
    59. "pageButtonItemGap": 5,
    60. "pageButtonPosition": "end",
    61. "pageFormatter": "{current}/{total}",
    62. "pageIconColor": "#2f4554",
    63. "pageIconInactiveColor": "#aaa",
    64. "pageIconSize": 15,
    65. "animationDurationUpdate": 800,
    66. "selector": false,
    67. "selectorPosition": "auto",
    68. "selectorItemGap": 7,
    69. "selectorButtonGap": 10
    70. }
    71. ],
    72. "tooltip": {
    73. "show": true,
    74. "trigger": "item",
    75. "triggerOn": "mousemove|click",
    76. "axisPointer": {
    77. "type": "line"
    78. },
    79. "showContent": true,
    80. "alwaysShowContent": false,
    81. "showDelay": 0,
    82. "hideDelay": 100,
    83. "enterable": false,
    84. "confine": false,
    85. "appendToBody": false,
    86. "transitionDuration": 0.4,
    87. "textStyle": {
    88. "fontSize": 14
    89. },
    90. "borderWidth": 0,
    91. "padding": 5,
    92. "order": "seriesAsc"
    93. },
    94. "geo3D": {
    95. "map": "\u5fb7\u56fd",
    96. "boxWidth": 100,
    97. "boxHeight": 100,
    98. "boxDepth": 80,
    99. "regionHeight": 3,
    100. "groundPlane": {
    101. "show": false,
    102. "color": "#aaa"
    103. },
    104. "instancing": false,
    105. "itemStyle": {
    106. "color": "rgb(5,101,123)",
    107. "borderColor": "rgb(62,215,213)",
    108. "borderWidth": 0.8,
    109. "opacity": 1
    110. },
    111. "label": {
    112. "show": false,
    113. "formatter": function(data){return data.name + + data.value[2];}
    114. },
    115. "emphasis": {
    116. "label": {
    117. "show": false,
    118. "color": "#fff",
    119. "margin": 8,
    120. "fontSize": 10,
    121. "backgroundColor": "rgba(0,23,11,0)"
    122. }
    123. },
    124. "light": {
    125. "main": {
    126. "color": "#fff",
    127. "intensity": 1.2,
    128. "shadow": false,
    129. "shadowQuality": "high",
    130. "alpha": 40,
    131. "beta": 10
    132. },
    133. "ambient": {
    134. "color": "#fff",
    135. "intensity": 0.3
    136. },
    137. "ambientCubemap": {
    138. "diffuseIntensity": 0.5,
    139. "specularIntensity": 0.5
    140. }
    141. },
    142. "temporalSuperSampling": {
    143. "enable": "auto"
    144. },
    145. "zlevel": -10,
    146. "left": "auto",
    147. "top": "auto",
    148. "right": "auto",
    149. "bottom": "auto",
    150. "width": "auto",
    151. "height": "auto"
    152. },
    153. "title": [
    154. {
    155. "show": true,
    156. "target": "blank",
    157. "subtarget": "blank",
    158. "padding": 5,
    159. "itemGap": 10,
    160. "textAlign": "auto",
    161. "textVerticalAlign": "auto",
    162. "triggerEvent": false
    163. }
    164. ]
    165. };
    166. chart_a89deb8db1fb42ce8c00f23e9a3bb018.setOption(option_a89deb8db1fb42ce8c00f23e9a3bb018);
    167. script>
    168. <div id="goal">div>
    169. <script>
    170. var chart_eaa4cf6c1af94509803071b8f3657f5b = echarts.init(
    171. document.getElementById('goal'), 'white', {renderer: 'canvas'});
    172. var option_eaa4cf6c1af94509803071b8f3657f5b = {
    173. "animation": true,
    174. "animationThreshold": 2000,
    175. "animationDuration": 1000,
    176. "animationEasing": "cubicOut",
    177. "animationDelay": 0,
    178. "animationDurationUpdate": 300,
    179. "animationEasingUpdate": "cubicOut",
    180. "animationDelayUpdate": 0,
    181. "aria": {
    182. "enabled": false
    183. },
    184. "color": [
    185. "blue",
    186. "green",
    187. "yellow"
    188. ],
    189. "series": [
    190. {
    191. "type": "pie",
    192. "name": "\u8fdb\u7403\u6765\u6e90\u5360\u6bd4",
    193. "colorBy": "data",
    194. "legendHoverLink": true,
    195. "selectedMode": false,
    196. "selectedOffset": 10,
    197. "clockwise": true,
    198. "startAngle": 90,
    199. "minAngle": 0,
    200. "minShowLabelAngle": 0,
    201. "avoidLabelOverlap": true,
    202. "stillShowZeroSum": true,
    203. "percentPrecision": 2,
    204. "showEmptyCircle": true,
    205. "emptyCircleStyle": {
    206. "color": "lightgray",
    207. "borderColor": "#000",
    208. "borderWidth": 0,
    209. "borderType": "solid",
    210. "borderDashOffset": 0,
    211. "borderCap": "butt",
    212. "borderJoin": "bevel",
    213. "borderMiterLimit": 10,
    214. "opacity": 1
    215. },
    216. "data": [
    217. {
    218. "name": "\u5927\u7981\u533a\u5185\u8fdb\u7403",
    219. "value": 2
    220. },
    221. {
    222. "name": "\u5c0f\u7981\u533a\u5185\u7981\u533a",
    223. "value": 12
    224. },
    225. {
    226. "name": "\u7981\u533a\u5916\u8fdb\u7403",
    227. "value": 1
    228. }
    229. ],
    230. "radius": [
    231. "0%",
    232. "75%"
    233. ],
    234. "center": [
    235. "50%",
    236. "50%"
    237. ],
    238. "label": {
    239. "show": true,
    240. "margin": 8,
    241. "formatter": "{b}: {c}"
    242. },
    243. "labelLine": {
    244. "show": true,
    245. "showAbove": false,
    246. "length": 15,
    247. "length2": 15,
    248. "smooth": false,
    249. "minTurnAngle": 90,
    250. "maxSurfaceAngle": 90
    251. },
    252. "tooltip": {
    253. "show": true,
    254. "trigger": "item",
    255. "triggerOn": "mousemove|click",
    256. "axisPointer": {
    257. "type": "line"
    258. },
    259. "showContent": true,
    260. "alwaysShowContent": false,
    261. "showDelay": 0,
    262. "hideDelay": 100,
    263. "enterable": false,
    264. "confine": false,
    265. "appendToBody": false,
    266. "transitionDuration": 0.4,
    267. "formatter": "{a}
      {b}: {c} ({d}%)"
      ,
    268. "textStyle": {
    269. "fontSize": 14
    270. },
    271. "borderWidth": 0,
    272. "padding": 5,
    273. "order": "seriesAsc"
    274. },
    275. "rippleEffect": {
    276. "show": true,
    277. "brushType": "stroke",
    278. "scale": 2.5,
    279. "period": 4
    280. }
    281. }
    282. ],
    283. "legend": [
    284. {
    285. "data": [
    286. "\u5927\u7981\u533a\u5185\u8fdb\u7403",
    287. "\u5c0f\u7981\u533a\u5185\u7981\u533a",
    288. "\u7981\u533a\u5916\u8fdb\u7403"
    289. ],
    290. "selected": {},
    291. "show": true,
    292. "padding": 5,
    293. "itemGap": 10,
    294. "itemWidth": 25,
    295. "itemHeight": 14,
    296. "backgroundColor": "transparent",
    297. "borderColor": "#ccc",
    298. "borderWidth": 1,
    299. "borderRadius": 0,
    300. "pageButtonItemGap": 5,
    301. "pageButtonPosition": "end",
    302. "pageFormatter": "{current}/{total}",
    303. "pageIconColor": "#2f4554",
    304. "pageIconInactiveColor": "#aaa",
    305. "pageIconSize": 15,
    306. "animationDurationUpdate": 800,
    307. "selector": false,
    308. "selectorPosition": "auto",
    309. "selectorItemGap": 7,
    310. "selectorButtonGap": 10
    311. }
    312. ],
    313. "tooltip": {
    314. "show": true,
    315. "trigger": "item",
    316. "triggerOn": "mousemove|click",
    317. "axisPointer": {
    318. "type": "line"
    319. },
    320. "showContent": true,
    321. "alwaysShowContent": false,
    322. "showDelay": 0,
    323. "hideDelay": 100,
    324. "enterable": false,
    325. "confine": false,
    326. "appendToBody": false,
    327. "transitionDuration": 0.4,
    328. "textStyle": {
    329. "fontSize": 14
    330. },
    331. "borderWidth": 0,
    332. "padding": 5,
    333. "order": "seriesAsc"
    334. },
    335. "title": [
    336. {
    337. "show": true,
    338. "text": "\u5fb7\u56fd\u8fdb\u7403\u5206\u6790",
    339. "target": "blank",
    340. "subtarget": "blank",
    341. "padding": 5,
    342. "itemGap": 10,
    343. "textAlign": "auto",
    344. "textVerticalAlign": "auto",
    345. "triggerEvent": false
    346. }
    347. ]
    348. };
    349. chart_eaa4cf6c1af94509803071b8f3657f5b.setOption(option_eaa4cf6c1af94509803071b8f3657f5b);
    350. script>
    351. div>

     css代码如下。其中最重要的是overflow:hidden这句话,可以隐藏其他没有被切换的代码

    1. /*展示国家地图和数据分析的图表*/
    2. .show_state {
    3. position: relative;
    4. width: 100%;
    5. height: 74%;
    6. overflow: hidden; /*很重要,用于隐藏其他要展示的内容*/
    7. }
    8. /*展示国家地图*/
    9. #map {
    10. position: relative;
    11. top: -5%;
    12. width: 100%;
    13. height: 100%;
    14. }
    15. #goal {
    16. position: relative;
    17. top: 15%;
    18. left: 5%;
    19. width: 100%;
    20. height: 100%;
    21. }

  • 相关阅读:
    pytest简介及jenkins集成
    软件测试之Web项目实战解析
    tiup cluster template
    前端制作
    Linux:LVS (DR群集搭建)
    Apple 注销账户 Revoke Token
    Java Double isInfinite(double v)方法具有什么功能呢?
    【MindSpore易点通】数据处理之Numpy数组的使用
    1.网络IO与select学习
    将 TiDB 迁移至 Kubernetes
  • 原文地址:https://blog.csdn.net/weixin_62588253/article/details/134075543