小编Sus*_*ium的帖子

达到CSS断点时触发事件

我有一组断点,我想在每次传递一个事件时触发一个事件。目前,我正在使用$(document).resize(function(){}),但是这不符合我的CSS断点我是否使用匹配windowdocument或任何其他选择。

有什么方法可以仅检测何时通过媒体查询吗?这是我当前的代码:

$( window ).resize(
    function() {
        if( $(window).width() < 500 ) {
            $(window).trigger("breakpoint-sm");
        }
        if( $(window).width() < 900 ) {
            $(window).trigger("breakpoint-md");
        }
    }
);

$(window).on(
    "breakpoint-md", function() {
        if($(window).width() < 900) {
            // this happens when below medium screen size
            alert( "breakpoint reached" );
        }
    }
);
Run Code Online (Sandbox Code Playgroud)
@media screen and (max-width: 500px) {
    /* do mobile things */
}
@media screen and (max-width: 900px) {
    /* do mobile things */
}
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> …
Run Code Online (Sandbox Code Playgroud)

html javascript css jquery breakpoints

4
推荐指数
1
解决办法
394
查看次数

标签 统计

breakpoints ×1

css ×1

html ×1

javascript ×1

jquery ×1