Jquery window.width if / doesnt工作

时间:2015-11-19 11:41:04

标签: jquery scroll window width

有人可以说明为什么这个脚本无效吗?此脚本尝试根据窗口宽度滚动后添加类。我不知道我在哪里弄错了。任何人吗?

$(document).ready(function () {
    var nav = $('nav');
    var ul = $('ul.navigation li');
    var width = $(window).width();

    function nav() {
        if (width < 700) {
            navbar1();
        } else {
            navbar2();
        }
    };

    function navbar1() {
        $(window).scroll(function () {
            var scroll = $(window).scrollTop();

            if (scroll > 10) {
                nav.addClass('scroll');
                ul.addClass('scroll');
            } else {
                nav.removeClass('scroll');
                ul.removeClass('scroll');
            }
        });
    }

    function navbar2() {
        $(window).scroll(function () {
            var scroll = $(window).scrollTop();
            if (scroll > 10) {
                nav.addClass('scroll');
            } else {
                nav.removeClass('scroll');
            }
        });
    }
});

1 个答案:

答案 0 :(得分:0)

这里是:https://jsfiddle.net/ggozdyra/hLazq8c7/

$(document).ready(function() {
    var nav = $('nav');
    var ul = $('ul.navigation li');
    var width = $(window).width();

function kupa(){

   if ( width < 700) {  
        navbar1();
   } else {
        navbar2();
   };  
};

function navbar1() {
    $(window).scroll(function() {
        var scroll = $(window).scrollTop();

        if ( scroll > 10) {
            nav.addClass('scroll');
            ul.addClass('scroll');
        } else {
            nav.removeClass('scroll');
            ul.removeClass('scroll');
        }
    });
}

function navbar2() {
    $(window).scroll(function() {
        var scroll = $(window).scrollTop();
        if ( scroll > 10) {
            nav.addClass('scroll');
        } else {
            nav.removeClass('scroll');
        }
    });
  }

});