`
ManGo.0317
  • 浏览: 21767 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

【转载+整理】IE-CSS3 – 让 IE6,7,8 支持CSS3中的圆角、阴影

    博客分类:
  • WEB
阅读更多

大家知道IE 6,7,8不支持CSS3中新加属性,老外写了一个htc,可以让IE 6,7,8模拟部分的CSS3属性,包括:border-radius(圆角),box-shadow(阴影),text-shadow(文本阴影)

 

IE-CSS3 是个能令 Internet Explorer 不完整地支持几个常用的 CSS3 属性的小 script。它利用了 Internet Explorer 专有的 Vector Markup Language (VML) 来绘制所需效果。

使用方法很简单,如果你需要圆角效果,只需一如以往地用 border-radius,再加上 behavior: url(ie-css3.htc) 即可:

 

 

.box {
    -moz-border-radius: 15px; /* Firefox */
    -webkit-border-radius: 15px; /* Safari and Chrome */
    border-radius: 15px; /* Opera 10.5+, future browsers, and now also Internet Explorer 6+ using IE-CSS3 */

    -moz-box-shadow: 10px 10px 20px #000; /* Firefox */
    -webkit-box-shadow: 10px 10px 20px #000; /* Safari and Chrome */
    box-shadow: 10px 10px 20px #000; /* Opera 10.5+, future browsers and IE6+ using IE-CSS3 */

    behavior: url(ie-css3.htc); /* 可以让IE 6,7,8模拟部分的CSS3属性 */
}

 

ie-css3.htc 代码:

 

--Do not remove this if you are using--
Original Author: Remiz Rahnas
Original Author URL: http://www.htmlremix.com
Published date: 2008/09/24

Changes by Nick Fetchak:
- IE8 standards mode compatibility
- VML elements now positioned behind original box rather than inside of it - should be less prone to breakage
- Added partial support for 'box-shadow' style
- Checks for VML support before doing anything
- Updates VML element size and position via timer and also via window resize event
- lots of other small things
Published date : 2010/03/14
http://fetchak.com/ie-css3

Thanks to TheBrightLines.com (http://www.thebrightlines.com/2009/12/03/using-ies-filter-in-a-cross-browser-way) for enlightening me about the DropShadow filter

<public:attach event="ondocumentready" onevent="ondocumentready('v08vnSVo78t4JfjH')" />
<script type="text/javascript">

timer_length = 200; // Milliseconds
border_opacity = false; // Use opacity on borders of rounded-corner elements? Note: This causes antialiasing issues


// supportsVml() borrowed from http://stackoverflow.com/questions/654112/how-do-you-detect-support-for-vml-or-svg-in-a-browser
function supportsVml() {
	if (typeof supportsVml.supported == "undefined") {
		var a = document.body.appendChild(document.createElement('div'));
		a.innerHTML = '<v:shape id="vml_flag1" adj="1" />';
		var b = a.firstChild;
		b.style.behavior = "url(#default#VML)";
		supportsVml.supported = b ? typeof b.adj == "object": true;
		a.parentNode.removeChild(a);
	}
	return supportsVml.supported
}


// findPos() borrowed from http://www.quirksmode.org/js/findpos.html
function findPos(obj) {
	var curleft = curtop = 0;

	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}

	return({
		'x': curleft,
		'y': curtop
	});
}

function createBoxShadow(element, vml_parent) {
	var style = element.currentStyle['iecss3-box-shadow'] || element.currentStyle['-moz-box-shadow'] || element.currentStyle['-webkit-box-shadow'] || element.currentStyle['box-shadow'] || '';
	var match = style.match(/^(\d+)px (\d+)px (\d+)px/);
	if (!match) { return(false); }


	var shadow = document.createElement('v:roundrect');
	shadow.userAttrs = {
		'x': parseInt(RegExp.$1 || 0),
		'y': parseInt(RegExp.$2 || 0),
		'radius': parseInt(RegExp.$3 || 0) / 2
	};
	shadow.position_offset = {
		'y': (0 - vml_parent.pos_ieCSS3.y - shadow.userAttrs.radius + shadow.userAttrs.y),
		'x': (0 - vml_parent.pos_ieCSS3.x - shadow.userAttrs.radius + shadow.userAttrs.x)
	};
	shadow.size_offset = {
		'width': 0,
		'height': 0
	};
	shadow.arcsize = element.arcSize +'px';
	shadow.style.display = 'block';
	shadow.style.position = 'absolute';
	shadow.style.top = (element.pos_ieCSS3.y + shadow.position_offset.y) +'px';
	shadow.style.left = (element.pos_ieCSS3.x + shadow.position_offset.x) +'px';
	shadow.style.width = element.offsetWidth +'px';
	shadow.style.height = element.offsetHeight +'px';
	shadow.style.antialias = true;
	shadow.className = 'vml_box_shadow';
	shadow.style.zIndex = element.zIndex - 1;
	shadow.style.filter = 'progid:DXImageTransform.Microsoft.Blur(pixelRadius='+ shadow.userAttrs.radius +',makeShadow=true,shadowOpacity='+ element.opacity +')';

	element.parentNode.appendChild(shadow);
	//element.parentNode.insertBefore(shadow, element.element);

	// For window resizing
	element.vml.push(shadow);

	return(true);
}

function createBorderRect(element, vml_parent) {
	if (isNaN(element.borderRadius)) { return(false); }

	element.style.background = 'transparent';
	element.style.borderColor = 'transparent';

	var rect = document.createElement('v:roundrect');
	rect.position_offset = {
		'y': (0.5 * element.strokeWeight) - vml_parent.pos_ieCSS3.y,
		'x': (0.5 * element.strokeWeight) - vml_parent.pos_ieCSS3.x
	};
	rect.size_offset = {
		'width': 0 - element.strokeWeight,
		'height': 0 - element.strokeWeight
	};
	rect.arcsize = element.arcSize +'px';
	rect.strokeColor = element.strokeColor;
	rect.strokeWeight = element.strokeWeight +'px';
	rect.stroked = element.stroked;
	rect.className = 'vml_border_radius';
	rect.style.display = 'block';
	rect.style.position = 'absolute';
	rect.style.top = (element.pos_ieCSS3.y + rect.position_offset.y) +'px';
	rect.style.left = (element.pos_ieCSS3.x + rect.position_offset.x) +'px';
	rect.style.width = (element.offsetWidth + rect.size_offset.width) +'px';
	rect.style.height = (element.offsetHeight + rect.size_offset.height) +'px';
	rect.style.antialias = true;
	rect.style.zIndex = element.zIndex - 1;

	if (border_opacity && (element.opacity < 1)) {
		rect.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(Opacity='+ parseFloat(element.opacity * 100) +')';
	}

	var fill = document.createElement('v:fill');
	fill.color = element.fillColor;
	fill.src = element.fillSrc;
	fill.className = 'vml_border_radius_fill';
	fill.type = 'tile';
	fill.opacity = element.opacity;

	// Hack: IE6 doesn't support transparent borders, use padding to offset original element
	isIE6 = /msie|MSIE 6/.test(navigator.userAgent);
	if (isIE6 && (element.strokeWeight > 0)) {
		element.style.borderStyle = 'none';
		element.style.paddingTop = parseInt(element.currentStyle.paddingTop || 0) + element.strokeWeight;
		element.style.paddingBottom = parseInt(element.currentStyle.paddingBottom || 0) + element.strokeWeight;
	}

	rect.appendChild(fill);
	element.parentNode.appendChild(rect);
	//element.parentNode.insertBefore(rect, element.element);

	// For window resizing
	element.vml.push(rect);

	return(true);
}

function createTextShadow(element, vml_parent) {
	if (!element.textShadow) { return(false); }

	var match = element.textShadow.match(/^(\d+)px (\d+)px (\d+)px (#?\w+)/);
	if (!match) { return(false); }


	//var shadow = document.createElement('span');
	var shadow = element.cloneNode(true);
	var radius = parseInt(RegExp.$3 || 0);
	shadow.userAttrs = {
		'x': parseInt(RegExp.$1 || 0) - (radius),
		'y': parseInt(RegExp.$2 || 0) - (radius),
		'radius': radius / 2,
		'color': (RegExp.$4 || '#000')
	};
	shadow.position_offset = {
		'y': (0 - vml_parent.pos_ieCSS3.y + shadow.userAttrs.y),
		'x': (0 - vml_parent.pos_ieCSS3.x + shadow.userAttrs.x)
	};
	shadow.size_offset = {
		'width': 0,
		'height': 0
	};
	shadow.style.color = shadow.userAttrs.color;
	shadow.style.position = 'absolute';
	shadow.style.top = (element.pos_ieCSS3.y + shadow.position_offset.y) +'px';
	shadow.style.left = (element.pos_ieCSS3.x + shadow.position_offset.x) +'px';
	shadow.style.antialias = true;
	shadow.style.behavior = null;
	shadow.className = 'ieCSS3_text_shadow';
	shadow.innerHTML = element.innerHTML;
	// For some reason it only looks right with opacity at 75%
	shadow.style.filter = '\
		progid:DXImageTransform.Microsoft.Alpha(Opacity=75)\
		progid:DXImageTransform.Microsoft.Blur(pixelRadius='+ shadow.userAttrs.radius +',makeShadow=false,shadowOpacity=100)\
	';

	var clone = element.cloneNode(true);
	clone.position_offset = {
		'y': (0 - vml_parent.pos_ieCSS3.y),
		'x': (0 - vml_parent.pos_ieCSS3.x)
	};
	clone.size_offset = {
		'width': 0,
		'height': 0
	};
	clone.style.behavior = null;
	clone.style.position = 'absolute';
	clone.style.top = (element.pos_ieCSS3.y + clone.position_offset.y) +'px';
	clone.style.left = (element.pos_ieCSS3.x + clone.position_offset.x) +'px';
	clone.className = 'ieCSS3_text_shadow';


	element.parentNode.appendChild(shadow);
	element.parentNode.appendChild(clone);

	element.style.visibility = 'hidden';

	// For window resizing
	element.vml.push(clone);
	element.vml.push(shadow);

	return(true);
}

function ondocumentready(classID) {
	if (!supportsVml()) { return(false); }

  if (this.className.match(classID)) { return(false); }
	this.className = this.className.concat(' ', classID);

	// Add a namespace for VML (IE8 requires it)
	if (!document.namespaces.v) { document.namespaces.add("v", "urn:schemas-microsoft-com:vml"); }

	// Check to see if we've run once before on this page
	if (typeof(window.ieCSS3) == 'undefined') {
		// Create global ieCSS3 object
		window.ieCSS3 = {
			'vmlified_elements': new Array(),
			'update_timer': setInterval(updatePositionAndSize, timer_length)
		};

		if (typeof(window.onresize) == 'function') { window.ieCSS3.previous_onresize = window.onresize; }

		// Attach window resize event
		window.onresize = updatePositionAndSize;
	}


	// These attrs are for the script and have no meaning to the browser:
	this.borderRadius = parseInt(this.currentStyle['iecss3-border-radius'] ||
	                             this.currentStyle['-moz-border-radius'] ||
	                             this.currentStyle['-webkit-border-radius'] ||
	                             this.currentStyle['border-radius'] ||
	                             this.currentStyle['-khtml-border-radius']);
	this.arcSize = Math.min(this.borderRadius / Math.min(this.offsetWidth, this.offsetHeight), 1);
	this.fillColor = this.currentStyle.backgroundColor;
	this.fillSrc = this.currentStyle.backgroundImage.replace(/^url\("(.+)"\)$/, '$1');
	this.strokeColor = this.currentStyle.borderColor;
	this.strokeWeight = parseInt(this.currentStyle.borderWidth);
	this.stroked = 'true';
	if (isNaN(this.strokeWeight) || (this.strokeWeight == 0)) {
		this.strokeWeight = 0;
		this.strokeColor = fillColor;
		this.stroked = 'false';
	}
	this.opacity = parseFloat(this.currentStyle.opacity || 1);
	this.textShadow = this.currentStyle['text-shadow'];

	this.element.vml = new Array();
	this.zIndex = parseInt(this.currentStyle.zIndex);
	if (isNaN(this.zIndex)) { this.zIndex = 0; }

	// Find which element provides position:relative for the target element (default to BODY)
	vml_parent = this;
	var limit = 100, i = 0;
	do {
		vml_parent = vml_parent.parentElement;
		i++;
		if (i >= limit) { return(false); }
	} while ((typeof(vml_parent) != 'undefined') && (vml_parent.currentStyle.position != 'relative') && (vml_parent.tagName != 'BODY'));

	vml_parent.pos_ieCSS3 = findPos(vml_parent);
	this.pos_ieCSS3 = findPos(this);

	var rv1 = createBoxShadow(this, vml_parent);
	var rv2 = createBorderRect(this, vml_parent);
	var rv3 = createTextShadow(this, vml_parent);
	if (rv1 || rv2 || rv3) { window.ieCSS3.vmlified_elements.push(this.element); }

	if (typeof(vml_parent.document.ieCSS3_stylesheet) == 'undefined') {
		vml_parent.document.ieCSS3_stylesheet = vml_parent.document.createStyleSheet();
		vml_parent.document.ieCSS3_stylesheet.addRule("v\\:roundrect", "behavior: url(#default#VML)");
		vml_parent.document.ieCSS3_stylesheet.addRule("v\\:fill", "behavior: url(#default#VML)");
		// Compatibility with IE7.js
		vml_parent.document.ieCSS3_stylesheet.ie7 = true;
	}
}

function updatePositionAndSize() {
	if (typeof(window.ieCSS3.vmlified_elements) != 'object') { return(false); }

	for (var i in window.ieCSS3.vmlified_elements) {
		var el = window.ieCSS3.vmlified_elements[i];

		if (typeof(el.vml) != 'object') { continue; }

		for (var z in el.vml) {
			//var parent_pos = findPos(el.vml[z].parentNode);
			var new_pos = findPos(el);
			new_pos.x = (new_pos.x + el.vml[z].position_offset.x) + 'px';
			new_pos.y = (new_pos.y + el.vml[z].position_offset.y) + 'px';
			if (el.vml[z].style.left != new_pos.x) { el.vml[z].style.left = new_pos.x; }
			if (el.vml[z].style.top != new_pos.y) { el.vml[z].style.top = new_pos.y; }

			var new_size = {
				'width': parseInt(el.offsetWidth + el.vml[z].size_offset.width),
				'height': parseInt(el.offsetHeight + el.vml[z].size_offset.height)
			}
			if (el.vml[z].offsetWidth != new_size.width) { el.vml[z].style.width = new_size.width +'px'; }
			if (el.vml[z].offsetHeight != new_size.height) { el.vml[z].style.height = new_size.height +'px'; }
		}
	}

	if (event && (event.type == 'resize') && typeof(window.ieCSS3.previous_onresize) == 'function') { window.ieCSS3.previous_onresize(); }
}
</script>

 

 

IE9圆角矩形的CSS语法:

border-top-left-radius:15px; /*左上角*/
border-top-right-radius:15px; /*右上角*/
border-bottom-left-radius:10px; /*左下角*/
border-bottom-right-radius:10px; /*右下角*/

 或者是写成:

border-radius:15px; /*四個角一起設定*/

 

PS:

 

  FF的属性修饰可以写成 ( 依序是 : 左上角; 右上角; 右下角; 左下角;)

-moz-border-radius:
15px 0 15px 0;

 

    但是,-webkit-border-radius 不支持这种写法

 

链接地址1:http://www.css88.com/archives/2211

链接地址2:http://directguo.com/blog/index.php/2010/06/ie-css3/

链接地址3:http://blog.xuite.net/andy19890411/Orz/37421619

 

分享到:
评论

相关推荐

    ie-css3.htc 免费版

    1. **border-radius**:CSS3中的圆角属性,可以让元素的边角变得圆润,ie-css3.htc通过计算和绘制图像来模拟这一效果,使得在IE6到IE8中也能看到圆角的元素。 2. **box-shadow**:此属性用于添加阴影效果,ie-css3....

    ie-css3(让ie6 ie7 ue8支持css3).rar

    标题中的“ie-css3(让ie6 ie7 ue8支持css3).rar”指的是一个针对Internet Explorer(IE)浏览器的解决方案,尤其是版本6、7和8,这些版本不原生支持CSS3的新特性。这个压缩包包含了一个名为“ie-css3(让ie678支持css...

    让IE6、IE7、IE8支持CSS3的圆角、阴影样式

    在早期的Internet Explorer浏览器,即IE6、IE7和IE8,它们并不支持CSS3的一些新特性,如圆角和阴影效果。这些特性在现代浏览器中已经非常常见,可以为网页设计提供更丰富的视觉体验。为了让这些老版本的IE浏览器也能...

    iecss3.htc支持输入框圆角

    在网页设计中,为了实现美观的效果,经常需要用到CSS3中的圆角边框(border-radius)属性,让元素的四个角落呈现出圆形或椭圆形的外观。然而,早期版本的Internet Explorer浏览器,尤其是IE8及以下版本,并不支持这...

    ie-css3.htc(内有使用方法).rar

    在IE6、7、8中,"ie-css3.htc"通过类似的方式,利用VML技术来模拟这个效果,让元素在视觉上看起来具有阴影。 为了使用"ie-css3.htc",开发者需要在CSS中引入该文件,并将它绑定到需要应用CSS3特性的元素上。例如: ...

    ie-css3.htc

    - **圆角边框**:在IE6-8中,无法直接使用CSS3的`border-radius`属性,ie-css3.htc可以模拟实现圆角效果。 - **阴影效果**:通过ie-css3.htc,开发者可以创建元素的阴影效果,虽然性能相比现代浏览器可能较差,但...

    解决IE不兼容css3圆角和阴影问题

    2. 使用CSS3 PIE:CSS3 PIE(Progressive Internet Explorer)是一个JavaScript库,它通过VML(Vector Markup Language)扩展了IE的CSS渲染能力,使IE6到IE9能够支持一些CSS3特性,包括圆角和阴影。只需在页面头部...

    CSS3完美支持IE圆角阴影样式.zip

    对于不支持CSS3的IE浏览器(尤其是IE8及以下版本),我们可以使用jQuery库来模拟圆角效果。一种常见的方法是使用jQuery插件,如"jQuery.corner"或"jQuery.roundCorners"。这些插件通过动态创建额外的DOM元素和CSS...

    css兼容包-CSS圆角、阴影

    PIE通过使用VML(Vector Markup Language)技术,为IE6到IE9提供了对圆角、阴影等CSS3特性的支持。 PIE这个压缩包文件很可能包含了PIE的源代码、文档、示例和必要的配置文件,帮助开发者在项目中集成并使用这个兼容...

    使得IE支持CSS3 圆角阴影 渐变

    使得IE支持CSS3 圆角阴影 渐变

    CSS3实现圆角阴影渐变效果兼容IE等浏览器

    在现代网页设计中,CSS3已经成为了不可或缺的一部分,它提供了许多增强视觉效果的功能,比如圆角、阴影和渐变。本文将深入探讨如何利用CSS3实现圆角、阴影和渐变效果,并确保这些效果在包括IE在内的主流浏览器中都能...

    selectivizr让IE6-9获得css3的选择器之外,或获得了圆角、阴影、渐变色资源等css3属性的支持.rar

    在IT行业中,CSS3是Web设计者不可或缺的一部分,它提供了丰富的样式和效果,如圆角、阴影和渐变。然而,这些先进的特性在早期版本的Internet Explorer(IE6到IE9)中并不完全支持,这为开发者带来了挑战。为了解决这...

    ie8兼容html5 css3圆角阴影渐变placeholder等属性

    标题中的“ie8兼容html5 css3圆角阴影渐变placeholder等属性”指的是在IE8浏览器上实现HTML5和CSS3的一些新特性,包括圆角、阴影、渐变以及placeholder属性的支持。这些特性在现代浏览器中是标准支持的,但在较旧的...

    让IE6、7 、8支持CSS3新特性插件

    标题中的“让IE6、7、8支持CSS3新特性插件”指的是针对早期版本的Internet Explorer(IE6、IE7、IE8)的一种解决方案,这些浏览器不原生支持CSS3的新特性。CSS3是层叠样式表的第三个主要版本,引入了许多增强网页...

    让 IE6, 7和 8支持CSS3

    标题中的“让IE6, 7和8支持CSS3”指的是如何在老旧的Internet Explorer(IE)浏览器上实现对CSS3特性的兼容性支持。在IE6、7和8这些较旧版本中,它们并不完全支持现代的CSS3规范,这使得开发者在设计网页时面临诸多...

    让ie也支持css3

    这个文件通常包含一些JavaScript代码,用于实现IE不支持的CSS3特性,如渐变、阴影、圆角等。 2. **定义CSS类名**:在CSS文件中定义一个公用的类,如`.css3-enabled`。 3. **编写行为规则**:为这个公用类添加`...

    ie支持css3部分功能

    CSS3Pie是一个开源JavaScript库,通过VML(Vector Markup Language)技术,使IE6-IE8也能支持圆角效果。只需在HTML文件中引入PIE.js,然后在需要圆角的元素上添加`behavior: url(/path/to/PIE.htc)`,即可实现兼容。...

    ie 6 ie7 ie8 支持css3

    标题中的“ie 6 ie7 ie8 支持css3”指的是如何使老旧的Internet Explorer(IE)浏览器,包括版本6、7和8,能够支持CSS3的新特性。CSS3是层叠样式表的第三个主要版本,引入了大量增强网页设计的新功能,如圆角、阴影...

Global site tag (gtag.js) - Google Analytics