1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > IE6 7 8中兼容css3圆角问题

IE6 7 8中兼容css3圆角问题

时间:2019-12-27 21:57:34

相关推荐

IE6 7 8中兼容css3圆角问题

圆角效果使人感觉网页更加协调美观,ie9、Opera 10.5、Safari 5、Chrome 4和Firefox 4及其以上版本都添加了对css3的支持,这让我们领略到了css3的强悍。

传统的圆角生成方案,必须使用多张图片作为背景图案。css3的出现,使得我们再也不必浪费时间去制作这些图片了,而且还有其他多个优点:

1. 减少维护的工作量。图片文件的生成、更新、编写网页代码,这些工作都不再需要了。

2. 提高网页性能。由于不必再发出多余的HTTP请求,网页的载入速度将变快。

3. 增加视觉可靠性。某些情况下(网络拥堵、服务器出错、网速过慢等等),背景图片会下载失败,导致视觉效果不佳。CSS3就不会发生这种情况。

下载一个ie-css3.htc的文件

/download/sunshine_love/5107742

或者复制一下代码:

1 --Do not remove this if you are using-- 2 Original Author: Remiz Rahnas 3 Original Author URL: 4 Published date: /09/24 5 6 Changes by Nick Fetchak: 7 - IE8 standards mode compatibility 8 - VML elements now positioned behind original box rather than inside of it - should be less prone to breakage 9 - Added partial support for 'box-shadow' style 10 - Checks for VML support before doing anything 11 - Updates VML element size and position via timer and also via window resize event 12 - lots of other small things 13 Published date : /03/14 14 /ie-css3 15 16 Thanks to (//12/03/using-ies-filter-in-a-cross-browser-way) for enlightening me about the DropShadow filter 17 18 <public:attach event="ondocumentready" onevent="ondocumentready('v08vnSVo78t4JfjH')" /> 19 <script type="text/javascript"> 20 21 timer_length = 200; // Milliseconds 22 border_opacity = false; // Use opacity on borders of rounded-corner elements? Note: This causes antialiasing issues 23 24 25 // supportsVml() borrowed from /questions/654112/how-do-you-detect-support-for-vml-or-svg-in-a-browser 26 function supportsVml() { 27if (typeof supportsVml.supported == "undefined") { 28 var a = document.body.appendChild(document.createElement('div')); 29 a.innerHTML = '<v:shape id="vml_flag1" adj="1" />'; 30 var b = a.firstChild; 31 b.style.behavior = "url(#default#VML)"; 32 supportsVml.supported = b ? typeof b.adj == "object": true; 33 a.parentNode.removeChild(a); 34} 35return supportsVml.supported 36 } 37 38 39 // findPos() borrowed from /js/findpos.html 40 function findPos(obj) { 41var curleft = curtop = 0; 42 43if (obj.offsetParent) { 44 do { 45 curleft += obj.offsetLeft; 46 curtop += obj.offsetTop; 47 } while (obj = obj.offsetParent); 48} 49 50return({ 51 'x': curleft, 52 'y': curtop 53}); 54 } 55 56 function createBoxShadow(element, vml_parent) { 57var style = element.currentStyle['iecss3-box-shadow'] || element.currentStyle['-moz-box-shadow'] || element.currentStyle['-webkit-box-shadow'] || element.currentStyle['box-shadow'] || ''; 58var match = style.match(/^(\d+)px (\d+)px (\d+)px/); 59if (!match) { return(false); } 60 61 62var shadow = document.createElement('v:roundrect'); 63shadow.userAttrs = { 64 'x': parseInt(RegExp.$1 || 0), 65 'y': parseInt(RegExp.$2 || 0), 66 'radius': parseInt(RegExp.$3 || 0) / 2 67}; 68shadow.position_offset = { 69 'y': (0 - vml_parent.pos_ieCSS3.y - shadow.userAttrs.radius + shadow.userAttrs.y), 70 'x': (0 - vml_parent.pos_ieCSS3.x - shadow.userAttrs.radius + shadow.userAttrs.x) 71}; 72shadow.size_offset = { 73 'width': 0, 74 'height': 0 75}; 76shadow.arcsize = element.arcSize +'px'; 77shadow.style.display = 'block'; 78shadow.style.position = 'absolute'; 79shadow.style.top = (element.pos_ieCSS3.y + shadow.position_offset.y) +'px'; 80shadow.style.left = (element.pos_ieCSS3.x + shadow.position_offset.x) +'px'; 81shadow.style.width = element.offsetWidth +'px'; 82shadow.style.height = element.offsetHeight +'px'; 83shadow.style.antialias = true; 84shadow.className = 'vml_box_shadow'; 85shadow.style.zIndex = element.zIndex - 1; 86shadow.style.filter = 'progid:DXImageTransform.Microsoft.Blur(pixelRadius='+ shadow.userAttrs.radius +',makeShadow=true,shadowOpacity='+ element.opacity +')'; 87 88element.parentNode.appendChild(shadow); 89//element.parentNode.insertBefore(shadow, element.element); 90 91// For window resizing 92element.vml.push(shadow); 93 94return(true); 95 } 96 97 function createBorderRect(element, vml_parent) { 98if (isNaN(element.borderRadius)) { return(false); } 99 100element.style.background = 'transparent';101element.style.borderColor = 'transparent';102 103var rect = document.createElement('v:roundrect');104rect.position_offset = {105 'y': (0.5 * element.strokeWeight) - vml_parent.pos_ieCSS3.y,106 'x': (0.5 * element.strokeWeight) - vml_parent.pos_ieCSS3.x107};108rect.size_offset = {109 'width': 0 - element.strokeWeight,110 'height': 0 - element.strokeWeight111};112rect.arcsize = element.arcSize +'px';113rect.strokeColor = element.strokeColor;114rect.strokeWeight = element.strokeWeight +'px';115rect.stroked = element.stroked;116rect.className = 'vml_border_radius';117rect.style.display = 'block';118rect.style.position = 'absolute';119rect.style.top = (element.pos_ieCSS3.y + rect.position_offset.y) +'px';120rect.style.left = (element.pos_ieCSS3.x + rect.position_offset.x) +'px';121rect.style.width = (element.offsetWidth + rect.size_offset.width) +'px';122rect.style.height = (element.offsetHeight + rect.size_offset.height) +'px';123rect.style.antialias = true;124rect.style.zIndex = element.zIndex - 1;125 126if (border_opacity && (element.opacity < 1)) {127 rect.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(Opacity='+ parseFloat(element.opacity * 100) +')';128}129 130var fill = document.createElement('v:fill');131fill.color = element.fillColor;132fill.src = element.fillSrc;133fill.className = 'vml_border_radius_fill';134fill.type = 'tile';135fill.opacity = element.opacity;136 137// Hack: IE6 doesn't support transparent borders, use padding to offset original element138isIE6 = /msie|MSIE 6/.test(navigator.userAgent);139if (isIE6 && (element.strokeWeight > 0)) {140 element.style.borderStyle = 'none';141 element.style.paddingTop = parseInt(element.currentStyle.paddingTop || 0) + element.strokeWeight;142 element.style.paddingBottom = parseInt(element.currentStyle.paddingBottom || 0) + element.strokeWeight;143}144 145rect.appendChild(fill);146element.parentNode.appendChild(rect);147//element.parentNode.insertBefore(rect, element.element);148 149// For window resizing150element.vml.push(rect);151 152return(true);153 }154 155 function createTextShadow(element, vml_parent) {156if (!element.textShadow) { return(false); }157 158var match = element.textShadow.match(/^(\d+)px (\d+)px (\d+)px (#?\w+)/);159if (!match) { return(false); }160 161 162//var shadow = document.createElement('span');163var shadow = element.cloneNode(true);164var radius = parseInt(RegExp.$3 || 0);165shadow.userAttrs = {166 'x': parseInt(RegExp.$1 || 0) - (radius),167 'y': parseInt(RegExp.$2 || 0) - (radius),168 'radius': radius / 2,169 'color': (RegExp.$4 || '#000')170};171shadow.position_offset = {172 'y': (0 - vml_parent.pos_ieCSS3.y + shadow.userAttrs.y),173 'x': (0 - vml_parent.pos_ieCSS3.x + shadow.userAttrs.x)174};175shadow.size_offset = {176 'width': 0,177 'height': 0178};179shadow.style.color = shadow.userAttrs.color;180shadow.style.position = 'absolute';181shadow.style.top = (element.pos_ieCSS3.y + shadow.position_offset.y) +'px';182shadow.style.left = (element.pos_ieCSS3.x + shadow.position_offset.x) +'px';183shadow.style.antialias = true;184shadow.style.behavior = null;185shadow.className = 'ieCSS3_text_shadow';186shadow.innerHTML = element.innerHTML;187// For some reason it only looks right with opacity at 75%188shadow.style.filter = '\189 progid:DXImageTransform.Microsoft.Alpha(Opacity=75)\190 progid:DXImageTransform.Microsoft.Blur(pixelRadius='+ shadow.userAttrs.radius +',makeShadow=false,shadowOpacity=100)\191';192 193var clone = element.cloneNode(true);194clone.position_offset = {195 'y': (0 - vml_parent.pos_ieCSS3.y),196 'x': (0 - vml_parent.pos_ieCSS3.x)197};198clone.size_offset = {199 'width': 0,200 'height': 0201};202clone.style.behavior = null;203clone.style.position = 'absolute';204clone.style.top = (element.pos_ieCSS3.y + clone.position_offset.y) +'px';205clone.style.left = (element.pos_ieCSS3.x + clone.position_offset.x) +'px';206clone.className = 'ieCSS3_text_shadow';207 208 209element.parentNode.appendChild(shadow);210element.parentNode.appendChild(clone);211 212element.style.visibility = 'hidden';213 214// For window resizing215element.vml.push(clone);216element.vml.push(shadow);217 218return(true);219 }220 221 function ondocumentready(classID) {222if (!supportsVml()) { return(false); }223 224 if (this.className.match(classID)) { return(false); }225this.className = this.className.concat(' ', classID);226 227// Add a namespace for VML (IE8 requires it)228if (!document.namespaces.v) { document.namespaces.add("v", "urn:schemas-microsoft-com:vml"); }229 230// Check to see if we've run once before on this page231if (typeof(window.ieCSS3) == 'undefined') {232 // Create global ieCSS3 object233 window.ieCSS3 = {234 'vmlified_elements': new Array(),235 'update_timer': setInterval(updatePositionAndSize, timer_length)236 };237 238 if (typeof(window.onresize) == 'function') { window.ieCSS3.previous_onresize = window.onresize; }239 240 // Attach window resize event241 window.onresize = updatePositionAndSize;242}243 244 245// These attrs are for the script and have no meaning to the browser:246this.borderRadius = parseInt(this.currentStyle['iecss3-border-radius'] ||247 this.currentStyle['-moz-border-radius'] ||248 this.currentStyle['-webkit-border-radius'] ||249 this.currentStyle['border-radius'] ||250 this.currentStyle['-khtml-border-radius']);251this.arcSize = Math.min(this.borderRadius / Math.min(this.offsetWidth, this.offsetHeight), 1);252this.fillColor = this.currentStyle.backgroundColor;253this.fillSrc = this.currentStyle.backgroundImage.replace(/^url\("(.+)"\)$/, '$1');254this.strokeColor = this.currentStyle.borderColor;255this.strokeWeight = parseInt(this.currentStyle.borderWidth);256this.stroked = 'true';257if (isNaN(this.strokeWeight) || (this.strokeWeight == 0)) {258 this.strokeWeight = 0;259 this.strokeColor = fillColor;260 this.stroked = 'false';261}262this.opacity = parseFloat(this.currentStyle.opacity || 1);263this.textShadow = this.currentStyle['text-shadow'];264 265this.element.vml = new Array();266this.zIndex = parseInt(this.currentStyle.zIndex);267if (isNaN(this.zIndex)) { this.zIndex = 0; }268 269// Find which element provides position:relative for the target element (default to BODY)270vml_parent = this;271var limit = 100, i = 0;272do {273 vml_parent = vml_parent.parentElement;274 i++;275 if (i >= limit) { return(false); }276} while ((typeof(vml_parent) != 'undefined') && (vml_parent.currentStyle.position != 'relative') && (vml_parent.tagName != 'BODY'));277 278vml_parent.pos_ieCSS3 = findPos(vml_parent);279this.pos_ieCSS3 = findPos(this);280 281var rv1 = createBoxShadow(this, vml_parent);282var rv2 = createBorderRect(this, vml_parent);283var rv3 = createTextShadow(this, vml_parent);284if (rv1 || rv2 || rv3) { window.ieCSS3.vmlified_elements.push(this.element); }285 286if (typeof(vml_parent.document.ieCSS3_stylesheet) == 'undefined') {287 vml_parent.document.ieCSS3_stylesheet = vml_parent.document.createStyleSheet();288 vml_parent.document.ieCSS3_stylesheet.addRule("v\\:roundrect", "behavior: url(#default#VML)");289 vml_parent.document.ieCSS3_stylesheet.addRule("v\\:fill", "behavior: url(#default#VML)");290 // Compatibility with IE7.js291 vml_parent.document.ieCSS3_stylesheet.ie7 = true;292}293 }294 295 function updatePositionAndSize() {296if (typeof(window.ieCSS3.vmlified_elements) != 'object') { return(false); }297 298for (var i in window.ieCSS3.vmlified_elements) {299 var el = window.ieCSS3.vmlified_elements[i];300 301 if (typeof(el.vml) != 'object') { continue; }302 303 for (var z in el.vml) {304 //var parent_pos = findPos(el.vml[z].parentNode);305 var new_pos = findPos(el);306 new_pos.x = (new_pos.x + el.vml[z].position_offset.x) + 'px';307 new_pos.y = (new_pos.y + el.vml[z].position_offset.y) + 'px';308 if (el.vml[z].style.left != new_pos.x) { el.vml[z].style.left = new_pos.x; }309 if (el.vml[z].style.top != new_pos.y) { el.vml[z].style.top = new_pos.y; }310 311 var new_size = {312 'width': parseInt(el.offsetWidth + el.vml[z].size_offset.width),313 'height': parseInt(el.offsetHeight + el.vml[z].size_offset.height)314 }315 if (el.vml[z].offsetWidth != new_size.width) { el.vml[z].style.width = new_size.width +'px'; }316 if (el.vml[z].offsetHeight != new_size.height) { el.vml[z].style.height = new_size.height +'px'; }317 }318}319 320if (event && (event.type == 'resize') && typeof(window.ieCSS3.previous_onresize) == 'function') { window.ieCSS3.previous_onresize(); }321 }322 </script>

使用如下:

.banner{width: 980px;height: 472px;background-color: white;border-radius: 30px;behavior: url(./css/ie-css3.htc); //使用绝对路径 position:relative;z-index:2;}

说明:

Webkit内核的浏览器支持“-webkit-border-radius: 10px;”属性(10px是圆角半径),可以直接解析出圆角;Firefox浏览器支持“-moz-border-radius: 10px;”属性,也是可以直接解析出圆角;IE系浏览器则需要加上“border-radius: 15px;”的属性。注意:

1、behavior的url里一定要填写ie-css3.htc的绝对路径,因为 IE浏览器找该文件是相对当前html文件路径来找的,所以对于Wordpress等动态程序生成的页面一定要填写绝对路径。

2、一定要有定位属性:position:relative;

3、因为在IE浏览器下这些CSS3效果的实现是要借助于VML,由VML绘制圆角或是投影效果,所以还需要一个z-index属性。z-index属性最好设置得比较大,如2。

4、如果在IE浏览器下某些模块无法用此渲染,可以试着绝对定位相应的层,即加上“ width: 400px; height:400px;”属性。

5、radius属性的10px是圆角半径,还可以给两个值如“border-radius: 10px 5px;”,这样则左上角与右下角半径为10px,右上角与左下角半径为5px。也可以赋4个值,为“上 右 下 左”。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。