복붙노트

[JQUERY] 화면 너비 미만 960 픽셀 경우 무언가를

JQUERY

화면 너비 미만 960 픽셀 경우 무언가를

해결법


  1. 1.사용 jQuery를 윈도우의 폭을 얻을 수 있습니다.

    사용 jQuery를 윈도우의 폭을 얻을 수 있습니다.

    if ($(window).width() < 960) {
       alert('Less than 960');
    }
    else {
       alert('More than 960');
    }
    

  2. 2.당신은 크기 조정 이벤트와 결합 할 수 있습니다 :

    당신은 크기 조정 이벤트와 결합 할 수 있습니다 :

     $(window).resize(function() {
      if ($(window).width() < 960) {
         alert('Less than 960');
      }
     else {
        alert('More than 960');
     }
    });
    

    R.J의 경우 :

    var eventFired = 0;
    
    if ($(window).width() < 960) {
        alert('Less than 960');
    
    }
    else {
        alert('More than 960');
        eventFired = 1;
    }
    
    $(window).on('resize', function() {
        if (!eventFired) {
            if ($(window).width() < 960) {
                alert('Less than 960 resize');
            } else {
                alert('More than 960 resize');
            }
        }
    });
    

    나는 eventFired 플래그와 함께 갔다 그래서 나는 성공하지 http://api.jquery.com/off/을 시도했다.


  3. 3.나는 그런 일을 위해 jQuery를 사용하고 window.innerWidth 진행하지 않는 것이 좋습니다

    나는 그런 일을 위해 jQuery를 사용하고 window.innerWidth 진행하지 않는 것이 좋습니다

    if (window.innerWidth < 960) {
        doSomething();
    }
    

  4. 4.또한 자바 스크립트로 미디어 쿼리를 사용할 수 있습니다.

    또한 자바 스크립트로 미디어 쿼리를 사용할 수 있습니다.

    const mq = window.matchMedia( "(min-width: 960px)" );
    
    if (mq.matches) {
           alert("window width >= 960px");
    } else {
         alert("window width < 960px");
    }
    

  5. 5.

    // Adds and removes body class depending on screen width.
    function screenClass() {
        if($(window).innerWidth() > 960) {
            $('body').addClass('big-screen').removeClass('small-screen');
        } else {
            $('body').addClass('small-screen').removeClass('big-screen');
        }
    }
    
    // Fire.
    screenClass();
    
    // And recheck when window gets resized.
    $(window).bind('resize',function(){
        screenClass();
    });
    

  6. 6.나는 (jQuery를 필요) 제안 :

    나는 (jQuery를 필요) 제안 :

    /*
     * windowSize
     * call this function to get windowSize any time
     */
    function windowSize() {
      windowHeight = window.innerHeight ? window.innerHeight : $(window).height();
      windowWidth = window.innerWidth ? window.innerWidth : $(window).width();
    
    }
    
    //Init Function of init it wherever you like...
    windowSize();
    
    // For example, get window size on window resize
    $(window).resize(function() {
      windowSize();
      console.log('width is :', windowWidth, 'Height is :', windowHeight);
      if (windowWidth < 768) {
        console.log('width is under 768px !');
      }
    });
    

    CodePen에 추가 : http://codepen.io/moabi/pen/QNRqpY?editors=0011

    그럼 당신은 쉽게 VAR와 윈도우의 폭을 얻을 수 있습니다 : windowWidth를 과와 높이 : windowHeight

    그렇지 않으면 JS 라이브러리를 얻을 : http://wicky.nillia.ms/enquire.js/


  7. 7.사용하다

    사용하다

    $(window).width()
    

    또는

    $(document).width()
    

    또는

    $('body').width()
    

  8. 8.나는이 답변을 늦어서 알고,하지만 난이 비슷한 문제를 가지고 누구에게 어떤 도움 바랍니다. 또한 어떤 이유로 든 경우 페이지 새로 고침을 작동합니다.

    나는이 답변을 늦어서 알고,하지만 난이 비슷한 문제를 가지고 누구에게 어떤 도움 바랍니다. 또한 어떤 이유로 든 경우 페이지 새로 고침을 작동합니다.

    $(document).ready(function(){
    
    if ($(window).width() < 960 && $(window).load()) {
            $("#up").hide();
        }
    
        if($(window).load()){
            if ($(window).width() < 960) {
            $("#up").hide();
            }
        }
    
    $(window).resize(function() {
        if ($(window).width() < 960 && $(window).load()) {
            $("#up").hide();
        }
        else{
            $("#up").show();
        }
    
        if($(window).load()){
            if ($(window).width() < 960) {
            $("#up").hide();
            }
        }
        else{
            $("#up").show();
        }
    
    });});
    

  9. 9.이 코드를 사용해보십시오

    이 코드를 사용해보십시오

    if ($(window).width() < 960) {
     alert('width is less than 960px');
    }
    else {
     alert('More than 960');
    }
    

    경우 ($ (창) .width () <960) { ( '960보다 작은 폭') 경고; } 다른 { 경고 ( '이상 960'); } <스크립트 SRC = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">


  10. 10.아니,이 중 어느 것도 작동하지 않습니다. 당신이 필요한 것은 이것이다!

    아니,이 중 어느 것도 작동하지 않습니다. 당신이 필요한 것은 이것이다!

    이 시도:

    if (screen.width <= 960) {
      alert('Less than 960');
    } else if (screen.width >960) {
      alert('More than 960');
    }
    
  11. from https://stackoverflow.com/questions/7715124/do-something-if-screen-width-is-less-than-960-px by cc-by-sa and MIT license