【问题标题】:jquery/css changing css on jquery show in IEjquery/css 在 IE 中更改 jquery 上的 css
【发布时间】:2012-02-15 06:39:45
【问题描述】:

我用 jquery 做了一个悬停功能。在 FireFox 和 Chrome 中一切正常,问题出在 IE 中。我更改 css 顶部/左侧并使用 jquery.show 使 div 可见。虽然在 IE 中第一次悬停它时,它显示在错误的位置。第二次显示就好了..

var x = (e.pageX - this.offsetLeft) - $(this).next("div").width();
var y = e.pageY - this.offsetTop;



$(this).next("div").css({ display: 'block', 'position': 'fixed', zIndex: 2, left: x, top: y });

$(this).next("div").show("slow");

再次在 Chrome 和 Firefox 中运行良好,但 IE..(我只测试过 IE9)

这是 div 默认隐藏的 CSS:

display:none;
position:fixed;
z-index:2;   

【问题讨论】:

  • 为什么不在css文件中为下一个div添加一个类并为该类定义一个css类?
  • 最初它有一个 css 类(显示为 display:none 等),但我不知道。我就是这样做的。 IE 获取 .css 和 addClass 的方式有什么不同吗?
  • 为我工作:jsfiddle.net/gilly3/gup8u。你能发一个jsfiddle来证明这个问题吗?
  • 是的,让我摆弄我的完整解决方案。 Fiddle 能坚持多久?像我以后可以删除它吗?

标签: jquery css internet-explorer


【解决方案1】:

您遇到此问题是因为在计算 x 时 div 尚未完全加载。 jQuery 不知道 div 的宽度将是多少,因此返回 0。下一次,当然,div 已经加载,jQuery 得到正确的宽度。

您可以通过设置right 而不是left 来纠正此问题。在我看来,这也改善了动画效果:

http://jsfiddle.net/gilly3/gup8u/22/

$('.linkin').hover(function(e) {
    var x = document.documentElement.offsetWidth - e.pageX;
    var y = e.pageY;

    $(this).next("div").css({
        right: x,
        top: y
    }).stop(true, true).show("slow");
},
function() {
    $(this).next().addClass('tooltip').stop(true, true).hide("slow");
});

【讨论】:

  • 谢谢,似乎确实有效! IE 太慢了,FF 和 Chrome 做得很好
  • @QuincyJones - 它不适用于我的 Chrome 或 Firefox。 Chrome 的行为类似于 IE:imgur.com/G21wz。 Firefox 从来没有正确,通常 div 位于缩略图的中心,有时它偏向左侧,但从未像应有的那样排列在缩略图的左侧。对我来说,Firefox 和 IE 的速度同样慢。 Chrome 只是快了一点。
猜你喜欢
  • 2020-10-28
  • 1970-01-01
  • 2022-11-29
  • 1970-01-01
  • 2011-12-10
  • 1970-01-01
  • 1970-01-01
  • 2011-11-03
  • 2015-12-16
相关资源
最近更新 更多