最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Resize HTML elements on Android orientation change - Stack Overflow

programmeradmin1浏览0评论

I am trying to bind an event in Javascript to either the orientationchange or resize events in Android in order to change the width/height of some elements for my web app. In the event, I use window.innerHeight and window.innerWidth to get the current height and width of the window.

This works great on iOS and desktop devices, but on Android it seems that it calls this event before changing the values in the window variable. Therefore, when someone switches from portrait to landscape, I still get the values for portrait and therefore cannot resize correctly. Does anyone know what the problem is, and how I can fix it?

I am trying to bind an event in Javascript to either the orientationchange or resize events in Android in order to change the width/height of some elements for my web app. In the event, I use window.innerHeight and window.innerWidth to get the current height and width of the window.

This works great on iOS and desktop devices, but on Android it seems that it calls this event before changing the values in the window variable. Therefore, when someone switches from portrait to landscape, I still get the values for portrait and therefore cannot resize correctly. Does anyone know what the problem is, and how I can fix it?

Share Improve this question asked Jan 13, 2011 at 22:47 Dan D.Dan D. 1,1251 gold badge12 silver badges18 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 4

I have fixed the problem. It turns out that the resize event works correctly, but the orientationchange event does not. I was testing to see if the orientationevent was present, and then binding to that if possible. I have changed my code over to only use the resize event, and have achieved the desired effect.

I'm not sure if this is unique to my code or not, but that's what fixed it for me.

My investigation showed that this Android webview bug ("orientationchange" fires 20-150ms before changing the window.innerHeight Javascript value) exists when you modify the default user agent of the web view. Don't ask me how one is related to another, though.

But to solve your probelm, you can do one of the following:

1) get rid of the following useragent modification from your code

webView.getSettings().setUserAgentString("custom-user-agent");

2) use "resize" event instead of "orientationchange" event

There are CSS selectors for this:

/*normal styles here */
#wrap {
   width:1024px;
}
@media only screen and (orientation:portrait){
   /* portrait styles here */
   #wrap {
      width:768px;
   }
}

Source: http://matthewjamestaylor./blog/ipad-layout-with-landscape-portrait-modes

I can confirm that "orientationchange" -event does not work as expected in android 2.2 and 2.3. It says it present when you check for availability but then does not fire. It works in Android 1.6 and 2.1 though. I ended up, just like you, using "resize" -event for all Android versions.

Take a look at the question and answers for Detect rotation of Android phone in the browser with JavaScript . It covers a cross platform to detect changes in orientation.
For getting the size of the viewport and/or window cross platform, take a look at http://responsejs./ or http://verge.airve./.

发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>