在IE中停止JS

时间:2015-04-04 15:58:15

标签: javascript internet-explorer cross-browser

如何阻止以下脚本在IE8 / 9中运行?出于某种原因,有时候这些部分会逐渐消失,有时候并不是这样,我宁愿在IE8 / 9中将其关闭。

$(document).ready(function() {

    /* Every time the window is scrolled ... */
    $(window).scroll( function(){

        /* Check the location of each desired element */
        $('.hideme').each( function(i){

            var bottom_of_object = $(this).position().top + $(this).outerHeight();
            var bottom_of_window = $(window).scrollTop() + $(window).height();

            /* If the object is completely visible in the window, fade it it */
            if( bottom_of_window > bottom_of_object ){

                $(this).animate({'opacity':'1'},1500);

            }

        }); 

    });

});

1 个答案:

答案 0 :(得分:0)

你可以使用这个功能

function IEVersion() {
    var userAgent = navigator.userAgent.toLowerCase();
    var msie = userAgent.indexOf("msie ");

    if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./)) // If Internet Explorer, return version number
           return parseFloat((userAgent.match(/.*(?:rv|ie)[\/: ](.+?)([ \);]|$)/) || [])[1]);

   return false;
}

要检测MS IE版本,您可以在Microsoft支持网站下面找到详细信息:

http://support.microsoft.com/en-us/kb/167820

您必须将其与您的代码一起使用,如下所示

function IEVersion() {
    var userAgent = navigator.userAgent.toLowerCase();
    var msie = userAgent.indexOf("msie ");

    if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./)) // If Internet Explorer, return version number
           return parseFloat((userAgent.match(/.*(?:rv|ie)[\/: ](.+?)([ \);]|$)/) || [])[1]);

   return false;
}

$(document).ready(function() {

    /* Every time the window is scrolled ... */
    var ie = IEVersion();
    if(ie == false or (ie != 8 and ie != 9))
         your_code();

    function your_code() {
    $(window).scroll( function(){

        /* Check the location of each desired element */
        $('.hideme').each( function(i){

            var bottom_of_object = $(this).position().top + $(this).outerHeight();
            var bottom_of_window = $(window).scrollTop() + $(window).height();

            /* If the object is completely visible in the window, fade it it */
            if( bottom_of_window > bottom_of_object ){

                $(this).animate({'opacity':'1'},1500);

            }

        }); 

    });
   }

});