【发布时间】:2016-01-22 13:42:50
【问题描述】:
我正在考虑制作一个带有悬停的图像地图,显示在同一 loc_id 内发生的总次数。
getOccCount.php
<?php
$query = "SELECT COUNT(occurrence_id) FROM major_occurrence GROUP BY location_id";
$result = mysqli_query($link, $query) or die(mysqli_error($link));
$array = mysql_fetch_row($result); //fetch result
echo json_encode($array);
?>
每当我将鼠标悬停到图像地图上的另一个位置时,悬停应该会改变结果,因为它具有不同的位置 ID。
<style type="text/css">
#map {
margin: 0;
padding: 0;
width: 950px;
height: 1211px;
background: url(images/Campus-Map.jpg);
background-size: 950px 1211px;
font-family: arial, helvetica, sans-serif;
font-size: 8pt;
}
#map li {
margin: 0;
padding: 0;
list-style: none;
}
#map li a {
position: absolute;
display: block;
/*
Specifying a background image
(a 1px by 1px transparent gif)
fixes a bug in older versions of
IE that causeses the block to not
render at its full dimensions.
*/
background: url(blank.gif);
text-decoration: none;
color: #000;
}
#map li a span {
display: none;
}
#map li a:hover span {
position: relative;
display: block;
width: 200px;
left: 20px;
top: 20px;
border: 1px solid #000;
background: #fff;
padding: 5px;
filter: alpha(opacity=80);
opacity: 0.8;
}
#map a.rpc {
top: 1060px;
left: 585px;
width: 78px;
height: 65px;
}
</style>
<script language="javascript" type="text/javascript">
$('#map>span').hover(function () {
$.ajax({
url: 'getOccCount.php',
data: "",
dataType: 'json',
method: GET,
success: function (data) {
var location_id = $(this).attr("location_id");
$.get("getOccCount.php", {location_id: location_id}, function (result) {
var result = data[0];
$('#map>span').html('Total Number of Occurrence: ' + result);
}
}
});
});
</script>
<body>
<html>
<ul id="map">
<li><a class="rpc" href="doRPMap.php?locID=1"><span><b>RPC</b></span></a></li>
<ul>
</body>
</html>
进行必要的更改后,它仍然不适合我。请给我一些关于这个问题的建议。谢谢。
【问题讨论】:
-
在每个悬停的元素上请求服务器肯定不行...
-
确切地说,我只需要将鼠标光标移动到元素上,然后一次又一次地移出,然后……用 AJAX 请求淹没您的服务器。如果您无法预先获取数据,那么您至少应该实现某种形式的缓存,以便下一次将鼠标悬停在同一元素上时不需要再次发出请求。
标签: javascript jquery html hover imagemap