简体   繁体   English

jQuery更改类-div在Firefox中消失,在Chrome,IE,Safari中很好

[英]jQuery changes class - div disappears in Firefox, fine in Chrome, IE, Safari

Here I have a div in a right sidebar that supposed to act in this way: 在这里,我在右侧栏中有一个div,它应该以这种方式起作用:

1) When you scroll down the page and div reaches the top of the browser screen, it's class changes and it will get fixed on top of the screen untill you reach to the bottom of div's parent element. 1)当您向下滚动页面并且div到达浏览器屏幕的顶部时,它的类将更改,并且它将固定在屏幕顶部,直到您到达div的父元素的底部。

2) Once the bottom of div reaches the bottom of parent element, it's class changes back to non-fixed position. 2)一旦div的底部到达父元素的底部,它的类就会变回非固定位置。

Here is the jsfiddle for you to play with http://jsfiddle.net/comparebest/z2Nyn/1/ 这是jsfiddle,供您使用http://jsfiddle.net/comparebest/z2Nyn/1/

Now to the problem: 现在解决问题:

For some reason in FireFox once div reaches the bottom of parent element, div disappears, while in Chrome, IE and Safari it stays visible. 由于某些原因,在div中,一旦div到达父元素的底部,div就会消失,而在Chrome,IE和Safari中它仍然可见。

You might need to make the size of your browser screen smaller in order to observe this behavior. 您可能需要减小浏览器屏幕的大小,才能观察到此现象。

How can I prevent div from disappearing in FF? 如何防止div在FF中消失?

PS: PS:

Heres the jQuery code: 继承人的jQuery代码:

    function fixInParent(selector) {
    var $el = $(selector);

    $(window).scroll(function() {
        if($el.parent().offset().top > $(this).scrollTop())
            $el.addClass('top').removeClass('floating').removeClass('bottom');
        else if ($(this).scrollTop() + $el.height() < $el.parent().offset().top + $el.parent().height())
            $el.addClass('floating').removeClass('top').removeClass('bottom');
        else
            $el.addClass('bottom').removeClass('top').removeClass('floating');
    });
}

$(document).ready(function() {
    fixInParent('#floater');
});​

The problem you're facing is actually very simple and yet kinda annoying. 您面临的问题实际上非常简单,但是有点烦人。 From the specs of CSS 2.1 it states: 根据CSS 2.1的规范,它指出:

The effect of 'position:relative' on table-row-group, table-header-group, table-footer-group, table-row, table-column-group, table-column, table-cell, and table-caption elements is undefined. 'position:relative'对table-row-group,table-header-group,table-footer-group,table-row,table-column-group,table-column,table-cell和table-caption元素的影响未定义。

From w3.org : " 9.3.1 Choosing a positioning scheme: 'position' property " 来自w3.org :“ 9.3.1选择定位方案:“ position”属性

So even if all other browsers use this value as " expected ", Firefox is not wrong in ignoring it. 因此,即使所有其他浏览器都将此值用作“ 期望值 ”,Firefox忽略它也不是错误的。 So you should think of an solution where the parent <td> -element with style="position:relative;" 因此,您应该考虑一个解决方案,其中将父<td> -element设置为style="position:relative;" is not the reference for your "floater"-box. 不是您的“漂流”框的参考。

Maybe the answers here can help you: 也许这里的答案可以帮助您:

" Does Firefox support position: relative on table elements? " Firefox是否支持位置:表元素上的相对位置?

the problem is Firefox's support for relative positioning on table-cells. 问题是Firefox支持在表格单元上进行相对定位。 See: Does Firefox support position: relative on table elements? 请参阅: Firefox支持表元素上的相对位置吗?

Your fix : 您的解决方法
Apply position:relative to the containing <table> tag, so that the table is the positioning context, instead of the cell. position:relative对于包含的<table>标记应用,以使表是定位上下文,而不是单元格。

This should work in your situation because you only have one row of cells, and they have the same bottom as the table. 这应该在您的情况下起作用,因为您只有一行单元格,并且它们的bottom与表格相同。 I was able to get this to work on your site in Firebug; 我能够在Firebug的您的网站上使用它; though not in your fiddle, because in that example, the cell is not aligned with the table bottom. 尽管不在您的摆弄中,因为在该示例中,单元格与桌面底部对齐。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 jQuery不会在IE中返回div,但是Firefox / Chrome / Safari是吗? - JQuery not returning div in IE, but Firefox/Chrome/Safari is? jQuery AJAX在任何IE中均不起作用,但在Firefox,Opera,Chrome和Safari中运行良好 - jQuery AJAX not working in any IE, but fine in Firefox, Opera, Chrome and Safari jQuery的.load()在IE中不起作用 - 但在Firefox,Chrome和Safari中都很好 - jQuery's .load() not working in IE - but fine in Firefox, Chrome and Safari Javascript / jQuery无法在Firefox,Safari和IE中运行。 精通Opera和Chrome - Javascript/jQuery not working in Firefox, Safari and IE. Fine in Opera and Chrome jQuery,可以在Chrome中正常运行,但不能在Firefox和Safari中运行 - Jquery,working fine in Chrome, but not in Firefox and Safari jQuery验证插件不受任何IE或Firefox &lt;3.6的支持,但适用于Chrome和Safari - JQuery validation plugin not supported by any IE or Firefox < 3.6 but works fine for Chrome and Safari 为什么这个jQuery脚本在Chrome和Opera中不起作用,而它在Firefox,IE和Safari中完全没问题? - Why is this jQuery script isn't working in Chrome and Opera while it's perfectly fine in Firefox, IE and Safari? JavaScript JQuery Ajax问题:POST在Firefox,IE,Safari和Chrome中无法正常工作 - JavaScript JQuery Ajax Issue: POST Works fine in Firefox, IE, Safari but not Chrome Safari浏览器未处理JQuery Right,但可在IE | Chrome | FireFox中使用 - Safari browser not processing JQuery Right but works in IE|Chrome|FireFox Just fine jQuery适用于Chrome和Safari,但不能适用于Firefox或IE? - jQuery working in Chrome and Safari, but not Firefox or IE?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM