最近在项目中遇到了一些与锚点相关的问题,由此引出hasLayout的一系列概念和方法,在此记录下来与大家一起分享。
一、锚点的相关问题
1.html map定位的名字问题。
<div id="top"> <img src="img/20120417/top.jpg" alt="80后游戏" width="950" height="710" border="0" usemap="#map1" /> <map id="map1" name="map1"> <area coords="222,176,19" shape="circle" href="#bk01" title="超级玛丽"></area> <area coords="335,185,19" shape="circle" href="#bk02" title="三国志"></area> <area coords="443,163,19" shape="circle" href="#bk03" title="魂斗罗"></area> </map> </div>
2.锚点的hasLayout问题。
.jumpAnchor{ width:0; display:block; }
.jumpAnchor{ height:0; display:inline-block; }
三、IE6下PNG24/PNG32不透明的解决方法
说完了锚点和hasLayout,还有一个常见的问题。我们知道在所有浏览器下都可以支持透明的png8图片,但是有时候是出于清晰度的要求,有时候是为了去除杂边(当然,杂边的消除方式,可以采用ps中添加与背景色一致的杂色而达到目的,不过某些情况下这好像不太现实,比如背景色不是纯色的时候),我们不得不选用位数更高的透明图片,常用的png-24和png-32的。
.toTop a{ height:165px; width:216px; background:url("../img/20120417/gototop.png") no-repeat scroll 0 0 transparent; position:fixed; top:70%; right:1%; _position:absolute; _top:expression(documentElement.scrollTop + 350 + "px"); cursor:pointer; opacity:0.8; _background: none; /*针对IE6*/ _filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=noscale, src="img/20120417/gototop.png");/*针对IE6*/ /*可以省略部分属性为_filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='img/20120417/gototop.png'); */ }
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>IE6下背景不透明解决办法</title> <style type="text/css"> .fixpng img { azimuth: expression( this.pngSet?this.pngSet=true:(this.nodeName == "IMG" && this.src.toLowerCase().indexOf('.png')>-1?(this.runtimeStyle.backgroundImage = "none", this.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + this.src + "', sizingMethod='image')", this.src = "transparent.gif"):(this.origBg = this.origBg? this.origBg :this.currentStyle.backgroundImage.toString().replace('url("','').replace('")',''), this.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + this.origBg + "', sizingMethod='crop')", this.runtimeStyle.backgroundImage = "none")),this.pngSet=true); } </style> </head> <body> <div class="fixpng"> <img src="1.png" width="10" height="10" border="0"/> <img src="2.png" width="20" height="20" border="0" /> <img src="3.png" width="30" height="30" border="0" /> </div> </body> </html>
<!--[if IE 6]> <script type="text/javascript" src="js/DD_belatedPNG_0.0.8a-min.js" ></script> <script type="text/javascript"> DD_belatedPNG.fix('#define,background'); </script> <![endif]-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>IE6下png背景不透明解决办法</title> <script language="JavaScript"> function correctPNG() // correctly handle PNG transparency in Win IE 5.5 & 6. { var arVersion = navigator.appVersion.split("MSIE") var version = parseFloat(arVersion[1]) if ((version >= 5.5) && (document.body.filters)) { for(var j=0; j<document.images.length; j++) { var img = document.images[j] var imgName = img.src.toUpperCase() if (imgName.substring(imgName.length-3, imgName.length) == "PNG") { var imgID = (img.id) ? "id='" + img.id + "' " : "" var imgClass = (img.className) ? "class='" + img.className + "' " : "" var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' " var imgStyle = "display:inline-block;" + img.style.cssText if (img.align == "left") imgStyle = "float:left;" + imgStyle if (img.align == "right") imgStyle = "float:right;" + imgStyle if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle var strNewHTML = "<span " + imgID + imgClass + imgTitle + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";" + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader" + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" img.outerHTML = strNewHTML j = j-1 } } } } window.attachEvent("onload", correctPNG); </script> <style type="text/css"> <!-- body { background-color: #9999CC; } --> </style> </head> <body> <img src="1.png" width="10" height="10" border="0"/> <img src="2.png" width="20" height="20" border="0" /> <img src="3.png" width="30" height="30" border="0" /> </body> </html>
<script type="text/javascript"> // 修复 IE 下 PNG 图片不能透明显示的问题 function fixPNG(myImage) { var arVersion = navigator.appVersion.split("MSIE"); var version = parseFloat(arVersion[1]); if ((version >= 5.5) && (version < 7) && (document.body.filters)) { var imgID = (myImage.id) ? "id='" + myImage.id + "' " : ""; var imgClass = (myImage.className) ? "class='" + myImage.className + "' " : ""; var imgTitle = (myImage.title) ? "title='" + myImage.title + "' " : "title='" + myImage.alt + "' "; var imgStyle = "display:inline-block;" + myImage.style.cssText; var strNewHTML = "<span " + imgID + imgClass + imgTitle + " style=\"" + "width:" + myImage.width + "px; height:" + myImage.height + "px;" + imgStyle + ";" + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader" + "(src=\'" + myImage.src + "\', sizingMethod='scale');\"></span>"; myImage.outerHTML = strNewHTML; } } window.onload=function(){ document.getElementById("top").style.height=screen.height/5+"px"; }// </script>