wor*_*ked 2 jquery orientation screen-orientation device-orientation
如果在发生方向更改时触发了onChange函数,如何在onChange中设置将更新jquery选择器的值.例如:
$(document).ready(function(){
var onChanged = function() {
if(window.orientation == 90 || window.orientation == -90){
image = '<img src="images/land_100.png">';
}else{
image = '<img src="images/port_100.png">';
}
}
$(window).bind(orientationEvent, onChanged).bind('load', onChanged);
$('#bgImage').html(image); //won't update image
});
Run Code Online (Sandbox Code Playgroud)
您需要将更新放在onChanged函数内的图像中,以便每次方向更改时,图像HTML都将更改.
$(document).ready(function(){
// The event for orientation change
var onChanged = function() {
// The orientation
var orientation = window.orientation,
// If landscape, then use "land" otherwise use "port"
image = orientation == 90 || orientation == -90 ? "land" : "port";
// Insert the image
$('#bgImage').html('<img src="images/'+image+'_100.png">');
};
// Bind the orientation change event and bind onLoad
$(window).bind(orientationEvent, onChanged).bind('load', onChanged);
});
Run Code Online (Sandbox Code Playgroud)