$(window).bind('orientationchange',doAction);このorientationchangeイベントは、$(window)に対してバインドします。これは間違えないようにしてください。これ以外のオブジェクトにバインドしてもうまく機能しません。また実行される関数は、以下のような形で定義されます。
function doAction(event){……}引数に、発生したイベントに関する情報をまとめたオブジェクトが渡されます。これは重要です。現在の画面が縦向きか横向きかは、この渡されたオブジェクトを使ってチェックする必要があるからです。
if (event.orientation=="portrait"){……サンプルでは、このようにして向きをチェックしていました。引数オブジェクトのorientationというプロパティに、向きに関する情報が保管されています。縦向きならば"portrait"、横向きならば"landscape"というテキストが設定されているのです。これを調べることで現在の向きが分かります。
※リストが表示されない場合
AddBlockなどの広告ブロックツールがONになっているとリストなどが表示されない場合があります。これらのツールをOFFにしてみてください。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja"> <head> <meta http-equiv="content-type" content="text/html;charset=utf-8" /> <title>Sample</title> <link rel="stylesheet" href="http://code.jquery.com/mobile/1.0.1/jquery.mobile-1.0.1.min.css" /> <script type="text/javascript" src="http://code.jquery.com/jquery-1.6.4.min.js"></script> <script type="text/javascript" src="http://code.jquery.com/mobile/1.0.1/jquery.mobile-1.0.1.min.js"></script> <script type="text/javascript"> $(document).ready(function(){ $(window).bind('orientationchange',doAction); }); function doAction(event){ if (event.orientation=="portrait"){ $('#msg').text("縦向き(portrait)です。"); } else { $('#msg').text("横向き(landscape)です。"); } } </script> </head> <body> <!-- home page --> <div id="home" data-role="page"> <div data-role="header"> <h1>Hello</h1> </div> <div data-role="content"> <p id="msg">※サンプル。</p> </div> </div> </body> </html>
<< 前へ | 次へ >> |