jQuery .css(“margin-top”, value) 在 IE 8(标准模式)中未更新

发布于 2024-08-23 05:33:17 字数 1361 浏览 11 评论 0原文

我正在构建一个绑定到 $(window).scroll() 事件的自动跟随 div。这是我的 JavaScript。

var alert_top = 0;
var alert_margin_top = 0;

$(function() {
  alert_top = $("#ActionBox").offset().top;
  alert_margin_top = parseInt($("#ActionBox").css("margin-top"));

  $(window).scroll(function () {
    var scroll_top = $(window).scrollTop();
    if(scroll_top > alert_top) {
      $("#ActionBox").css("margin-top", ((scroll_top-alert_top)+(alert_margin_top*2))+"px");
      console.log("Setting margin-top to "+$("#ActionBox").css("margin-top"));
    } else {
      $("#ActionBox").css("margin-top", alert_margin_top+"px");
    };
  });
});

此代码假设存在此 CSS 规则

#ActionBox {
  margin-top: 15px;
}

,并且它采用 id 为“ActionBox”的元素(在本例中为 div)。 div 位于左侧对齐的菜单中,该菜单沿着侧面延伸,因此它的起始偏移量约为 200 px)。目标是一旦用户滚动超过 div 可能开始从浏览器视口顶部消失的点,就开始添加 margin-top 值(是的,我知道将其设置为position:fixed 会做同样的事情,但随后它会掩盖 ActionBox 下面但仍在菜单中的内容)。

现在,console.log 显示该事件每次都会触发,并且设置了正确的值。但在我的网络应用程序的某些页面中,div 没有重新绘制。这尤其奇怪,因为在其他页面(在 IE 中)代码按预期工作(并且在 FF、Opera 和 WebKit 中每次都有效)。所有页面都会评估(根据 W3C 验证器和 FireFox HTMLTidy 验证器,有 0 个错误和 0 个警告),并且不会抛出任何 JS 错误(根据 IE 开发人员工具栏和 Firebug)。这个谜团的另一部分是,如果我在 IE 开发工具的 HTML 样式资源管理器中取消选择 #ActionBox margin-top 规则,那么 div 会立即跳回到新调整的位置,如果滚动事件触发了重画,它应该具有该位置。此外,如果我强制 IE8 进入怪异模式或兼容模式,那么甚至会触发更新。

还有一件事,它在 IE7 和 IE 6 中按预期工作(感谢精彩的 IETester)

I'm building an auto-follow div that is bound to the $(window).scroll() event. Here is my JavaScript.

var alert_top = 0;
var alert_margin_top = 0;

$(function() {
  alert_top = $("#ActionBox").offset().top;
  alert_margin_top = parseInt($("#ActionBox").css("margin-top"));

  $(window).scroll(function () {
    var scroll_top = $(window).scrollTop();
    if(scroll_top > alert_top) {
      $("#ActionBox").css("margin-top", ((scroll_top-alert_top)+(alert_margin_top*2))+"px");
      console.log("Setting margin-top to "+$("#ActionBox").css("margin-top"));
    } else {
      $("#ActionBox").css("margin-top", alert_margin_top+"px");
    };
  });
});

This code assumes that there is this CSS rule in place

#ActionBox {
  margin-top: 15px;
}

And it takes an element with the id "ActionBox" (in this case a div). The div is positioned in a left aligned menu that runs down the side, so it's starting offset is approximately 200 px). The goal is to start adding to the margin-top value once the user has scrolled past the point where the div might start to disappear off the top of the browser viewport (yes I know setting it to position: fixed would do the same thing, but then it would obscure the content below the ActionBox but still in the menu).

Now the console.log shows that the event is firing every time it should and it's setting the correct value. But in some pages of my web app the div isn't redrawn. This is especially odd because in other pages (in IE) the code works as expected (and it works every time in FF, Opera and WebKit). All pages evaluate (0 errors and 0 warnings according to the W3C validator and the FireFox HTMLTidy Validator), and no JS errors are thrown (according to the IE Developer Toolbar and Firebug). One other part to this mystery, if I unselect the #ActionBox margin-top rule in the HTML Style explorer in the IE Developer Tools then the div jumps immediately back in the newly adjusted place that it should have if the scroll event had triggered a redraw. Also if I force IE8 into Quirks Mode or compatibility mode then the even triggers an update.

One More thing, it works as expected in IE7 and IE 6 (thanks to the wonderful IETester for that)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

是伱的 2024-08-30 05:33:17

我在 Firefox 中使用您的脚本时遇到问题。当我向下滚动时,脚本继续向页面添加边距,但我永远不会到达页面底部。发生这种情况是因为 ActionBox 仍然是页面元素的一部分。我在此处发布了演示

  • 一种解决方案是在 CSS 定义中添加 position:fixed ,但我发现这对您不起作用
  • 另一种解决方案是将 ActionBox 绝对定位(到文档正文)并调整顶部
  • 更新了代码以适应为其他人提供的解决方案。

