用css3解决移动端页面自适应横屏竖屏的思考
之前对于横屏的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解决移动端页面自适应横屏竖屏的思考的更多相关文章
- 解决移动端页面滚动后不触发touchend事件
解决移动端页面滚动后不触发touchend事件 问题 在移动端页面进行优化时,一般使用touch事件替代鼠标相关事件.用的较多的是使用touchend事件替代PC端的click和mouseup事件. ...
- js 横屏 竖屏 相关代码 与知识点
<!DOCTYPE html> <html> <head> <title></title> </head> <body&g ...
- HTML5中判断横屏竖屏
在移动端中我们经常碰到横屏竖屏的问题,那么我们应该如何去判断或者针对横屏.竖屏来写不同的代码呢. 这里有两种方法: 一:CSS判断横屏竖屏 写在同一个CSS中 1 2 3 4 5 6 @media s ...
- 【转】Android 模拟器横屏竖屏切换设置
http://blog.csdn.net/zanfeng/article/details/18355305# Android 模拟器横屏竖屏切换设置时间:2012-07-04 来源:设计与开发 ...
- Android之横屏竖屏显示问题
1.採用不同的布局文件 在res下创建目录layout-land 在该目录下写入的layout(xml文件)横屏的时候系统自己主动选择显示的layout 同理: 在res下创建目录layout-por ...
- vue手机端横屏竖屏切换
1.建一个空白的vue文件,添加上如下代码 data() { this.$router.go(-1) return {} } 2.在需要横屏竖屏切换的页面中加入如下代码: beforeMount( ...
- javascript判断手机旋转横屏竖屏
javascript判断手机旋转横屏竖屏 // 横屏竖屏函数 function orientationChange(){ switch(window.orientation) { case 0: // ...
- Android横屏竖屏切换的问题
Android横屏竖屏切换的问题 http://blog.sina.com.cn/s/blog_77c632410101790w.html
- RK3288 6.0 双屏异显,横屏+竖屏【转】
本文转载自:http://blog.csdn.net/clx44551/article/details/78215730?locationNum=8&fps=1 RK3288 6.0 双屏异显 ...
随机推荐
- vsphere client和vsphere web client的区别
vsphere client是一个运行在windows桌面上的客户端,在linux环境下无法运行,在vsphere5.0以后,VMware在逐渐弱化vsphere client的作用,现在很多高级功能 ...
- CodeForcesGym 100502H Clock Pictures
Clock Pictures Time Limit: 1000ms Memory Limit: 524288KB This problem will be judged on CodeForcesGy ...
- POJ——T 2406 Power Strings
http://poj.org/problem?id=2406 Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 50627 ...
- [Angular] How to get Store state in ngrx Effect
For example, what you want to do is navgiate from current item to next or previous item. In your com ...
- JAVASE学习笔记:第十章 SWing经常使用控件类(二)
7.JComboBox 下拉列表 /* * 初始化下拉列表 */ public void addcomb(){ String[] area = {"山西省&qu ...
- 阿姆达尔定律(Amdahl's law)
首先给出阿姆达尔定律的数学公式描述: S(N)=1(1−p)+pN p:程序中可并行部分的程序在单核上执行时间的占比: N:处理器的数目(总的核心数) S(N):程序在 N 个处理器(总核心数)相对在 ...
- Mysql数据库的瓶颈处理一点建议
Mysql数据库的瓶颈处理一点建议 我们在使用Mysql数据库是常见的两个瓶颈是CPU和I/O的瓶颈,CPU在饱和的时候一般发生在数据装入内存或从磁盘上读取数据时候.磁盘I/O瓶颈的出 ...
- activity 接回返回值
activity 接回返回值 今天做订单列表显示 点击某一项显示订单详细信息,在详细activity中用户可以选择取消订单(未支付的状态下)当用户取消订单后订单列表也要改变状态,原来最初做法是所加载绑 ...
- Codeforces Round #194 (Div. 2) 部分题解
http://codeforces.com/contest/334 A题意:1-n^2 平均分成 n 份,每份n个数,且和相同 解法 : 构造矩阵1-n^2的矩阵即可 ][]; int main() ...
- vue移动端上拉加载更多
LoadMore.vue <template> <div class="load-more-wrapper" @touchstart="touchSta ...