1. 程式人生 > >js特效 圖片展示放大、說明

js特效 圖片展示放大、說明

Js程式碼部分:

var dom = (document.getElementById) ? true : false;

var ns5 = ((navigator.userAgent.indexOf("Gecko")>-1) && dom) ? true: false;

var ie5 = ((navigator.userAgent.indexOf("MSIE")>-1) && dom) ? true : false;

var ns4 = (document.layers && !dom) ? true : false;

var ie4 = (document.all && !dom) ? true : false;

var nodyn = (!ns5 && !ns4 && !ie4 && !ie5) ? true : false;

// resize fix for ns4

var origWidth, origHeight;

if (ns4) {

origWidth = window.innerWidth; origHeight = window.innerHeight;

window.onresize = function() { if (window.innerWidth != origWidth || window.innerHeight != origHeight) history.go(0); }

}

// avoid error of passing event object in older browsers

if (nodyn) { event = "nope" }

///////////////////////  CUSTOMIZE HERE   ////////////////////

// settings for tooltip

// Do you want tip to move when mouse moves over link?

var tipFollowMouse= true;

var tipWidth= 352;

var offX= 45;    // how far from mouse to show tip

var offY= -45;

var tipFontFamily= "Verdana, arial, helvetica, sans-serif";

var tipFontSize= "8pt";

var tipFontColor= "#000000";

var tipBgColor= "#DDECFF";

var origBgColor= tipBgColor; // in case no bgColor set in array

var tipBorderColor= "#cccccc";

var tipBorderWidth= 0;

var tipBorderStyle= "ridge";

var tipPadding= 0;

// preload images that are to appear in tooltip, (width,height)

var img1 = new Image(106,131);    img1.src = "images/001.jpg";

var img2 = new Image(90,45);                  img2.src = "images/001.jpg";

// tooltip content goes here

var messages = new Array();

// multi-dimensional arrays containing

// image, text, bgColor to be sent to tooltip

messages[0] = new Array('images/001.jpg','廣場拍攝',"#FFFFFF");

messages[1] = new Array('images/002.jpg','西區走廊',"#FFFFFF");

messages[2] = new Array('images/003.jpg','這個會館應該是中上產階級用的',"#FFFFFF");

messages[3] = new Array('images/004.jpg','天通苑藝術教學氛圍很濃',"#FFFFFF");

messages[4] = new Array('images/005.jpg','石材的房子,恐怕價格不低',"#FFFFFF");

messages[5] = new Array('images/006.jpg','中心公園',"#FFFFFF");

messages[6] = new Array('images/007.jpg','龍德廣場北京第二大商場了',"#FFFFFF");

messages[7] = new Array('images/008.jpg','現代雕塑,看不懂,關於親情演繹的吧',"#FFFFFF");

messages[8] = new Array('images/009.jpg','天通大廈',"#FFFFFF");

messages[9] = new Array('images/010.jpg','天驕幼兒園',"#FFFFFF");

messages[10] = new Array('images/011.jpg','從東區看中苑的樓',"#FFFFFF");

messages[11] = new Array('images/012.jpg','很和諧',"#FFFFFF");

messages[12] = new Array('images/013.jpg','也是小編超喜歡的樓,很有氣質',"#FFFFFF");

messages[13] = new Array('images/014.jpg','中心公園',"#FFFFFF");

messages[14] = new Array('images/015.jpg','西苑的小區內部',"#FFFFFF");

messages[15] = new Array('images/016.jpg','西苑實景',"#FFFFFF");

messages[16] = new Array('images/017.jpg','東區商業街',"#FFFFFF");

messages[17] = new Array('images/018.jpg','這個房子感覺好,天通東苑的',"#FFFFFF");

messages[18] = new Array('images/019.jpg','街心花園',"#FFFFFF");

messages[19] = new Array('images/020.jpg','天通中苑',"#FFFFFF");

messages[20] = new Array('images/021.jpg','遠望東區',"#FFFFFF");

////////////////////  END OF CUSTOMIZATION AREA  ///////////////////

// to layout image and text, 2-row table, image centered in top cell

// these go in var tip in doTooltip function

// startStr goes before image, midStr goes between image and text

var startStr = '<table height="272" background="pic-bg.gif" width="' + tipWidth + '"><tr><td style="margin-top:5px" align="center" width="100%" valign="middle"><img src="';

var midStr = '" border="0"></td></tr><tr><td style="margin-top:5px" align="center">';

var endStr = '</td></tr></table>';

////////////////////////////////////////////////////////////

//  initTip        - initialization for tooltip.

//               Global variables for tooltip.

//               Set styles for all but ns4.

//               Set up mousemove capture if tipFollowMouse set true.

////////////////////////////////////////////////////////////

var tooltip, tipcss;

