【问题标题】:jquery feature detection - how can I implement this?jquery 特征检测 - 我该如何实现呢?
【发布时间】:2011-02-12 02:30:32
【问题描述】:

我需要防止 IE 识别此插件中的淡入/淡出效果。如何在其中添加一行 jquery 特征检测代码:

$(function() {

var newHash      = "",
    $mainContent = $("#main-content"),
    $pageWrap    = $("#page-wrap"),
    baseHeight   = 0,
    $el;



$("nav#footer").delegate("a", "click", function() {
    window.location.hash = $(this).attr("href");
    return false;
});

$(window).bind('hashchange', function(){

    newHash = window.location.hash.substring(1);

    if (newHash) {
        $mainContent
            .find("#guts")
            .fadeOut(200, function() {
                $mainContent.show().load(newHash + " #guts", function() {
                    $mainContent.fadeIn(200, function() {
                    });
                    $("nav#footer a").removeClass("current");
                    $("nav#footer a[href="+newHash+"]").addClass("current");
                });
            });
    };

});

$(window).trigger('hashchange');

});

我有一些类似的代码

var FADE_TIME = 500; if(!($.support.opacity)) { FADE_TIME = 0}

$('element').fadeOut(FADE_TIME)

我应该在代码中的什么位置添加它?有人可以帮我实现这个工作吗?拜托!!

【问题讨论】:

  • 老实说。我需要做的就是将 support.opacity 代码添加到它上面的插件中。有jquery知识的人请帮助我:(

标签: javascript jquery plugins detection


【解决方案1】:

无法弄清楚为什么有人不只是编辑代码并回答问题。 您需要做的就是创建一个新变量,其值由 $.support.Opacity 属性确定,然后在代码的淡入/淡出部分中引用它。

$(function() {

var newHash      = "",
    $mainContent = $("#main-content"),
    $pageWrap    = $("#page-wrap"),
    baseHeight   = 0,
    $el,
    // new fadeTime property.
    fadeTime = $.support.opacity ? 500 : 0;



$("nav#footer").delegate("a", "click", function() {
    window.location.hash = $(this).attr("href");
    return false;
});

$(window).bind('hashchange', function(){

    newHash = window.location.hash.substring(1);

    if (newHash) {
        $mainContent
            .find("#guts")
            .fadeOut(fadeTime, function() {
                $mainContent.show().load(newHash + " #guts", function() {
                    $mainContent.fadeIn(fadeTime, function() {
                    });
                    $("nav#footer a").removeClass("current");
                    $("nav#footer a[href="+newHash+"]").addClass("current");
                });
            });
    };

});

$(window).trigger('hashchange');

});

【讨论】:

    【解决方案2】:

    如果用户不使用 Internet Explorer,您只是想运行一些东西?

    试试这个:

    if ($.browser.msie) {
        return false;
    } else {
        //do something
    }
    

    【讨论】:

    • $.browser.msie 可能不可靠,考虑使用特征检测。请参阅api.jquery.com/jQuery.browser 上的文档
    • 不,我知道我需要使用特征检测,但是如何将我粘贴在顶部的那些代码行添加到我的插件??????
    【解决方案3】:

    我想你说的是对 CSS opacity 属性的支持,而不是 IE8 及以下版本使用的有点错误的 -ms-filterfilter。在这种情况下,最好的方法是测试元素的style 对象中是否存在属性:

    if('opacity' in document.createElement('div').style) {
        // Do something that requires native opacity support
    }
    

    (如果你想测试多个属性,你可能需要缓存测试元素)

    【讨论】:

    • Hi Yi - 问题是当页面褪色时,IE 中的透明图像周围会出现黑色“绒毛”。我只是想防止 IE 褪色,只是从一个页面到另一个页面捕捉
    • @JeffreyJones 你看过mezzoblue.com/archives/2010/05/20/ie8_still_fa吗?
    猜你喜欢
    • 2016-01-11
    • 2011-01-15
    • 1970-01-01
    • 2017-07-25
    • 2013-07-02
    • 2015-08-07
    • 1970-01-01
    • 2021-11-14
    • 2015-10-19
    相关资源
    最近更新 更多