让IE6显示阴影

 

大家都知道,想让一个div显示有阴影效果,一般是加背景图片,这里提供了两个方法,不用加背景图片的方法,甚至让该死的IE6上也有效果,而且还是圆角的哦

方法一:

下载ie-css3.htc的文件,文本打开后,就是js代码,如下,可以拷贝粘贴保存:

 

[javascript] view plaincopyprint?
  1. --Do not remove thisif you are using-- 
  2. Original Author: Remiz Rahnas 
  3. Original Author URL: http://www.htmlremix.com  
  4. Published date: 2008/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 : 2010/03/14 
  14. http://fetchak.com/ie-css3  
  15.  
  16. 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  
  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 http://stackoverflow.com/questions/654112/how-do-you-detect-support-for-vml-or-svg-in-a-browser  
  26. function supportsVml() { 
  27.     if (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.     } 
  35.     return supportsVml.supported 
  36.  
  37.  
  38. // findPos() borrowed from http://www.quirksmode.org/js/findpos.html  
  39. function findPos(obj) { 
  40.     var curleft = curtop = 0; 
  41.  
  42.     if (obj.offsetParent) { 
  43.         do
  44.             curleft += obj.offsetLeft; 
  45.             curtop += obj.offsetTop; 
  46.         } while (obj = obj.offsetParent); 
  47.     } 
  48.  
  49.     return({ 
  50.         'x': curleft, 
  51.         'y': curtop 
  52.     }); 
  53.  
  54. function createBoxShadow(element, vml_parent) { 
  55.     var style = element.currentStyle['iecss3-box-shadow'] || element.currentStyle['-moz-box-shadow'] || element.currentStyle['-webkit-box-shadow'] || element.currentStyle['box-shadow'] || ''
  56.     var match = style.match(/^(\d+)px (\d+)px (\d+)px/); 
  57.     if (!match) { return(false); } 
  58.  
  59.  
  60.     var shadow = document.createElement('v:roundrect'); 
  61.     shadow.userAttrs = { 
  62.         'x': parseInt(RegExp.$1 || 0), 
  63.         'y': parseInt(RegExp.$2 || 0), 
  64.         'radius': parseInt(RegExp.$3 || 0) / 2 
  65.     }; 
  66.     shadow.position_offset = { 
  67.         'y': (0 - vml_parent.pos_ieCSS3.y - shadow.userAttrs.radius + shadow.userAttrs.y), 
  68.         'x': (0 - vml_parent.pos_ieCSS3.x - shadow.userAttrs.radius + shadow.userAttrs.x) 
  69.     }; 
  70.     shadow.size_offset = { 
  71.         'width': 0, 
  72.         'height': 0 
  73.     }; 
  74.     shadow.arcsize = element.arcSize +'px'
  75.     shadow.style.display = 'block'
  76.     shadow.style.position = 'absolute'
  77.     shadow.style.top = (element.pos_ieCSS3.y + shadow.position_offset.y) +'px'
  78.     shadow.style.left = (element.pos_ieCSS3.x + shadow.position_offset.x) +'px'
  79.     shadow.style.width = element.offsetWidth +'px'
  80.     shadow.style.height = element.offsetHeight +'px'
  81.     shadow.style.antialias = true
  82.     shadow.className = 'vml_box_shadow'
  83.     shadow.style.zIndex = element.zIndex - 1; 
  84.     shadow.style.filter = 'progid:DXImageTransform.Microsoft.Blur(pixelRadius='+ shadow.userAttrs.radius +',makeShadow=true,shadowOpacity='+ element.opacity +')'
  85.  
  86.     element.parentNode.appendChild(shadow); 
  87.     //element.parentNode.insertBefore(shadow, element.element);  
  88.  
  89.     // For window resizing  
  90.     element.vml.push(shadow); 
  91.  
  92.     return(true); 
  93.  
  94. function createBorderRect(element, vml_parent) { 
  95.     if (isNaN(element.borderRadius)) { return(false); } 
  96.  
  97.     element.style.background = 'transparent'
  98.     element.style.borderColor = 'transparent'
  99.  
  100.     var rect = document.createElement('v:roundrect'); 
  101.     rect.position_offset = { 
  102.         'y': (0.5 * element.strokeWeight) - vml_parent.pos_ieCSS3.y, 
  103.         'x': (0.5 * element.strokeWeight) - vml_parent.pos_ieCSS3.x 
  104.     }; 
  105.     rect.size_offset = { 
  106.         'width': 0 - element.strokeWeight, 
  107.         'height': 0 - element.strokeWeight 
  108.     }; 
  109.     rect.arcsize = element.arcSize +'px'
  110.     rect.strokeColor = element.strokeColor; 
  111.     rect.strokeWeight = element.strokeWeight +'px'
  112.     rect.stroked = element.stroked; 
  113.     rect.className = 'vml_border_radius'
  114.     rect.style.display = 'block'
  115.     rect.style.position = 'absolute'
  116.     rect.style.top = (element.pos_ieCSS3.y + rect.position_offset.y) +'px'
  117.     rect.style.left = (element.pos_ieCSS3.x + rect.position_offset.x) +'px'
  118.     rect.style.width = (element.offsetWidth + rect.size_offset.width) +'px'
  119.     rect.style.height = (element.offsetHeight + rect.size_offset.height) +'px'
  120.     rect.style.antialias = true
  121.     rect.style.zIndex = element.zIndex - 1; 
  122.  
  123.     if (border_opacity && (element.opacity < 1)) { 
  124.         rect.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(Opacity='+ parseFloat(element.opacity * 100) +')'
  125.     } 
  126.  
  127.     var fill = document.createElement('v:fill'); 
  128.     fill.color = element.fillColor; 
  129.     fill.src = element.fillSrc; 
  130.     fill.className = 'vml_border_radius_fill'
  131.     fill.type = 'tile'
  132.     fill.opacity = element.opacity; 
  133.  
  134.     // Hack: IE6 doesn't support transparent borders, use padding to offset original element  
  135.     isIE6 = /msie|MSIE 6/.test(navigator.userAgent); 
  136.     if (isIE6 && (element.strokeWeight > 0)) { 
  137.         element.style.borderStyle = 'none'
  138.         element.style.paddingTop = parseInt(element.currentStyle.paddingTop || 0) + element.strokeWeight; 
  139.         element.style.paddingBottom = parseInt(element.currentStyle.paddingBottom || 0) + element.strokeWeight; 
  140.     } 
  141.  
  142.     rect.appendChild(fill); 
  143.     element.parentNode.appendChild(rect); 
  144.     //element.parentNode.insertBefore(rect, element.element);  
  145.  
  146.     // For window resizing  
  147.     element.vml.push(rect); 
  148.  
  149.     return(true); 
  150.  
  151. function createTextShadow(element, vml_parent) { 
  152.     if (!element.textShadow) { return(false); } 
  153.  
  154.     var match = element.textShadow.match(/^(\d+)px (\d+)px (\d+)px (#?\w+)/); 
  155.     if (!match) { return(false); } 
  156.  
  157.  
  158.     //var shadow = document.createElement('span');  
  159.     var shadow = element.cloneNode(true); 
  160.     var radius = parseInt(RegExp.$3 || 0); 
  161.     shadow.userAttrs = { 
  162.         'x': parseInt(RegExp.$1 || 0) - (radius), 
  163.         'y': parseInt(RegExp.$2 || 0) - (radius), 
  164.         'radius': radius / 2, 
  165.         'color': (RegExp.$4 || '#000'
  166.     }; 
  167.     shadow.position_offset = { 
  168.         'y': (0 - vml_parent.pos_ieCSS3.y + shadow.userAttrs.y), 
  169.         'x': (0 - vml_parent.pos_ieCSS3.x + shadow.userAttrs.x) 
  170.     }; 
  171.     shadow.size_offset = { 
  172.         'width': 0, 
  173.         'height': 0 
  174.     }; 
  175.     shadow.style.color = shadow.userAttrs.color; 
  176.     shadow.style.position = 'absolute'
  177.     shadow.style.top = (element.pos_ieCSS3.y + shadow.position_offset.y) +'px'
  178.     shadow.style.left = (element.pos_ieCSS3.x + shadow.position_offset.x) +'px'
  179.     shadow.style.antialias = true
  180.     shadow.style.behavior = null
  181.     shadow.className = 'ieCSS3_text_shadow'
  182.     shadow.innerHTML = element.innerHTML; 
  183.     // For some reason it only looks right with opacity at 75%  
  184.     shadow.style.filter = '\ 
  185.         progid:DXImageTransform.Microsoft.Alpha(Opacity=75)\ 
  186.         progid:DXImageTransform.Microsoft.Blur(pixelRadius='+ shadow.userAttrs.radius +',makeShadow=false,shadowOpacity=100)\ 
  187.     '; 
  188.  
  189.     var clone = element.cloneNode(true); 
  190.     clone.position_offset = { 
  191.         'y': (0 - vml_parent.pos_ieCSS3.y), 
  192.         'x': (0 - vml_parent.pos_ieCSS3.x) 
  193.     }; 
  194.     clone.size_offset = { 
  195.         'width': 0, 
  196.         'height': 0 
  197.     }; 
  198.     clone.style.behavior = null
  199.     clone.style.position = 'absolute'
  200.     clone.style.top = (element.pos_ieCSS3.y + clone.position_offset.y) +'px'
  201.     clone.style.left = (element.pos_ieCSS3.x + clone.position_offset.x) +'px'
  202.     clone.className = 'ieCSS3_text_shadow'
  203.  
  204.  
  205.     element.parentNode.appendChild(shadow); 
  206.     element.parentNode.appendChild(clone); 
  207.  
  208.     element.style.visibility = 'hidden'
  209.  
  210.     // For window resizing  
  211.     element.vml.push(clone); 
  212.     element.vml.push(shadow); 
  213.  
  214.     return(true); 
  215.  
  216. function ondocumentready(classID) { 
  217.     if (!supportsVml()) { return(false); } 
  218.  
  219.   if (this.className.match(classID)) { return(false); } 
  220.     this.className = this.className.concat(' ', classID); 
  221.  
  222.     // Add a namespace for VML (IE8 requires it)  
  223.     if (!document.namespaces.v) { document.namespaces.add("v", "urn:schemas-microsoft-com:vml"); } 
  224.  
  225.     // Check to see if we've run once before on this page  
  226.     if (typeof(window.ieCSS3) == 'undefined') { 
  227.         // Create global ieCSS3 object  
  228.         window.ieCSS3 = { 
  229.             'vmlified_elements': new Array(), 
  230.             'update_timer': setInterval(updatePositionAndSize, timer_length) 
  231.         }; 
  232.  
  233.         if (typeof(window.onresize) == 'function') { window.ieCSS3.previous_onresize = window.onresize; } 
  234.  
  235.         // Attach window resize event  
  236.         window.onresize = updatePositionAndSize; 
  237.     } 
  238.  
  239.  
  240.     // These attrs are for the script and have no meaning to the browser:  
  241.     this.borderRadius = parseInt(this.currentStyle['iecss3-border-radius'] || 
  242.                                  this.currentStyle['-moz-border-radius'] || 
  243.                                  this.currentStyle['-webkit-border-radius'] || 
  244.                                  this.currentStyle['border-radius'] || 
  245.                                  this.currentStyle['-khtml-border-radius']); 
  246.     this.arcSize = Math.min(this.borderRadius / Math.min(this.offsetWidth, this.offsetHeight), 1); 
  247.     this.fillColor = this.currentStyle.backgroundColor; 
  248.     this.fillSrc = this.currentStyle.backgroundImage.replace(/^url\("(.+)"\)$/, '$1'); 
  249.     this.strokeColor = this.currentStyle.borderColor; 
  250.     this.strokeWeight = parseInt(this.currentStyle.borderWidth); 
  251.     this.stroked = 'true'
  252.     if (isNaN(this.strokeWeight) || (this.strokeWeight == 0)) { 
  253.         this.strokeWeight = 0; 
  254.         this.strokeColor = fillColor; 
  255.         this.stroked = 'false'
  256.     } 
  257.     this.opacity = parseFloat(this.currentStyle.opacity || 1); 
  258.     this.textShadow = this.currentStyle['text-shadow']; 
  259.  
  260.     this.element.vml = new Array(); 
  261.     this.zIndex = parseInt(this.currentStyle.zIndex); 
  262.     if (isNaN(this.zIndex)) { this.zIndex = 0; } 
  263.  
  264.     // Find which element provides position:relative for the target element (default to BODY)  
  265.     vml_parent = this
  266.     var limit = 100, i = 0; 
  267.     do
  268.         vml_parent = vml_parent.parentElement; 
  269.         i++; 
  270.         if (i >= limit) { return(false); } 
  271.     } while ((typeof(vml_parent) != 'undefined') && (vml_parent.currentStyle.position != 'relative') && (vml_parent.tagName != 'BODY')); 
  272.  
  273.     vml_parent.pos_ieCSS3 = findPos(vml_parent); 
  274.     this.pos_ieCSS3 = findPos(this); 
  275.  
  276.     var rv1 = createBoxShadow(this, vml_parent); 
  277.     var rv2 = createBorderRect(this, vml_parent); 
  278.     var rv3 = createTextShadow(this, vml_parent); 
  279.     if (rv1 || rv2 || rv3) { window.ieCSS3.vmlified_elements.push(this.element); } 
  280.  
  281.     if (typeof(vml_parent.document.ieCSS3_stylesheet) == 'undefined') { 
  282.         vml_parent.document.ieCSS3_stylesheet = vml_parent.document.createStyleSheet(); 
  283.         vml_parent.document.ieCSS3_stylesheet.addRule("v\\:roundrect", "behavior: url(#default#VML)"); 
  284.         vml_parent.document.ieCSS3_stylesheet.addRule("v\\:fill", "behavior: url(#default#VML)"); 
  285.         // Compatibility with IE7.js  
  286.         vml_parent.document.ieCSS3_stylesheet.ie7 = true
  287.     } 
  288.  
  289. function updatePositionAndSize() { 
  290.     if (typeof(window.ieCSS3.vmlified_elements) != 'object') { return(false); } 
  291.  
  292.     for (var i in window.ieCSS3.vmlified_elements) { 
  293.         var el = window.ieCSS3.vmlified_elements[i]; 
  294.  
  295.         if (typeof(el.vml) != 'object') { continue; } 
  296.  
  297.         for (var z in el.vml) { 
  298.             //var parent_pos = findPos(el.vml[z].parentNode);  
  299.             var new_pos = findPos(el); 
  300.             new_pos.x = (new_pos.x + el.vml[z].position_offset.x) + 'px'
  301.             new_pos.y = (new_pos.y + el.vml[z].position_offset.y) + 'px'
  302.             if (el.vml[z].style.left != new_pos.x) { el.vml[z].style.left = new_pos.x; } 
  303.             if (el.vml[z].style.top != new_pos.y) { el.vml[z].style.top = new_pos.y; } 
  304.  
  305.             var new_size = { 
  306.                 'width': parseInt(el.offsetWidth + el.vml[z].size_offset.width), 
  307.                 'height': parseInt(el.offsetHeight + el.vml[z].size_offset.height) 
  308.             } 
  309.             if (el.vml[z].offsetWidth != new_size.width) { el.vml[z].style.width = new_size.width +'px'; } 
  310.             if (el.vml[z].offsetHeight != new_size.height) { el.vml[z].style.height = new_size.height +'px'; } 
  311.         } 
  312.     } 
  313.  
  314.     if (event && (event.type == 'resize') && typeof(window.ieCSS3.previous_onresize) == 'function') { window.ieCSS3.previous_onresize(); } 
  315. </script> 
--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>
然后在页面中引入这个文件,当css文件引入,再定义一个class

 

 

[html] view plaincopyprint?
  1. .box 
  2.     -moz-border-radius: 15px; /* Firefox */ 
  3.     -webkit-border-radius: 15px; /* Safari and Chrome */ 
  4.     border-radius: 15px; /* Opera 10.5+, future browsers, and now also Internet Explorer 6+ using IE-CSS3 */ 
  5.     -moz-box-shadow: 10px 10px 20px #000; /* Firefox */ 
  6.     -webkit-box-shadow: 10px 10px 20px #000; /* Safari and Chrome */ 
  7.     box-shadow: 10px 10px 20px #000; /* Opera 10.5+, future browsers and IE6+ using IE-CSS3 */ 
  8.     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);
        }
然后在div中的class属性中加入box,再设置长宽就可以看到阴影效果

 

html代码如下:

 

[html] view plaincopyprint?
  1. <html> 
  2. <head> 
  3.     <linkhref="ie-css3.htc"rel="stylesheet"type="text/css"/> 
  4.     <styletype="text/css"> 
  5.         .box 
  6.         { 
  7.             -moz-border-radius: 15px; /* Firefox */ 
  8.             -moz-box-shadow: 10px 10px 20px #000; /* Firefox */ 
  9.             border-radius: 15px; /* Opera 10.5+, future browsers, and now also Internet Explorer 6+ using IE-CSS3 */ 
  10.             -webkit-border-radius: 15px; /* Safari and Chrome */ 
  11.             -webkit-box-shadow: 10px 10px 20px #000; /* Safari and Chrome */ 
  12.             box-shadow: 10px 10px 20px #000; /* Opera 10.5+, future browsers and IE6+ using IE-CSS3 */ 
  13.             behavior: url(ie-css3.htc); 
  14.         } 
  15.     </style> 
  16. </head> 
  17. <body> 
  18.     <divclass="box"style="width: 200px; height: 200px;"> 
  19.     </div> 
  20. </body> 
  21. </html> 
<html>
<head>
    <link href="ie-css3.htc" rel="stylesheet" type="text/css" />
    <style type="text/css">
        .box
        {
            -moz-border-radius: 15px; /* Firefox */
            -moz-box-shadow: 10px 10px 20px #000; /* Firefox */
            border-radius: 15px; /* Opera 10.5+, future browsers, and now also Internet Explorer 6+ using IE-CSS3 */
            -webkit-border-radius: 15px; /* Safari and Chrome */
            -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);
        }
    </style>
</head>
<body>
    <div class="box" style="width: 200px; height: 200px;">
    </div>
</body>
</html>

 

但ie6中显示背景是黑色,可以通过添加background-color:white;属性来让背景色为白色。

而且IE中阴影色只是黑色,不管你设置成什么颜色

方法二:只适用于IE浏览器

纯css控制

 

[html] view plaincopyprint?
  1. <html> 
  2. <head> 
  3.     <styletype="text/css"> 
  4.         .img 
  5.         { 
  6.             background: #fff; 
  7.             -ms-filter:"progid:DXImageTransform.Microsoft.Shadow(color=#eeeeee,direction=0,strength=6)  
  8.             progid:DXImageTransform.Microsoft.Shadow(color=#dddddd,direction=90,strength=10)        progid:DXImageTransform.Microsoft.Shadow 
  9.             (color=#dddddd,direction=180,strength=10) progid:DXImageTransform.Microsoft.Shadow          (color=#eeeeee,direction=270,strength=6)"; 
  10.             *filter:  
  11.             progid:DXImageTransform.Microsoft.Shadow(color=#eeeeee,direction=0,strength=6
  12.             progid:DXImageTransform.Microsoft.Shadow(color=#dddddd,direction=90,strength=10
  13.             progid:DXImageTransform.Microsoft.Shadow(color=#dddddd,direction=180,strength=10
  14.             progid:DXImageTransform.Microsoft.Shadow(color=#eeeeee,direction=270,strength=6); 
  15.         } 
  16.     </style> 
  17. </head> 
  18. <body> 
  19.     <divclass="img"style="width: 200px; height: 200px;"> 
  20.     </div> 
  21. </body> 
  22. </html> 
<html>
<head>
    <style type="text/css">
        .img
        {
            background: #fff;
            -ms-filter:"progid:DXImageTransform.Microsoft.Shadow(color=#eeeeee,direction=0,strength=6) 
            progid:DXImageTransform.Microsoft.Shadow(color=#dddddd,direction=90,strength=10) 	    progid:DXImageTransform.Microsoft.Shadow
            (color=#dddddd,direction=180,strength=10) progid:DXImageTransform.Microsoft.Shadow		    (color=#eeeeee,direction=270,strength=6)";
            *filter: 
            progid:DXImageTransform.Microsoft.Shadow(color=#eeeeee,direction=0,strength=6)
            progid:DXImageTransform.Microsoft.Shadow(color=#dddddd,direction=90,strength=10)
            progid:DXImageTransform.Microsoft.Shadow(color=#dddddd,direction=180,strength=10)
            progid:DXImageTransform.Microsoft.Shadow(color=#eeeeee,direction=270,strength=6);
        }
    </style>
</head>
<body>
    <div class="img" style="width: 200px; height: 200px;">
    </div>
</body>
</html>
其中,color是颜色,direction是顺时针偏转的角度,strength是阴影的大小
posted @ 2012-11-12 18:35  szwencan  阅读(211)  评论(0编辑  收藏  举报