function initTip() {

if (nodyn) return;

tooltip = (ns4)? document.tipDiv.document: (ie4)? document.all['tipDiv']: (ie5||ns5)? document.getElementById('tipDiv'): null;

tipcss = (ns4)? document.tipDiv: tooltip.style;

if (ie4||ie5||ns5) {   // ns4 would lose all this on rewrites

tipcss.width = tipWidth+"px";

tipcss.fontFamily = tipFontFamily;

tipcss.fontSize = tipFontSize;

tipcss.color = tipFontColor;

tipcss.backgroundColor = tipBgColor;

tipcss.borderColor = tipBorderColor;

tipcss.borderWidth = tipBorderWidth+"px";

tipcss.padding = tipPadding+"px";

tipcss.borderStyle = tipBorderStyle;

}

if (tooltip&&tipFollowMouse) {

if (ns4) document.captureEvents(Event.MOUSEMOVE);

document.onmousemove = trackMouse;

}

}

window.onload = initTip;

/////////////////////////////////////////////////

//  doTooltip function

//                         Assembles content for tooltip and writes

//                         it to tipDiv

/////////////////////////////////////////////////

var t1,t2; // for setTimeouts

var tipOn = false;     // check if over tooltip link

function doTooltip(evt,num) {

if (!tooltip) return;

if (t1) clearTimeout(t1);   if (t2) clearTimeout(t2);

tipOn = true;

if (ns4) {

if (messages[num][2])      tipBgColor = messages[num][2];

else tipBgColor = origBgColor;

var tip = '<table background="pic-bg.gif" bgcolor="' + tipBorderColor + '" width="' + tipWidth + '" cellspacing="0" cellpadding="' + tipBorderWidth + '" border="0"><tr><td ><table bgcolor="' + tipBgColor + '" width="100%" cellspacing="0" cellpadding="' + tipPadding + '" border="0"><tr><td>'+ startStr + messages[num][0] + midStr + '<span style=" text-align:center;font-family:' + tipFontFamily + '; font-size:' + tipFontSize + '; color:' + tipFontColor + ';">' + messages[num][1] + '</span>' + endStr + '</td></tr></table></td></tr></table>';

tooltip.write(tip);

tooltip.close();

} else if (ie4||ie5||ns5) {

var tip = startStr + messages[num][0] + midStr + '<span style=" text-align:center;font-family:' + tipFontFamily + '; font-size:' + tipFontSize + '; color:' + tipFontColor + ';">' + messages[num][1] + '</span>' + endStr;

tipcss.backgroundColor = messages[num][2];

tooltip.innerHTML = tip;

}

if (!tipFollowMouse) positionTip(evt);

else t1=setTimeout("tipcss.visibility='visible'",100);

}

var mouseX, mouseY;

function trackMouse(evt) {

mouseX = (ns4||ns5)? evt.pageX: window.event.clientX + document.body.scrollLeft;

mouseY = (ns4||ns5)? evt.pageY: window.event.clientY + document.body.scrollTop;

if (tipOn) positionTip(evt);

}

/////////////////////////////////////////////////////////////

//  positionTip function

//               If tipFollowMouse set false, so trackMouse function

//               not being used, get position of mouseover event.

//               Calculations use mouseover event position,

//               offset amounts and tooltip width to position

//               tooltip within window.

/////////////////////////////////////////////////////////////

function positionTip(evt) {

if (!tipFollowMouse) {

mouseX = (ns4||ns5)? evt.pageX: window.event.clientX + document.body.scrollLeft;

mouseY = (ns4||ns5)? evt.pageY: window.event.clientY + document.body.scrollTop;

}

// tooltip width and height

var tpWd = (ns4)? tooltip.width: (ie4||ie5)? tooltip.clientWidth: tooltip.offsetWidth;

var tpHt = (ns4)? tooltip.height: (ie4||ie5)? tooltip.clientHeight: tooltip.offsetHeight;

// document area in view (subtract scrollbar width for ns)

var winWd = (ns4||ns5)? window.innerWidth-100+window.pageXOffset: document.body.clientWidth+document.body.scrollLeft;

var winHt = (ns4||ns5)? window.innerHeight+window.pageYOffset: document.body.clientHeight+document.body.scrollTop;

// check mouse position against tip and window dimensions

// and position the tooltip

if ((mouseX+offX+tpWd)>winWd)

tipcss.left = (ns4)? mouseX-(tpWd+offX): mouseX-(tpWd+offX)+"px";

else tipcss.left = (ns4)? mouseX+offX: mouseX+offX+"px";

if ((mouseY+offY+tpHt)>winHt)

tipcss.top = (ns4)? mouseY-(tpHt+offY): mouseY-(tpHt+offY)+"px";

else tipcss.top = (ns4)? mouseY+offY: mouseY+offY+"px";

if (!tipFollowMouse) t1=setTimeout("tipcss.visibility='visible'",100);

}

