Twitter Bootstrap - how to detect when media queries starts

后端 未结 16 758
抹茶落季
抹茶落季 2020-12-14 02:19

which is the fastest and easy way to fire when bootstrap-responsive.css media queries go in action?

go in action = when you resize window to mobile width and site is

16条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-14 02:48

    I have prepered a super lightweight library to deal with events fired when window width and Bootstrap breakpoint is changed - responsive-breakpoint-tester

    var viewport = new ResponsiveTester();
    
    $('body').on('in.screen.xs', function(event, devices) {
        // Code executed when viewport is was changed to xs
    });
    
    $('body').on('out.screen.xs', function(event, devices) {
        // Code executed when viewport is no longer xs
    });
    

    Other features like current breakpoint check are also included:

    if (viewport.is('xs')) {
        // Executed only on xs breakpoint
    }
    
    if (viewport.is('!=xs')) {
        // Executed on all breakpoints that are not equal xs (sm, md, lg)
    }
    
    if (viewport.is('md')) {
        // Executed on breakpoints that are larger than md (lg)
    }
    

    Bootstrap 4 and Foundation configuraions are supported, more info on GitHub Repository

提交回复
热议问题