我们要在地图上有限的区块内展示更多的信息,更好的办法是通过地图交互来实现。本文将给大家讲解通过鼠标滑动到地图指定省份区域,在弹出的提示框中显示对应省份的数据信息。适用于数据统计和地图区块展示等场景。
查看演示 下载源码
本文紧接本站上一篇文章:PHP+Mysql+jQuery实现地图区域数据统计-载入数据,在原文实例基础上新加提示框展示数据功能,如果您对地图绘制和数据载入不太了解,建议先阅读本站上篇文章的介绍。
HTML
首先在head部分载入raphael.js库文件和chinamapPath.js路径信息文件,本文不重复写下,唯一不同的地方是需要在body中加一个div#tip,用来展示地图信息的提示框。
<code class="html"><span class="html__tag_start"><div</span> <span class="html__attr_name">id</span>=<span class="html__attr_value">"map"</span><span class="html__tag_start">></span><span class="html__tag_end"></div></span> <br><span class="html__tag_start"><div</span> <span class="html__attr_name">id</span>=<span class="html__attr_value">"tip"</span><span class="html__tag_start">></span><span class="html__tag_end"></div></span> <br></code>
jQuery
通过调用raphael绘制出中国地图,然后载入统计数据,由于地图区块小,我们不在地图载入的时候就将数据显示在地图区块上了,我们通过鼠标交互实现将数据信息更好的展示给用户。当鼠标滑向省份区块时,通过e.clientX和e.clientY定位鼠标坐标,然后通过jquery的css()方法定位提示框div#tip,并且将对应省份的的名称和活跃用户数加到提示框里并展现出来,请看代码:
<code class="js">$(<span class="js__operator">function</span>()<span class="js__brace">{</span> <br> $.get(<span class="js__string">"json.php"</span>,<span class="js__operator">function</span>(json)<span class="js__brace">{</span> <br> <br> ......<span class="js__sl_comment">//这里省略代码若干</span> <br> <br> <span class="js__statement">var</span> i=<span class="js__num">0</span>; <br> <span class="js__statement">for</span> (<span class="js__statement">var</span> state <span class="js__operator">in</span> china) <span class="js__brace">{</span> <br> china[state][<span class="js__string">'path'</span>].color = Raphael.getColor(<span class="js__num">0.9</span>); <br> (<span class="js__operator">function</span> (st, state) <span class="js__brace">{</span> <br> <span class="js__statement">var</span> prodata = data[i]; <br> <span class="js__statement">var</span> fillcolor = colors[arr[i]]; <br> st.attr(<span class="js__brace">{</span>fill:fillcolor<span class="js__brace">}</span>);<span class="js__sl_comment">//填充背景色</span> <br> xOffset = <span class="js__num">70</span>; <br> yOffset = <span class="js__num">180</span>; <br> st.hover(<span class="js__operator">function</span>(e)<span class="js__brace">{</span><span class="js__sl_comment">//鼠标滑向</span> <br> st.animate(<span class="js__brace">{</span>fill: <span class="js__string">"#fdd"</span>, stroke: <span class="js__string">"#eee"</span><span class="js__brace">}</span>, <span class="js__num">500</span>); <br> R.safari(); <br> $(<span class="js__string">"#tip"</span>).css(<span class="js__brace">{</span><span class="js__string">"top"</span>:(e.clientY-xOffset)+<span class="js__string">"px"</span>,<span class="js__string">"left"</span>:(e.clientX-yOffset)+<span class="js__string">"px"</span><span class="js__brace">}</span>).fadeIn(<span class="js__string">"fast"</span>) <br> .html(<span class="js__string">"<h4>"</span>+china[state][<span class="js__string">'name'</span>]+<span class="js__string">"</h4><p>活跃用户数:"</span>+prodata+<span class="js__string">"</p>"</span>); <br> <span class="js__brace">}</span>,<span class="js__operator">function</span>()<span class="js__brace">{</span><span class="js__sl_comment">//鼠标离开</span> <br> st.animate(<span class="js__brace">{</span>fill: fillcolor, stroke: <span class="js__string">"#eee"</span><span class="js__brace">}</span>, <span class="js__num">500</span>); <br> R.safari(); <br> $(<span class="js__string">"#tip"</span>).hide(); <br> <span class="js__brace">}</span>); <br> <br> st.mousemove(<span class="js__operator">function</span>(e)<span class="js__brace">{</span><span class="js__sl_comment">//鼠标移动</span> <br> $(<span class="js__string">"#tip"</span>).css(<span class="js__brace">{</span><span class="js__string">"top"</span>:(e.clientY-xOffset)+<span class="js__string">"px"</span>,<span class="js__string">"left"</span>:(e.clientX-yOffset)+<span class="js__string">"px"</span><span class="js__brace">}</span>); <br> R.safari(); <br> <span class="js__brace">}</span>); <br> <br> <span class="js__brace">}</span>)(china[state][<span class="js__string">'path'</span>], state); <br> i++; <br> <span class="js__brace">}</span> <br> <span class="js__brace">}</span>); <br><span class="js__brace">}</span>); <br></code>
以上代码可以看出,通过jQuery的hover()鼠标滑向省份区块时,调用弹出提示框,并将数据载入显示在提示框中,而值得关注的是,我们还需要加一个效果,就是鼠标在省份区块上移动mousemove()的时候,也应该调用提示框跟随鼠标一起移动,否则的话当鼠标在一个省份区块内滑动的话提示框位置不会变化,这样会影响体验效果,小小的改动可以提升用户体验。
最后,如果您需要定制提示框的效果的话,可以设置提示框的CSS样式,本例简单的CSS代码如下:
<code class="css"><span class="css__id">#tip</span>{<span class="css__property">position:</span><span class="css__value">absolute</span>; <span class="css__property">width:</span><span class="css__number">180px</span>; <span class="css__property">border:</span><span class="css__number">1px</span> <span class="css__value">solid</span> <span class="css__color">#d3d3d3</span>; <span class="css__property">background:</span><span class="css__color">#fff</span>;<span class="css__property">display:</span><span class="css__value">none</span>; <br>-moz-border-radius:<span class="css__number">5px</span>; -webkit-border-radius:<span class="css__number">5px</span>; <span class="css__property">overflow:</span><span class="css__value">hidden</span>; border-radius:<span class="css__number">5px</span>; <br>-moz-box-shadow:<span class="css__number">1px</span> <span class="css__number">1px</span> <span class="css__number">2px</span> rgba(<span class="css__number">0</span>,<span class="css__number">0</span>,<span class="css__number">0</span>,.<span class="css__number">2</span>); -webkit-box-shadow:<span class="css__number">1px</span> <span class="css__number">1px</span> <span class="css__number">2px</span> rgba(<span class="css__number">0</span>,<span class="css__number">0</span>,<span class="css__number">0</span>,.<span class="css__number">2</span>); <br>box-shadow:<span class="css__number">1px</span> <span class="css__number">1px</span> <span class="css__number">2px</span> rgba(<span class="css__number">0</span>,<span class="css__number">0</span>,<span class="css__number">0</span>,.<span class="css__number">2</span>);} <br><span class="css__id">#tip</span> <span class="css__element">h4</span>{<span class="css__property">height:</span><span class="css__number">28px</span>; <span class="css__property">line-height:</span><span class="css__number">28px</span>; <span class="css__property">padding-left:</span><span class="css__number">6px</span>; <span class="css__property">background:</span><span class="css__color">#f0f0f0</span>} <br><span class="css__id">#tip</span> <span class="css__element">p</span>{<span class="css__property">line-height:</span><span class="css__number">24px</span>; <span class="css__property">padding:</span><span class="css__number">2px</span> <span class="css__number">4px</span>} </code>