编写者:网友Fly Mirage
主要作用:
使不支持position:fixed的浏览器支持此属性
适用环境:
IE 6 、IE 7、IE 8 not in standards mode(兼容模式),在其他浏览器默认不会启用,也不会占用资源
无任何闪烁,无任何滑动动画,效果和Chrome、Firefox一样,无任何拖影。
并非监听onscroll、onresize事件,也不是采用setTimeout。使用IE中特有的CSS expression()。
支持:
常用: left:1px;top:1px;、 left:1px;bottom:1px;、 right:1px;bottom:1px; 、top:1px;right:1px;
这些方式理论上需要指定width和height,当然您不写宽高,系统可以自动判断,但是容易出现不可预料的结果(并非代码问题,其它浏览器也会)
代码如下:
-
-
-
-
-
-
-
-
-
-
-
-
-
- (function($) {
- var isIE = !!window.ActiveXObject;
- var isIE6 = isIE && !window.XMLHttpRequest;
- var isIE8 = isIE && !!document.documentMode && (document.documentMode == 8);
- var isIE7 = isIE && !isIE6 && !isIE8;
- if (isIE6 || isIE7) {
- $().ready(function() {
- var body = document.body;
- var BLANK_GIF;
- if (body.currentStyle.backgroundAttachment != "fixed") {
- if (body.currentStyle.backgroundImage == "none") {
- body.runtimeStyle.backgroundImage = "url(" + BLANK_GIF + ")";
- body.runtimeStyle.backgroundAttachment = "fixed";
- }
- }
- });
- }
- $.fn.extend({
- toFixed: function(position) {
- var isIE = !!window.ActiveXObject;
- var isIE6 = isIE && !window.XMLHttpRequest;
- var isIE8 = isIE && !!document.documentMode && (document.documentMode == 8);
- var isIE7 = isIE && !isIE6 && !isIE8;
- if (isIE6 || isIE7) {} else {
- return this;
- }
- return this.each(function() {
- var t = $(this);
- var id = t.get(0).id || 'fixed_' + parseInt(Math.rand() * 10000);
- var rect = {
- w: t.width(),
- h: t.height(),
- l: t.css('left'),
- r: t.css('right'),
- 't': t.css('top'),
- b: t.css('bottom')
- };
- if (rect.l != 'auto') rect.l = parseInt(rect.l);
- if (rect.r != 'auto') rect.r = parseInt(rect.r);
- if (rect.t != 'auto') rect.t = parseInt(rect.t);
- if (rect.b != 'auto') rect.b = parseInt(rect.b);
- var _pos = {
- left: rect.l,
- right: rect.r,
- top: rect.t,
- bottom: rect.b
- };
- _pos = $.extend(_pos, position);
- var css = '<style type="text/css">.' + id + '-fixed {position:absolute;bottom:auto;right:auto;clear:both;';
- if (rect.l != 'auto' && rect.r != 'auto')
- css += 'width:expression(eval(document.compatMode && document.compatMode==\'CSS1Compat\') ? documentElement.clientWidth - ' + rect.l + ' - ' + rect.r + ' : document.body.clientWidth - ' + rect.l + ' - ' + rect.r + ' );';
- if (rect.l == 'auto' && rect.r != 'auto')
- css += 'left:expression(eval(document.compatMode && document.compatMode==\'CSS1Compat\') ? documentElement.scrollLeft + (documentElement.clientWidth-this.clientWidth - ' + rect.r + ') : document.body.scrollLeft +(document.body.clientWidth-this.clientWidth - ' + rect.r + '));';
- else
- css += 'left:expression(eval(document.compatMode && document.compatMode==\'CSS1Compat\') ? documentElement.scrollLeft + ' + rect.l + ' : document.body.scrollLeft + ' + rect.l + ');';
- if (rect.t == 'auto' && rect.b != 'auto')
- css += 'top:expression(eval(document.compatMode && document.compatMode==\'CSS1Compat\') ? documentElement.scrollTop + (documentElement.clientHeight-this.clientHeight - ' + rect.b + ') : document.body.scrollTop +(document.body.clientHeight-this.clientHeight - ' + rect.b + '));';
- else
- css += 'top:expression(eval(document.compatMode && document.compatMode==\'CSS1Compat\') ? documentElement.scrollTop + ' + rect.t + ' : document.body.scrollTop + ' + rect.t + ');';
- css += '}</style>';
- $(css).appendTo('head');
- t.addClass(id + '-fixed');
- });
- }
- });
- })(jQuery);
打包下载
如果您觉得本文的内容对您的学习有所帮助:
关键字:
Jquery完美解决IE6解决Fixed兼容插件