简体   繁体   English

我在某些屏幕尺寸上禁用了某些jQuery脚本,但是如何在调整屏幕尺寸时检查它?

[英]I Disabled certain jQuery script at certain screen size but how do I get it check when screen is resized?

I got this to disable on screen sizes below 1020 but any idea how to get it to check dynamically as the browser window is being resized?: 我在1020以下的屏幕尺寸上禁用了此功能,但是不知道如何在调整浏览器窗口大小时使其动态检查吗?:

<script>
$(document).ready(function() {

/*Set height of sections to window height*/
$( "#homepage-fold" ).each(function(){
if ($(window).width() >= 1020) {
    var $this = $(this);}
    $this.css({'height':($(window).height())+'px'});

    /*Recalculate on window resize*/
$(window).resize(function(){
        $this.css({'height':($(window).height())+'px'});
    });
 });

 });</script>

Any idea how to get the if statement I added to work dynamically as the browser's being resized. 任何想法如何使我添加的if语句在调整浏览器大小时都能动态工作。 i tried wrapping that first if tag in the second Recalculate section but I must not have put it in the correct spot. 我尝试在第二个“ 重新计算”部分中将第一个if标签包装起来,但是我一定不能将其放在正确的位置。

Thanks!! 谢谢!!

-- -

First off, this seems to be a simple solution but the solution I tried didn't work. 首先,这似乎是一个简单的解决方案,但我尝试过的解决方案没有用。 I'm newer to jQuery so I'm guessing it was an error in my syntax - probably improper placement of this rule. 我是jQuery的新手,所以我猜测这是我的语法错误-可能是该规则的放置不当。 If somebody could help me figure out how to get this to work I'd greatly appreciate it. 如果有人可以帮助我弄清楚如何使它工作,我将不胜感激。 I only want this script to be active on screen sizes greater than 1020px. 我只希望此脚本在大于1020px的屏幕上有效。 Screens below 1020 should see the page without this active script. 1020以下的屏幕应该看到没有此活动脚本的页面。

   <script>
   $(document).ready(function() {

    /*Set height of sections to window height*/
    $( "#homepage-fold" ).each(function(){
        var $this = $(this);
        $this.css({'height':($(window).height())+'px'});

        /*Recalculate on window resize*/
        $(window).resize(function(){
            $this.css({'height':($(window).height())+'px'});
        });
    });

});</script>

I thought by adding 我想通过添加

    if(screen.width >= 1020){

Like this: $(document).ready(function() { 像这样:$(document).ready(function(){

    /*Set height of sections to window height*/
    $( "#homepage-fold" ).each(function(){

if(screen.width >= 1020){ if(screen.width> = 1020){

        var $this = $(this);
        $this.css({'height':($(window).height())+'px'});

        /*Recalculate on window resize*/
        $(window).resize(function(){
            $this.css({'height':($(window).height())+'px'});
        });

Added: }); 添加: }); }); });

});</script>

Any thoughts on where I messed up this code? 我在哪里弄乱了这段代码有什么想法? Thank you!! 谢谢!!

This would be my approach. 这就是我的方法。 The setFold function sets the height of the desired element to the window's height or - if the window's width is smaller than the breakpoint (1020) to a default value (999). setFold函数将所需元素的高度设置为窗口的高度,或者-如果窗口的宽度小于断点(1020),则将其设置为默认值(999)。 I've also wrapped the onresize callback into a timeout which delays its execution until the resize event has actually ended (thus making it a onresizeend really). 我还将onresize回调包装为一个超时,该超时将延迟其执行,直到resize事件实际结束为止(因此实际上使它成为一个onresizeend )。 This sometimes is a good idea because resize is fired multiple times while someone resizes the window. 有时这是一个好主意,因为在有人调整窗口resize会多次触发调整大小。 Depending on what is done onresize this can cause quite a lag and in most cases it's totally ok for it to just happen once when the user stopped resizing (you can play around with callbackDelay here or just remove the timeout stuff completely). 根据onresize这可能会导致相当大的滞后,并且在大多数情况下,仅在用户停止调整大小时发生一次是完全可以的(您可以在此处使用callbackDelay或完全删除超时内容)。

$(document).ready(function(){

    // DOM READY
    var $window    = $(window), // cached
        $fold      = $('#homepage-fold'),
        breakpoint = 1020,
        defaultHeight = 999,
        resizeTimeout,
        callbackDelay = 200; // ms


    function setFold(){
        if( $window.width() < breakpoint ){
            $fold.css('height', defaultHeight );
        }
        else {
            $fold.css('height', $window.height() );
        }
    }

    setFold(); // initial setting

    // Attach event
    $window.on('resize', function(){
        clearTimeout( resizeTimeout );
        resizeTimeout = setTimeout(function(){
            setFold();
        }, callbackDelay );
    })

});

Do it like this: 像这样做:

if ($(window).width() >= 1020) {

}

Also, you're probably going to want to write this into a resize function as well so that if somebody resizes their browser window the script will fire or not fire based on the resized dimensions instead of just the initial dimensions. 另外,您可能还希望将其写入调整大小函数,以便如果有人调整浏览器窗口的大小,脚本将根据调整大小而不是仅基于初始尺寸触发或不触发。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM