我可以根据窗口大小执行javascript吗? [英] Can I execute javascript based on window size?

查看:63
本文介绍了我可以根据窗口大小执行javascript吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自适应网站,我正在使用javascript创建一个粘性边栏。

I have a responsive site where I'm using a javascript to create a sticky sidebar.

我还使用媒体查询来更改多列当浏览器大小小于768px时,布局为单列布局。

I'm also using media queries to change from a multi-column layout to a single-column layout when the browser size is less than 768px.

我需要弄清楚如何在单列布局中禁用粘性菜单脚本。基本上,我需要类似于脚本语句的媒体查询。

I need to figure out how to disable the sticky menu script in the single-column layout. Essentially, I need something like a media query for the script statement.

这是我用来启用脚本的代码:

This is the code I'm using to enable the script:

<script>
jQuery('#info').containedStickyScroll({
        duration: 0,
        unstick: false
    });
</script>

我可以添加一些内容,只有在窗口宽度为768px或更宽时才触发它?

Is there something I can add to it to only have it trigger if the window is 768px wide or wider?

编辑:我正在寻找一种解决方案,如果用户动态调整窗口大小,该解决方案将起作用。

I'm looking for a solution that will work if the user resizes the window on the fly.

推荐答案

试试这个。

$(function(){
     $(window).resize(function(){
         if($(this).width() >= 768){
             jQuery('#info').containedStickyScroll({
                 duration: 0,
                 unstick: false
             });
         }
      })
      .resize();//trigger resize on page load
});

这篇关于我可以根据窗口大小执行javascript吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