jQuery fadeIn 不适用于 IE9

发布于 2024-11-28 06:31:19 字数 2431 浏览 1 评论 0原文

奇怪的问题这个。我有一个网页,可以使用 jQuery 的 fadeIn() 和 fadeOut() 方法在计时器上淡入和淡出图像。它在 Firefox 和 Chrome 中运行良好,但在 IE9 中则不然——仅显示第一张图像。在大量删除 CSS、HTML 等之后,为了减少原因,我设法让它在 IE9 中工作但前提是我删除 HTML 文件的第一行

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

有了该行, fadeIn() 失败。如果没有该行,fadeIn() 就可以工作。不管怎样,fadeOut() 似乎都能工作。

我正在使用最新的稳定版 jQuery(昨天下载)。

这是 HTML 文件:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <script src="scripts/jquery.js" type="text/javascript"></script>
        <script src="scripts/imageswitch.js" type="text/javascript"></script>
    </head>
    <body>
        <div id="myGallery">
            <img src="/images/Img1.jpg" id="firstimg" class="active ready" onload="setInterval( 'swapImages()', 3000 );" />
            <img src="/images/Img2.jpg" onload="SetImgReady(this);" />
            <img src="/images/Img3.jpg" onload="SetImgReady(this);" />
        </div>
    </body>
</html>

这是“imageswitch.js”的内容:(我使用 SetImgReady() 函数在完成下载后将“ready”类添加到每个图像,因此它只在完全加载的图像之间旋转)

function swapImages() {
    var $active = $('#myGallery .active');
    var $next = ($('#myGallery .active').next().length > 0) ? $('#myGallery .active').next() : $('#myGallery img:first');

    while ($next && !$next.hasClass('ready')) {
        if ($next.next().length > 0)
            $next = $next.next();
        else
            $next = false;
    }

    if ($next && $active) {
        $next.fadeIn( 2000 ).addClass('active');
        $active.fadeOut( 2000 ).removeClass('active');
    }
}

function SetImgReady( $ImgObj ) {
    if ($ImgObj)
        $ImgObj.className += "ready";
}

这就是 CSS:

#myGallery{
    position:relative;
    width:400px;
    height:300px;
}
#myGallery img{
    display:none;
    position:absolute;
    top:0;
    left:0;
    border: 1px solid black;
}
#myGallery img.active{
    display:block;
}

那么...声明是否使 IE9 过度符合标准并决定在 XHTML1.0 或其他情况下不允许淡入淡出?

一旦淡入淡出起作用,还会发生另一个奇怪的事情:仅在第一个图像切换时,显示的图像立即消失而不是淡出。所有其他图像都可以根据需要很好地淡入/淡出。对此也有什么想法吗?

谢谢大家。

Weird problem this. I've got a web page that fades images in and out on a timer, using jQuery's fadeIn() and fadeOut() methods. It worked fine in Firefox and Chrome, but not in IE9 - only the first image ever showed. After a lot of removing CSS, HTML, etc, to whittle down the reason, I managed to get it working in IE9 but only if I remove the first line of my HTML file:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

With that line present, fadeIn() fails. Without that line, fadeIn() works. fadeOut() appears to work regardless.

I'm using the latest stable build of jQuery (downloaded yesterday).

This is the HTML file:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <script src="scripts/jquery.js" type="text/javascript"></script>
        <script src="scripts/imageswitch.js" type="text/javascript"></script>
    </head>
    <body>
        <div id="myGallery">
            <img src="/images/Img1.jpg" id="firstimg" class="active ready" onload="setInterval( 'swapImages()', 3000 );" />
            <img src="/images/Img2.jpg" onload="SetImgReady(this);" />
            <img src="/images/Img3.jpg" onload="SetImgReady(this);" />
        </div>
    </body>
</html>

This is the contents of "imageswitch.js": (I use the SetImgReady() function to add the 'ready' class to each image once it's finished downloading, so it only ever rotates between fully loaded images)

function swapImages() {
    var $active = $('#myGallery .active');
    var $next = ($('#myGallery .active').next().length > 0) ? $('#myGallery .active').next() : $('#myGallery img:first');

    while ($next && !$next.hasClass('ready')) {
        if ($next.next().length > 0)
            $next = $next.next();
        else
            $next = false;
    }

    if ($next && $active) {
        $next.fadeIn( 2000 ).addClass('active');
        $active.fadeOut( 2000 ).removeClass('active');
    }
}

function SetImgReady( $ImgObj ) {
    if ($ImgObj)
        $ImgObj.className += "ready";
}

And this is the CSS:

#myGallery{
    position:relative;
    width:400px;
    height:300px;
}
#myGallery img{
    display:none;
    position:absolute;
    top:0;
    left:0;
    border: 1px solid black;
}
#myGallery img.active{
    display:block;
}