function hideTip() {

if (!tooltip) return;

//      tipcss.visibility = "hidden";

t2=setTimeout("tipcss.visibility='hidden'",100);

tipOn = false;

}

Html頁面引用的方式:

由於時間問題這裡直接貼出引用部分,放到body裡面就可以除錯。

<table width="100%" border="0" cellpadding="5" cellspacing="5" class="tbpic2">

<tr bordercolor="#CCCCCC" bgcolor="#D0E3A1">

<td><a href="#" onmouseover="doTooltip(event,14)" onmouseout="hideTip()"><img src="kftp/tiantong-images/015.jpg" width="110" height="80" border="0" /></a></td>

<td><a href="#" onmouseover="doTooltip(event,15)" onmouseout="hideTip()"><img src="kftp/tiantong-images/016.jpg" width="110" height="80" border="0" /></a></td>

<td><a href="#" onmouseover="doTooltip(event,16)" onmouseout="hideTip()"><img src="kftp/tiantong-images/017.jpg" width="110" height="80" border="0" /></a></td>

<td><a href="#" onmouseover="doTooltip(event,17)" onmouseout="hideTip()"><img src="kftp/tiantong-images/018.jpg" width="110" height="80" border="0" /></a></td>

<td><a href="#" onmouseover="doTooltip(event,18)" onmouseout="hideTip()"><img src="kftp/tiantong-images/019.jpg" width="110" height="80" border="0"></a></td>

<td><a href="#" onmouseover="doTooltip(event,19)" onmouseout="hideTip()"><img src="kftp/tiantong-images/020.jpg" width="110" height="80" border="0"></a></td>

<td><a href="#" onmouseover="doTooltip(event,20)" onmouseout="hideTip()"><img src="kftp/tiantong-images/021.jpg" width="110" height="80" border="0"/></a></td>

</tr>

<tr bordercolor="#CCCCCC" bgcolor="#D0E3A1">

<td><a href="#" onmouseover="doTooltip(event,7)" onmouseout="hideTip()"><img src="kftp/tiantong-images/008.jpg" width="110" height="80" border="0"></a></td>

<td><a href="#" onmouseover="doTooltip(event,8)" onmouseout="hideTip()"><img src="kftp/tiantong-images/009.jpg" width="110" height="80" border="0"></a></td>

<td><a href="#" onmouseover="doTooltip(event,9)" onmouseout="hideTip()"><img src="kftp/tiantong-images/010.jpg" width="110" height="80" border="0" /></a></td>

<td><a href="#" onmouseover="doTooltip(event,10)" onmouseout="hideTip()"><img src="kftp/tiantong-images/011.jpg" width="110" height="80" border="0" /></a></a></td>

<td><a href="#" onmouseover="doTooltip(event,11)" onmouseout="hideTip()"><img src="kftp/tiantong-images/012.jpg" width="110" height="80" border="0"/></a></td>

<td><a href="#" onmouseover="doTooltip(event,12)" onmouseout="hideTip()"><img src="kftp/tiantong-images/013.jpg" width="110" height="80" border="0"/></a></td>

<td><a href="#" onmouseover="doTooltip(event,13)" onmouseout="hideTip()"><img src="kftp/tiantong-images/014.jpg" width="110" height="80" border="0"/></a></td>

</tr>

<tr bordercolor="#CCCCCC" bgcolor="#D0E3A1">

<td><a href="#" onmouseover="doTooltip(event,0)" onmouseout="hideTip()"><img src="kftp/tiantong-images/001.jpg" width="110" height="80" border="0"></a></td>

<td><a href="#" onmouseover="doTooltip(event,1)" onmouseout="hideTip()"><img src="kftp/tiantong-images/002.jpg" width="110" height="80" border="0" /></a></td>

<td><a href="#" onmouseover="doTooltip(event,2)" onmouseout="hideTip()"><img src="kftp/tiantong-images/003.jpg" width="110" height="80" border="0" /></a></td>

<td><a href="#" onmouseover="doTooltip(event,3)" onmouseout="hideTip()"><img src="kftp/tiantong-images/004.jpg" width="110" height="80" border="0" /></a></td>

<td><a href="#" onmouseover="doTooltip(event,4)" onmouseout="hideTip()"><img src="kftp/tiantong-images/005.jpg" width="110" height="80" border="0" /></a></td>

<td><a href="#" onmouseover="doTooltip(event,5)" onmouseout="hideTip()"><img src="kftp/tiantong-images/006.jpg" width="110" height="80" border="0" /></a></td>

<td><a href="#" onmouseover="doTooltip(event,6)" onmouseout="hideTip()"><img src="kftp/tiantong-images/007.jpg" width="110" height="80" border="0" /></a>

<div id="tipDiv" style="position:absolute; visibility:hidden; z-index:100; left: 63px; top: 97px;"></div></td>

</tr>

</table>