[jQuery]イメージマップでTooltip表示するサンプル





jQuery1.2.6


jQuery


pageX/Y  0

clientX/Y  0



<div id="imgmap">
<img src="tags.png" alt="Tags" width="400" height="151" border="0" usemap="#Map" />
<map name="Map" id="Map"><area shape="rect" coords="33,45,101,83" href="#sea" alt="Sea" />
<area shape="rect" coords="146,23,279,67" href="#beach" alt="Beach" />
<area shape="rect" coords="94,100,205,136" href="#flower" alt="Flower" />
<area shape="rect" coords="232,83,294,106" href="#cloud" alt="Cloud" />
<area shape="rect" coords="311,58,364,95" href="#sky" alt="Sky" />
</map></div>
<p id="sea" class="detail"><img src="sea.jpg" alt="Sea" width="300" height="240" /></p>
<p id="beach" class="detail"><img src="beach.jpg" alt="Beach" width="300" height="240" /></p>
<div id="flower" class="detail"><img src="flower.jpg" alt="Flower" width="300" height="240" />
<p>テキストも入ります</p></div>
<p id="cloud" class="detail"><img src="cloud.jpg" alt="Cloud" width="240" height="300" /></p>
<p id="sky" class="detail"><img src="sky.jpg" alt="Sky" width="300" height="240" /></p>

調
$('#Map area')
$(function (){
 if($('#imgmap')){
  
  $('#Map area').each(function(i){
  var name = $(this).attr('href');
   $(name).css({
    "display":"none",
    "position": "absolute",
    "zIndex": "9999",
    "display": "none"
   });
   $(this).mouseover(
    function(e){ 
      var bodyX =$(window).width();
      var bodyY =$(window).height();
      var Y = ((e.clientY-$(name).height()) -15);
      var X = e.clientX;
      
      if(Y < 0){
        Y = e.clientY +15;
      }
      var eqY = Y+$(name).height();
      var eqX = X+$(name).width();
      if( eqY > bodyY){
      Y = e.clientY+(bodyY-eqY);
      }
      if( eqX > bodyX){
       eX = bodyX-X;
       X = bodyX-(eX+$(name).width())-20;
      }else{
       X=X+15;
      }
     $(name).css({ "display":"block","left":X+"px","top":Y+"px"});
    });
   $(this).mouseout(
    function(){ 
    
     $(name).css({ "display":"none"});
   });
  });
 }
});
 

jQuery-Imagemap Tooltip  
WTN-tooltip_jq.zip  2668   147.93 KB
areahrefID
ID
hrefID

areaIDjQuery
areahref #ID_img 
<div id="imgmap">
<img src="tags.png" alt="Tags" width="400" height="151" border="0" usemap="#Map" />
<map name="Map" id="Map"><area shape="rect" coords="33,45,101,83" href="#sea_img" alt="Sea" id="sea" />
<area shape="rect" coords="146,23,279,67" href="#beach_img" alt="Beach" id="beach" />
<area shape="rect" coords="94,100,205,136" href="#flower_img" alt="Flower" id="flower" />
<area shape="rect" coords="232,83,294,106" href="#cloud_img" alt="Cloud" id="cloud" />
<area shape="rect" coords="311,58,364,95" href="#sky_img" alt="Sky" id="sky" />
</map></div>
if($('#Map').is(':visible')){
	
	$('#Map area').each(function(i){
								 
	var myName = $(this).attr('href');
	var myAlt = $(this).attr('alt');
	var myID = $(this).attr('id');
	var myClass = $(this).attr("class");
	
	if(myClass)
		var mySRC = myClass;
	else 
		var mySRC = myID;
	
		$(this).mouseover(
			function(e){
			  var poY = e.pageY;
			  var poX = e.pageX;
			  var bodyX =$('body').width();
			  var bodyY =$('body').height();
			  
		var pict = $("<img></img>").attr({ 
			  "src": 'images/machine/'+mySRC+'.jpg',
			  "width":180,
			  "title": myAlt,
			  "alt": myAlt
			  
		});
		var datDIV = $("<div id='"+myID+"_img'></div>").html('<br />'+myAlt);
		
		datDIV.prepend(pict);
		datDIV.css({
			"border":"solid 1px #000",
			"background":"#fff",
			"font-size":"93%",
			"text-align":"center",
			"line-height":"1.5",
			"display":"none"
		});
		$('body').append(datDIV);

		var eHeight = $(myName).height();
		var eWidth = $(myName).width();
		
			  if((poY - eHeight)<0){
				  var Y = parseInt((poY-eHeight));
			  }else{
				  var Y = poY - eHeight - 15;
			  }
			 if((poX + eWidth) > bodyX){
				 var X =poX-eWidth;
			 }else{
				 var X = poX +15;
			 }
			
			datDIV.css({ "position":'absolute',"display":"block","left":X+"px","top":Y+"px"});
			
			});
		$(this).mouseout(
			function(){ 
			
				$(myName).remove();
		});
	
	});
}

コメントを残す

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください