繁体   English   中英

如何在调整大小事件上触发我的jQuery脚本

[英]How do I trigger my jQuery script on resize event

我有一个jQuery脚本,它将我的菜单重新组织成一个select表单元素,它工作正常。

我想在.resize()事件上触发它,但无法正常工作。 我该怎么办?

 $(function () {
    $(window).resize(function () {
        /* Get the window's width, and check whether it is narrower than 580 pixels */
        var windowWidth = $(window).width();
        if (windowWidth <= 580) {

            /* Clone our navigation */
            var mainNavigation = $('nav.main-navigation').clone();

            /* Replace unordered list with a "select" element to be populated with options, and create a variable to select our new empty option menu */
            $('nav.main-navigation').html('<select class="menu"></select>');
            var selectMenu = $('select.menu');

            /* Navigate our nav clone for information needed to populate options */
            $(mainNavigation).children('ul').children('li').each(function () {

                /* Get top-level link and text */
                var href = $(this).children('a').attr('href');
                var text = $(this).children('a').text();

                /* Append this option to our "select" */
                $(selectMenu).append('<option value="' + href + '">' + text + '</option>');

                /* Check for "children" and navigate for more options if they exist */
                if ($(this).children('ul').length > 0) {
                    $(this).children('ul').children('li').each(function () {

                        /* Get child-level link and text */
                        var href2 = $(this).children('a').attr('href');
                        var text2 = $(this).children('a').text();

                        /* Append this option to our "select" */
                        $(selectMenu).append('<option value="' + href2 + '">--- ' + text2 + '</option>');
                    });
                }
            });

        }

        /* When our select menu is changed, change the window location to match the value of the selected option. */
        $(selectMenu).change(function () {
            location = this.options[this.selectedIndex].value;
        });
    });
});

好吧,试一下以resize!

$("#div").resize(function(e){
   // do something when #div element resize
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM