之前对于横屏的webapp做过一些尝试,可是始终不是非常好的解决方式,前段时间又接触了类似的需求,尝试了感觉更好的解决方式。

之前的方法写的博客:移动网页横竖屏兼容适应的一些体会

这里举的样例还是平时常见的移动端的滑动页面,也就是上下切换页面的”H5“。

首先要做的准备:

1、html布局

<div id="wrap">
<div class="pageWrap">
<div class="img11"><img data="images/1/plane.png" alt=""></div>
<div class="img12 animated"><img data="images/1/tips.png" alt=""></div>
</div>
<div class="pageWrap"> </div> </div>

2、然后是css样式:

/*
YUI 3.18.1 (build f7e7bcb)
Copyright 2014 Yahoo! Inc. All rights reserved.
Licensed under the BSD License.
http://yuilibrary.com/license/
*/ html{color:#000;background:#FFF}body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,code,form,fieldset,legend,input,textarea,p,blockquote,th,td{margin:0;padding:0}table{border-collapse:collapse;border-spacing:0}fieldset,img{border:0}address,caption,cite,code,dfn,em,strong,th,var{font-style:normal;font-weight:normal}ol,ul{list-style:none}caption,th{text-align:left}h1,h2,h3,h4,h5,h6{font-size:100%;font-weight:normal}q:before,q:after{content:''}abbr,acronym{border:0;font-variant:normal}sup{vertical-align:text-top}sub{vertical-align:text-bottom}input,textarea,select{font-family:inherit;font-size:inherit;font-weight:inherit;*font-size:100%}legend{color:#000}#yui3-css-stamp.cssreset{display:none} img{ width: 100%; display: block;} /* main css */
body{ width: 100%; height: 100%; position: relative; left:0; top: 0; background: #000; overflow: hidden;}
#wrap{ height: 100%; position: absolute; left: 50%; top:50%; overflow: hidden;} .pageWrap{ width: 100%; height: 100%; position: absolute; overflow: hidden; left: 0; top:0; -webkit-transition:all 1s; transition:all 1s; transform: translate3d(100%, 0, 0); -webkit-transform: translate3d(100%, 0, 0);}
.pageWrap div{ display: none;}
.pageWrap:nth-child(1){ background: #42a8fe url(http://7xioxc.com1.z0.glb.clouddn.com/bigxm_1_house.jpg) no-repeat center bottom; background-size:cover; transform: translate3d(0, 0, 0); -webkit-transform: translate3d(0, 0, 0);}
.pageWrap:nth-child(1) div{ display: block;}
.pageWrap:nth-child(2){ background: #e22143 url(http://7xioxc.com1.z0.glb.clouddn.com/bigxm_2_bg2.jpg) no-repeat center bottom; background-size:cover;} /* p1 */
.logo{ width:94px; position: absolute; left:50%; margin-left: -47px; top:23px; }
.img11{ width:97px; position: absolute; left:2%; top:10%; }
.img12{ width: 190px; position: absolute; left: 50%; margin-left: -95px; bottom:85px;}
.img13{ width: 100%; position: absolute; left: 0; top:0;}

上面的样式包括了reset.css以及页面的样式,主要关注的地方是body的样式和#wrap、.pageWrap的样式。页面是依照横屏来写的。当为竖屏时。须要把页面旋转90度。

----------------------------------------------------------------------------------------------------------------------------------

准备好上面的内容之后。接下来就是要写我们的js实现了。

通过js来得到宽高的值来推断是竖屏还是横屏,为什么要这样子呢?

由于不是全部的浏览器都支持orientation的方法,所以我就通过这个笨笨的方法来实现了。

(1)假设浏览器的宽度大于高度。说明是横屏的,画布的宽度 == 浏览器的宽度,所以wrap不须要旋转。

$('body').css({
'width':ww+'px',
'height':wh+'px'
}); wrap.css({
'width':ww + 'px',
'height':wh + 'px',
'transform':'translate3d(-50%,-50%,0)',
'-webkit-transform':'translate3d(-50%,-50%,0)'
});

(2)假设浏览器的宽度小于高度,说明是竖的。画布的宽度 == 浏览器的高度

$('body').css({
'width':ww+'px',
'height':wh+'px'
}); wrap.css({
'width':wh + 'px',
'height':ww + 'px',
'transform':'translate3d(-50%,-50%,0) rotate(90deg)',
'-webkit-transform':'translate3d(-50%,-50%,0) rotate(90deg)'
});

这个时候,就须要把页面旋转过来了。

-------------------------------------------------

除了须要注意这一点之外,还要考虑到的是滑动页面的时候的方向。

由于横屏和竖屏的时候手指滑动的方向并非一致的。所以手指滑动的事件也须要写两个情况。

完整的測试代码能够在百度云盘中下载:http://pan.baidu.com/s/1gdix9QF

用css3解决移动端页面自适应横屏竖屏的思考的更多相关文章

  1. 解决移动端页面滚动后不触发touchend事件

    解决移动端页面滚动后不触发touchend事件 问题 在移动端页面进行优化时,一般使用touch事件替代鼠标相关事件.用的较多的是使用touchend事件替代PC端的click和mouseup事件. ...

  2. js 横屏 竖屏 相关代码 与知识点

    <!DOCTYPE html> <html> <head> <title></title> </head> <body&g ...

  3. HTML5中判断横屏竖屏

    在移动端中我们经常碰到横屏竖屏的问题,那么我们应该如何去判断或者针对横屏.竖屏来写不同的代码呢. 这里有两种方法: 一:CSS判断横屏竖屏 写在同一个CSS中 1 2 3 4 5 6 @media s ...

  4. 【转】Android 模拟器横屏竖屏切换设置

    http://blog.csdn.net/zanfeng/article/details/18355305# Android 模拟器横屏竖屏切换设置时间:2012-07-04   来源:设计与开发   ...

  5. Android之横屏竖屏显示问题

    1.採用不同的布局文件 在res下创建目录layout-land 在该目录下写入的layout(xml文件)横屏的时候系统自己主动选择显示的layout 同理: 在res下创建目录layout-por ...

  6. vue手机端横屏竖屏切换

    1.建一个空白的vue文件,添加上如下代码 data() { this.$router.go(-1) return {} }   2.在需要横屏竖屏切换的页面中加入如下代码: beforeMount( ...

  7. javascript判断手机旋转横屏竖屏

    javascript判断手机旋转横屏竖屏 // 横屏竖屏函数 function orientationChange(){ switch(window.orientation) { case 0: // ...

  8. Android横屏竖屏切换的问题

    Android横屏竖屏切换的问题 http://blog.sina.com.cn/s/blog_77c632410101790w.html

  9. RK3288 6.0 双屏异显,横屏+竖屏【转】

    本文转载自:http://blog.csdn.net/clx44551/article/details/78215730?locationNum=8&fps=1 RK3288 6.0 双屏异显 ...

随机推荐

  1. 不固定高宽的 div 水平垂直居中

    <div class="father"> <div id="main"></div> </div> .fathe ...

  2. LuoguP4012 深海机器人问题(费用流)

    题目描述 深海资源考察探险队的潜艇将到达深海的海底进行科学考察. 潜艇内有多个深海机器人.潜艇到达深海海底后,深海机器人将离开潜艇向预定目标移动. 深海机器人在移动中还必须沿途采集海底生物标本.沿途生 ...

  3. AJAX与XMLHttpRequest

    XMLHttpRequest: 中文可以解释为可扩展超文本传输请求.Xml可扩展标记语言,Http超文本传输协议,Request请求.XMLHttpRequest对象可以在不向服务器提交整个页面的情况 ...

  4. JS原生方法被覆盖后的恢复办法

    alert 被覆盖 今天装修博客园,调试了下JS代码发现 alert() 方法被官方覆盖了,查看源码得知 alert 的功能被替换成了 console.log. 恢复 var _frame = doc ...

  5. 《深入理解java虚拟机》学习笔记四/垃圾收集器GC学习/一

    Grabage Collection      GC GC要完毕的三件事情: 哪些内存须要回收? 什么时候回收? 怎样回收? 内存运行时区域的各个部分中: 程序计数器.虚拟机栈.本地方法栈这3个区域随 ...

  6. 10小时之内,暴力破解SSH账号的IP

    10小时之内,暴力破解SSH账号的IP,IP 地址数据来源于  ip138.com 182.18.76.246 北京市昌平区 北京亿安天下网络科技有限公司 联通 221.223.200.143 北京市 ...

  7. actionBar-双行字体大小修改

    <style name="BackupRestore.Theme.Person" parent="@style/BackupRestore.Theme"& ...

  8. 73.fseek与宽字符读取文件

    fseek //文件路径 ] = "1.txt"; //FILE *pf = fopen(path, "a+");//尾部添加,文件指针在尾部 //FILE * ...

  9. Objective-C基础笔记(9)Foundation常用类NSArray

    NSArray用来存储对象的有序列表,它是不可变的 NSArray不能存储C语言中的基本数据类型,如int.float.enum.struct,也不能存储nil,nil代表数组元素的结束 // // ...

  10. Spring源码分析专题——目录

    Spring源码分析专题 -- 阅读指引 IOC容器 Spring源码分析专题 -- IOC容器启动过程(上篇) Spring源码分析专题 -- IOC容器启动过程(中篇) Spring源码分析专题 ...