jQuery: How to detect window width on the fly?

后端 未结 4 549
栀梦
栀梦 2020-11-27 12:27

I have a scrolling element on my page (with the jScrollPane jQuery plugin). What I want to accomplish is a way to turn off the scrolling window by detecting the width of the

相关标签:
4条回答
  • 2020-11-27 12:50

    Put your if condition inside resize function:

    var windowsize = $(window).width();
    
    $(window).resize(function() {
      windowsize = $(window).width();
      if (windowsize > 440) {
        //if the window is greater than 440px wide then turn on jScrollPane..
          $('#pane1').jScrollPane({
             scrollbarWidth:15, 
             scrollbarMargin:52
          });
      }
    });
    
    0 讨论(0)
  • 2020-11-27 12:52

    Changing a variable doesn't magically execute code within the if-block. Place the common code in a function, then bind the event, and call the function:

    $(document).ready(function() {
        // Optimalisation: Store the references outside the event handler:
        var $window = $(window);
        var $pane = $('#pane1');
    
        function checkWidth() {
            var windowsize = $window.width();
            if (windowsize > 440) {
                //if the window is greater than 440px wide then turn on jScrollPane..
                $pane.jScrollPane({
                   scrollbarWidth:15, 
                   scrollbarMargin:52
                });
            }
        }
        // Execute on load
        checkWidth();
        // Bind event listener
        $(window).resize(checkWidth);
    });
    
    0 讨论(0)
  • 2020-11-27 13:02

    I dont know if this useful for you when you resize your page:

    $(window).resize(function() {
           if(screen.width == window.innerWidth){
               alert("you are on normal page with 100% zoom");
           } else if(screen.width > window.innerWidth){
               alert("you have zoomed in the page i.e more than 100%");
           } else {
               alert("you have zoomed out i.e less than 100%");
           }
        });
    
    0 讨论(0)
  • 2020-11-27 13:05

    Below is what i did to hide some Id element when screen size is below 768px, and show up when is above 768px. It works great.

    var screensize= $( window ).width();
    
    if(screensize<=768){
            if($('#column-d0f6e77c699556473e4ff2967e9c0251').length>0)
                {
                    $('#column-d0f6e77c699556473e4ff2967e9c0251').css('display','none');
                }
    }
    else{
            if($('#column-d0f6e77c699556473e4ff2967e9c0251').length>0)
                {
                    $('#column-d0f6e77c699556473e4ff2967e9c0251').removeAttr( "style" );
                }
    
    }
    changething = function(screensize){
            if(screensize<=768){
                if($('#column-d0f6e77c699556473e4ff2967e9c0251').length>0)
                {
                    $('#column-d0f6e77c699556473e4ff2967e9c0251').css('display','none');
                }
            }
            else{
            if($('#column-d0f6e77c699556473e4ff2967e9c0251').length>0)
                {
                    $('#column-d0f6e77c699556473e4ff2967e9c0251').removeAttr( "style" );
                }
    
            }
    }
    $( window ).resize(function() {
     var screensize= $( window ).width();
      changething(screensize);
    });
    
    0 讨论(0)
提交回复
热议问题