How to use javascript conditionally like CSS3 media queries, orientation?

前端 未结 6 1398
猫巷女王i
猫巷女王i 2020-11-29 19:37

How to use javascript conditionally like CSS3 media queries, orientation?

For Example I can write css for specific

@media only screen and (width : 10         


        
相关标签:
6条回答
  • 2020-11-29 20:19

    I can think of a quick solution: Apply some styles conditionally to an invisible div, and check if they are applied with javascript:

    div#test { display: none }
    @media only screen and (width : 1024px) and (orientation : landscape) {
        div#test { background-color: white; }
    }
    
    if(document.getElementById('test').style.backgroundColor == 'white')
        mediaSelectorIsActive();
    
    0 讨论(0)
  • 2020-11-29 20:24

    ...I want to run a javascript only if max-width of browser is 1900px and min-width is 768

    EDIT: Actually, that was all wrong. If you're using a mobile device, use:

    function doStuff(){
        landscape = window.orientation? window.orientation=='landscape' : true;
    
        if(landscape && window.innerWidth<1900 && window.innerWidth > 768){
            //code here
        }
    }
    window.onload=window.onresize=doStuff;
    if(window.onorientationchange){
        window.onorientationchange=doStuff;
    }
    
    0 讨论(0)
  • 2020-11-29 20:26

    The simplest way I found is to get the width of our page; then to conditionally use it.

    var x = document.documentElement.clientWidth;
    if (x < 992)            
            document.body.style.backgroundColor = "yellow";

    0 讨论(0)
  • 2020-11-29 20:40

    It's probably worth mentioning that there is the orientationchange event that you might want to hook into like so:

    $('body').bind('orientationchange', function(){
        // check orientation, update styles accordingly
    });
    

    I know that about this event being fired by mobile Safari, you'd have to check about other browsers.

    0 讨论(0)
  • 2020-11-29 20:42

    You could just rewrite the media query as a javascript expression instead:

    function sizehandler(evt) {
        ...
    }
    
    function orientationhandler(evt){  
    
      // For FF3.6+  
      if (!evt.gamma && !evt.beta) {  
        evt.gamma = -(evt.x * (180 / Math.PI));  
        evt.beta = -(evt.y * (180 / Math.PI));  
      }  
    
      // use evt.gamma, evt.beta, and evt.alpha   
      // according to dev.w3.org/geo/api/spec-source-orientation  
    
      ...
    }  
    
    window.addEventListener('deviceorientation', orientationhandler, false);
    window.addEventListener('MozOrientation', orientationhandler, false);
    window.addEventListener('load', orientationhandler, false); 
    window.addEventListener('resize', sizehandler, false); 
    window.addEventListener('load', sizehandler, false); 
    
    0 讨论(0)
  • 2020-11-29 20:43

    You can use window.matchMedia():

    Test a mobile device media query

    if (matchMedia('only screen and (max-width: 480px)').matches) {
      // smartphone/iphone... maybe run some small-screen related dom scripting?
    }
    

    Test landscape orientation

    if (matchMedia('all and (orientation:landscape)').matches) {
      // probably tablet in widescreen view
    }
    

    Currently supported in all modern browsers (more details)

    Polyfill for old browsers: https://github.com/paulirish/matchMedia.js/

    0 讨论(0)
提交回复
热议问题