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

前端 未结 6 1425
猫巷女王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: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); 
    

提交回复
热议问题