媒体查询/matchMedia上的脚本

Script on media query/matchMedia

本文关键字:脚本 matchMedia 查询 媒体      更新时间:2023-09-26

我有一个脚本(https://github.com/SubZane/flyPanels)只能在小型设备上执行。

脚本由调用:

<script>
document.addEventListener("DOMContentLoaded", function(event) {
    FastClick.attach(document.body);
    flyPanels.init({
            treeMenu: {
                init: true
            },
            search: {
                init: true,
                saveQueryCookie: true
            }
    })
});
</script>

我该如何做到这一点?

请参阅此链接

所以基本上你可以通过像这样的if语句进行检查

if(screen.width == 768){
    //your code here
}

然后将您的事件代码放在它下面。只需询问您希望代码工作的屏幕大小并检查即可。附言:js的屏幕对象的规格是px。。。。所以一定要签入px。