2014-06-26 24 views
1

我正在使用下面的JavaScript代碼。但它不適用於瀏覽器大小調整。我也在這個項目中使用了基礎(zurb)框架。爲什麼'orientationchange'事件不能在瀏覽器中調整大小?

<script type="text/javascript"> 
    $(window).on("orientationchange", function() { 
     alert("The orientation has changed!"); 
     // I want to do my other stuff here which is depend on Landscap or portrait 
    }); 
</script> 

請讓我知道我在這裏錯過了什麼。 感謝

回答

1

orientation change是縱向/橫向變化

resize是不同的事件

,你可以關注一下了load這是大多數類似案件load所需的事件會先啓用頁面加載時所以你可以做一些初步的調整。

$(window).on("orientationchange load resize", function() { 
    alert("The orientation has changed!"); 
    // I want to do my other stuff here which is depend on Landscap or portrait 
}); 
+1

非常感謝!它運作完美。 – prog1011

-1

試試這個:

// Find matches 
var mql = window.matchMedia("(orientation: portrait)"); 

// If there are matches, we're in portrait 
if(mql.matches) { 
    // Portrait orientation 
} else { 
    // Landscape orientation 
} 

// Add a media query change listener 
mql.addListener(function(m) { 
    if(m.matches) { 
     // Changed to portrait 
    } 
    else { 
     // Changed to landscape 
    } 
}); 
相關問題