【问题标题】:JQuery Animation doesn't work in Internet ExplorerJQuery 动画在 Internet Explorer 中不起作用
【发布时间】:2011-12-09 12:30:20
【问题描述】:

我在让这个 JQuery 在 Internet Explorer 中运行时遇到了一些麻烦,它在 FF、WebKit 等中运行良好,可以向上滑动、弹跳和回落到原位,但在 Internet Explorer 中它什么也不做,按钮不会'不起作用,页面加载时内容永远不会向上滑动。真的很奇怪,我在网站的早期设计中有一个元素淡出,它在 IE 中不起作用,但在其他所有东西中都起作用......

<script>

$(document).ready(function(){
           $('#homecontent').delay("750").animate({ marginTop: "-15px" }, 1500).animate({ marginTop: "5px" }, 500);
})

</script>


<script>
$("#shop").click(function(){
           $('#homecontent').animate({ marginTop: "1500px" }, 1500).delay("1500", function(){
           window.location.href = "http://www.jamiedurham.co.uk/shop/","shop"      
  });
})
$("#blog").click(function(){
           $('#homecontent').animate({ marginTop: "1500px" }, 1500).delay("1500", function(){
           window.location.href = "http://www.jamiedurham.co.uk/blog/","blog"      
  });
})
</script>


<div id="homecontent" style="width:1000px; margin-left:20px; margin-right:20px; position:absolute; margin-top:1500px; line-height:19px;"></div>

<table style="margin-top:70px;">
    <tr style="height:50px; width:738px;">
         <td style="width:242px;"><a href="http://www.jamiedurham.co.uk/" ><img src="http://www.jamiedurham.co.uk/pics/homehover.gif" alt="home" name="home" id="home"></a></td>
         <td style="width:242px;"><a href="http://www.jamiedurham.co.uk/shop" id="shop" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('shop','','http://www.jamiedurham.co.uk/pics/partnershover.gif',1)"><img src="http://www.jamiedurham.co.uk/pics/shop.gif" alt="shop" name="shop" id="shop"></a></td>
         <td style="width:242px;"><a id="blog" href="http://www.jamiedurham.co.uk/blog" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('blog','','http://www.jamiedurham.co.uk/pics/bloghover.gif',1)"><img src="http://www.jamiedurham.co.uk/pics/blog.gif" alt="blog" name="blog" id="blog"></a></td>     
    </tr>
</table>

【问题讨论】:

  • 你在什么版本的IE中测试过代码?
  • IE 8 和 9(如果这些不支持的话,我无法想象早期版本会工作......)
  • 当我在不同浏览器中使用 jQuery 时遇到问题,似乎是因为我没有在文档开头指定 DocType。可能值得先检查一下:)
  • jsfiddle.net/QZc5q(您的代码)在 ie9 和 ie9-mode 的 ie7-mode 中为我工作
  • 感谢大家的帮助,传出的 IE 只是不喜欢我链接到 jQuery 文件夹的方式,只要我把它移到我的服务器上它就可以完美运行,真的很奇怪......

标签: jquery internet-explorer animation fade slide


【解决方案1】:

http://jsfiddle.net/koolvin/MXwXA/5/ 这个已经在所有 IE 版本中测试过了,它可以在 IE6+ 中运行

我做了三件事:

  1. 我让它看起来不错
  2. 我以;结束了这些陈述
  3. 我添加了 e.preventDefault() 以确保您的 javascript 按预期工作。

原来是这样的:

$(document).ready(function() {
    $('#homecontent').delay("750").animate({
        marginTop: "-15px"
    }, 1500).animate({
        marginTop: "5px"
    }, 500);
});
$("#shop").click(function(e) {
    e.preventDefault();
    $('#homecontent').animate({
        marginTop: "1500px"
    }, 1500).delay("1500", function() {
        window.location.href = "http://www.jamiedurham.co.uk/shop/", "shop"
    });
});
$("#blog").click(function(e) {
    e.preventDefault();
    $('#homecontent').animate({
        marginTop: "1500px"
    }, 1500).delay("1500", function() {
        window.location.href = "http://www.jamiedurham.co.uk/blog/", "blog"
    });
});

【讨论】:

    【解决方案2】:

    我认为您的 HTML 中有一些错误。您已经在 a 和 img 标签上指定了 id="shop" 和 id="blog"。从 img 标签中删除它。

    试试这个新的 Javascript 代码。点击动作没有被执行,通过添加 event.preventDefault(),标准的点击事件将不会执行,用户只会在 window.location 发生时被重定向。

        <script>
    $(document).ready(function(){
       $('#homecontent').delay("750").animate({ marginTop: "-15px" }, 1500).animate({ marginTop: "5px" }, 500);
    
    $("#shop").click(function(event){
            event.preventDefault();
           $('#homecontent').animate({ marginTop: "1500px" }, 1500).delay("1500", function(){
                window.location.href = "http://www.jamiedurham.co.uk/shop/","shop"
            });
    });
    $("#blog").click(function(event){
        event.preventDefault();
       $('#homecontent').animate({ marginTop: "1500px" }, 1500).delay("1500", function(){
           window.location.href = "http://www.jamiedurham.co.uk/blog/","blog"
        });
    });
    });
    </script>
    

    【讨论】:

    • 给 img 标签一个 ID 不会破坏 IE8/9 中的 JS
    • id 被附加到 img 和标签上,所以我确定 jquery 被混淆了。
    • 我明白你的推理,但是 jQuery 是为处理具有相同属性的多个元素而构建的,这是它的主要功能之一。它所做的只是将事件处理程序附加到每个选定的对象。
    • 似乎只是 jQuery 文件在 CDN IE 上的链接方式不喜欢,将文件移动到我的服务器并且现在可以正常工作了......不过感谢所有建议!
    猜你喜欢
    • 1970-01-01
    • 2011-08-16
    • 1970-01-01
    • 2015-11-05
    • 2012-06-07
    • 2019-08-12
    • 1970-01-01
    • 2014-10-12
    • 1970-01-01
    相关资源
    最近更新 更多