在调整大小事件上禁用 superfish

发布于 2024-11-25 07:50:16 字数 1090 浏览 2 评论 0原文

我正在尝试将 Superfish jQuery 插件与 Nathan Smith 的 adjustment.js 片段结合起来,该片段根据浏览器宽度动态加载不同的 CSS 文件。我想在移动模式下禁用/替换 Superfish 菜单,因为下拉菜单在那里没有任何意义。我尝试检测更改并禁用菜单,但我需要在窗口再次调整宽度时重新启用它。

这就是我所拥有的:

function htmlId(i, width) {
    document.documentElement.id = 'pagesize_' + i;
}

var ADAPT_CONFIG = {
  path: '/css/',
  dynamic: true,
  callback: htmlId,
  range: [
    '0px    to 760px  = mobile.css',
    '760px  = 960_12.css'
  ]
};

function sfMenu() {
    $("#pagesize_1 ul.sf-menu").superfish({ 
        delay:       800,
        animation:   {opacity:'show'},
        speed:       'fast',
        autoArrows:  true,
        dropShadows: true
    }); 
}

$(document).ready(function(){
    sfMenu();
});

基本原理是更改 resize 上 html 元素的 id(在 pagesize_0pagesize_1 之间 - 有效)并使用 CSS 中的后代选择器来禁用菜单,但这不起作用。我尝试在 resize 上重新运行 sfMenu() (上面未显示代码),但它似乎没有检查更改后的 DOM,意识到 pagesize_1不再存在,然后优雅地失败(我认为这会达到我想要的效果)。

有什么想法吗?理想情况下,我想销毁 resize-to-mobile 上的 superfish 功能,然后在屏幕再次变大时重新设置它。

I'm trying to combine the Superfish jQuery plugin with Nathan Smith's adapt.js snippet, which dynamically loads in different CSS files depending on browser width. I want to disable/replace/something the Superfish menu when in mobile mode, because drop-downs don't make any sense there. I've attempted to detect the change and disable the menu, but I need it to re-enable when the window is resized wide again.

Here's what I have:

function htmlId(i, width) {
    document.documentElement.id = 'pagesize_' + i;
}

var ADAPT_CONFIG = {
  path: '/css/',
  dynamic: true,
  callback: htmlId,
  range: [
    '0px    to 760px  = mobile.css',
    '760px  = 960_12.css'
  ]
};

function sfMenu() {
    $("#pagesize_1 ul.sf-menu").superfish({ 
        delay:       800,
        animation:   {opacity:'show'},
        speed:       'fast',
        autoArrows:  true,
        dropShadows: true
    }); 
}

$(document).ready(function(){
    sfMenu();
});

The rationale was to change the id of the html element on resize (between pagesize_0 and pagesize_1 - which works) and to use descendent selectors in CSS to disable the menu, but that doesn't work. I tried rerunning sfMenu() on resize (code not shown above), but it doesn't seem to inspect the changed DOM, realise pagesize_1 no longer exists, then fail gracefully (which I think would achieve the effect I'm after).

Any thoughts? Ideally I'd like to destroy the superfish function on resize-to-mobile, then re-instate it when the screen is large again.

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

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

发布评论

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

评论(2

‖放下 2024-12-02 07:50:16

SuperFish 有一个“销毁”方法(当然在最新的 1.7.3 版本中),您可以根据屏幕大小调用该方法来禁用它,然后使用 CSS 媒体查询重新设置导航样式。当您想再次启用 SuperFish 时,您也可以调用 SuperFish 的“init”方法:

var sf, body;
var breakpoint = 600;
jQuery(document).ready(function($) {
    body = $('body');
    sf = $('ul.sf-menu');
    if(body.width() >= breakpoint) {
      // enable superfish when the page first loads if we're on desktop
      sf.superfish();
    }
    $(window).resize(function() {
        if(body.width() >= breakpoint && !sf.hasClass('sf-js-enabled')) {
            // you only want SuperFish to be re-enabled once (sf.hasClass)
            sf.superfish('init');
        } else if(body.width() < breakpoint) {
            // smaller screen, disable SuperFish
            sf.superfish('destroy');
        }
    });
});

这有望解释我正在谈论的内容:)

http: //cdpn.io/jFBtw

SuperFish has a 'destroy' method (certainly in latest 1.7.3 version) that you could call depending on screen size to disable it and then re-style the navigation using CSS media queries. You could also then call the 'init' method of SuperFish when you wanted to enable it again:

var sf, body;
var breakpoint = 600;
jQuery(document).ready(function($) {
    body = $('body');
    sf = $('ul.sf-menu');
    if(body.width() >= breakpoint) {
      // enable superfish when the page first loads if we're on desktop
      sf.superfish();
    }
    $(window).resize(function() {
        if(body.width() >= breakpoint && !sf.hasClass('sf-js-enabled')) {
            // you only want SuperFish to be re-enabled once (sf.hasClass)
            sf.superfish('init');
        } else if(body.width() < breakpoint) {
            // smaller screen, disable SuperFish
            sf.superfish('destroy');
        }
    });
});

This should hopefully explain what I'm talking about :)

http://cdpn.io/jFBtw

绝影如岚 2024-12-02 07:50:16

我一直在玩同样的事情,从水平导航栏样式(窗口比子导航宽)到垂直下拉样式(子导航比窗口宽)到简单的 ol-list (主导航比窗口宽) 。

不确定它有多优雅,但最终 unbind() 和 removeAttr('style') 为我禁用了下拉菜单:

$(window).resize(function() {
    if ($(this).width() < maxNavigationWidth) {
        $('#neck .navigation').removeClass('sf-menu');
        $('.navigation li').unbind();
        $('.navigation li ul').removeAttr('style');
    } else {
        $('#neck .navigation').addClass('sf-menu').addClass('sf-js-enabled');
        applySuperfish();
    }
});

I've been playing around with the same thing, going from horizontal nav-bar style (window wider than subnav) to vertical drop-down style (subnav wider than window) to just plain-ol-list (main nav wider than window).

Not sure how elegant it is, but in the end unbind() and removeAttr('style') disabled the dropdowns for me:

$(window).resize(function() {
    if ($(this).width() < maxNavigationWidth) {
        $('#neck .navigation').removeClass('sf-menu');
        $('.navigation li').unbind();
        $('.navigation li ul').removeAttr('style');
    } else {
        $('#neck .navigation').addClass('sf-menu').addClass('sf-js-enabled');
        applySuperfish();
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文