So... is the declaration making IE9 go overly-standards-compliant and decide that fading isn't allowed under XHTML1.0 or something?

Another odd thing that also happens, once the fading is working: On only the first image switch, the displayed image dissapears immediately instead of fading out. All other images nicely fade in/out as required. Any thoughts on that too?

Thanks all.

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

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

发布评论

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

评论(4

小…红帽 2024-12-05 06:31:19

大卫:头部缺少标题并不是一个错误。事实上,HTML5 让我们跳过整个头部部分,它是有效的。根据官方 W3C HTML 规范,你只需要有一个 body 部分:) 所以我猜这是允许的最短的 HTML:

<html><body><h1>hello world</h1></body></html>

只是不确定是否需要 !doctype:)

主题问题是 - 某种程度上与 IE 中的 CSS 和显示属性有关,大多数情况来自IE6。如果将“display:none”设置为 HTML 元素,则不会得到 FadeIn 的结果,因为元素因“display:none”而未显示,并且 fadeIn 不会更改此状态。

我的解决方案是:像往常一样创建一个CSS类“隐藏”并设置为“display:none”。这行 JavaScript 代码在 document.load 上触发:

$('.hidden').fadeTo(0,0).css('visibility','visible');

在 fadeIn 之后 - 以及最终 fadeOut :) - 现在应该可以工作了。如果您仍然遇到任何问题 - 尝试使用 fadeTo 或不透明动画效果代替 fadeIn/fadeOut。

David: missing title in the head is NOT an error. In fact HTML5 let's skip whole head section and it is valid. According to official W3C HTML specification you only need to have a body section :) so i guess this is shortest HTML allowed:

<html><body><h1>hello world</h1></body></html>

just not sure about need of !doctype :)

the topic problem is - somehow releated to CSS and display property in IE, most cases from IE6. If you set "display:none" to an HTML element, you get no results with FadeIn as element is not displayed because of "display:none", and fadeIn does not change this state.

my solution is: make a CSS class "hidden" and set as "display:none" as usually. And this line of JavaScript code fired on document.load:

$('.hidden').fadeTo(0,0).css('visibility','visible');

after that fadeIn - and eventually fadeOut :) - shoud be working now. If you still get any issues - try to use fadeTo or opacity animation effect in place of fadeIn/fadeOut.

半城柳色半声笛 2024-12-05 06:31:19

删除 DOCTYPE 会使 IE 进入怪异模式,您可以通过按 F12 激活调试控制台,然后在屏幕底部选择怪异模式或兼容模式来实现相同的效果。

但我也有同样奇怪的行为。

由于某种原因,通过在调试模式下执行代码似乎可以更好地工作,或者如果您将行为附加到按钮,如果您单击它足够多,它最终会工作。

我认为淡入淡出行为是有问题的,并且仅在元素的背景中淡出,而不是在前景中淡出。 show() 表现出同样奇怪的行为。

在兼容性视图中一切都工作正常,尽管我不喜欢使用它。

Removing the DOCTYPE puts IE into quirks mode, you can achieve the same thing by pressing F12 to activate the debug console and then selecting quirks mode or compatibility mode on the bottom of the screen.

I am getting the same weird behaviour though.

It seems to work better by executing the code in debug mode for some reason, or if you attach the behaviour to a button, it will eventually work if you click it enough.

i think the fadein behaviour is buggy, and only fades in the background of the element and not the foreground. show() exhibits the same weird behaviour.

Everything works fine in compatibility view, as much as I dislike using it.

2024-12-05 06:31:19

我在 1.6.2 中也遇到了同样的问题。 1.6.4 的更新解决了这个问题。

I had the same issue with 1.6.2. An update to 1.6.4 solved it.

眼眸里的那抹悲凉 2024-12-05 06:31:19

如果您使用旧版本的 jquery,请升级到 1.6+ 版本。旧版本的 IE9 几乎没有问题。我希望这能解决这个问题。

If you are using older version of jquery please upgrade to 1.6+ versions. There were fews issues in IE9 with older versions. I hope this will fix the problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文