更新:

CSS

#ActionBox {
 position: relative;
 float: right;
}

脚本

var alert_top = 0;
var alert_margin_top = 0;

$(function() {
  alert_top = $("#ActionBox").offset().top;
  alert_margin_top = parseInt($("#ActionBox").css("margin-top"),10);

  $(window).scroll(function () {
    var scroll_top = $(window).scrollTop();
    if (scroll_top > alert_top) {
      $("#ActionBox").css("margin-top", ((scroll_top-alert_top)+(alert_margin_top*2)) + "px");
      console.log("Setting margin-top to " + $("#ActionBox").css("margin-top"));
    } else {
      $("#ActionBox").css("margin-top", alert_margin_top+"px");
    };
  });
});

另外,向 parseInt() 添加基数(本例中为 10)也很重要,例如

parseInt($("#ActionBox").css("top"),10);

I'm having a problem with your script in Firefox. When I scroll down, the script continues to add a margin to the page and I never reach the bottom of the page. This occurs because the ActionBox is still part of the page elements. I posted a demo here.

  • One solution would be to add a position: fixed to the CSS definition, but I see this won't work for you
  • Another solution would be to position the ActionBox absolutely (to the document body) and adjust the top.
  • Updated the code to fit with the solution found for others to benefit.

UPDATED:

CSS

#ActionBox {
 position: relative;
 float: right;
}

Script

var alert_top = 0;
var alert_margin_top = 0;

$(function() {
  alert_top = $("#ActionBox").offset().top;
  alert_margin_top = parseInt($("#ActionBox").css("margin-top"),10);

  $(window).scroll(function () {
    var scroll_top = $(window).scrollTop();
    if (scroll_top > alert_top) {
      $("#ActionBox").css("margin-top", ((scroll_top-alert_top)+(alert_margin_top*2)) + "px");
      console.log("Setting margin-top to " + $("#ActionBox").css("margin-top"));
    } else {
      $("#ActionBox").css("margin-top", alert_margin_top+"px");
    };
  });
});

Also it is important to add a base (10 in this case) to your parseInt(), e.g.

parseInt($("#ActionBox").css("top"),10);
菊凝晚露 2024-08-30 05:33:17

尝试用 marginTop 代替 margin-top,例如:

$("#ActionBox").css("marginTop", foo);

Try marginTop in place of margin-top, eg:

$("#ActionBox").css("marginTop", foo);
情绪失控 2024-08-30 05:33:17

我找到了答案!

我想感谢大家为寻找更好的方法来解决这个问题所做的辛勤工作,不幸的是,由于一系列较大的限制,我无法选择他们作为“答案”(我投票给他们,因为你的贡献值得得分)。

我面临的具体问题是 JavaScript onScoll 事件正在触发,但随后的 CSS 更新并没有导致 IE8(在标准模式下)重绘。更奇怪的是,在某些页面中它正在重新绘制,而在其他页面中(没有明显的相似性)则没有。最后的解决方案是添加以下 CSS

#ActionBox {
  position: relative;
  float: right;
}

这是更新的 pastbin 显示此(我添加了更多样式来展示我如何实现此代码)。 IE“编辑代码”然后“查看输出”的错误 fudgey 谈到的仍然发生(但这似乎是 Pastbin(和类似服务)特有的事件绑定问题

我不知道为什么添加“float:right”允许 IE8对已经触发的事件完成重绘,但由于某种原因确实如此。

I found the answer!

I want to acknowledge the hard work of everyone in trying to find a better way to solve this problem, unfortunately because of a series of larger constraints I am unable to select them as the "answer" (I am voting them up because you deserve points for contributing).

The specific problem I was facing was a JavaScript onScoll event that was firing but a subsequent CSS update that wasn't causing IE8 (in standards mode) to redraw. Even stranger was the fact that in some pages it was redrawing while in others (with no obvious similarity) it wasn't. The solution in the end was to add the following CSS

#ActionBox {
  position: relative;
  float: right;
}

Here is an updated pastbin showing this (I added some more style to show how I am implementing this code). The IE "edit code" then "view output" bug fudgey talked about still occurs (but it seems to be a event binding issue unique to pastbin (and similar services)

I don't know why adding "float: right" allows IE8 to complete a redraw on an event that was already firing, but for some reason it does.

天涯沦落人 2024-08-30 05:33:17

IE8 的正确格式是:

  $("#ActionBox").css({ 'margin-top': '10px' });  

with this work.

The correct format for IE8 is:

  $("#ActionBox").css({ 'margin-top': '10px' });  

with this work.

老街孤人 2024-08-30 05:33:17

试试这个方法

$("your id or class name").css({ 'margin-top': '18px' });  

try this method

$("your id or class name").css({ 'margin-top': '18px' });  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文