zoukankan      html  css  js  c++  java
  • CSS3 Media Queries 特性的妙用

    第一招:

    在网页中,pixel与point比值称为 device-pixel-ratio,普通设备都是1,iPhone 4是2,有些Android机型是1.5。

    那么-webkit-min-device-pixel-ratio:2 可以用来区分iphone(4/4s/5)和其它的手机

    iPhone4/4s的分辨率为640*960 pixels,DPI为是326,设备高度为480px

    iPhone5的分辨率为640*1136 pixels,DPI依然是326,设备高度为568px

    那么我们只需要判断iphone手机的device-height(设备高)值即可区别iPhone4和iPhone5

    使用css

    通过 CSS3 的 Media Queries 特性,可以写出兼容iPhone4和iPhone5的代码~~

    方式一,直接写到样式里面

    @media (device-height:480px) and (-webkit-min-device-pixel-ratio:2){/* 兼容iphone4/4s */
    .class{}
    }
    @media (device-height:568px) and (-webkit-min-device-pixel-ratio:2){/* 兼容iphone5 */
    .class{}
    }

    方式二,链接到一个单独的样式表,把下面的代码放在<head>标签里

    <link rel="stylesheet" media="(device-height: 480px) and (-webkit-min-device-pixel-ratio:2)" href="iphone4.css" />
    <link rel="stylesheet" media="(device-height: 568px)and (-webkit-min-device-pixel-ratio:2)" href="iphone5.css" />

    使用JS

    //通过高度来判断是否是iPhone 4还是iPhone 5
    isPhone4inches = (window.screen.height==480);
    isPhone5inches = (window.screen.height==568);

    移动开发发展飞猛,各种新的设备也不断的出现,我们在向后兼容的同时,也需要不断术向前学习,赶上时代步伐~

    参考资料:

    iPhone 5 and iOS 6 for HTML5 developers, a big step forward: web inspector, new APIs and more

    在iPhone 4上为视网膜显示屏优化网页图片

    第二招

    有些时候,我们做手机端的时候不得不考虑横屏的浏览效果。而大多数情况下横屏的style都被无视掉了,可我们把丑陋的一面暴露出来真的好吗?

    于是,我想到了下面的方法:

    @media screen and (orientation:portrait) {
        #tips{ display:none;}/* 提示竖屏隐藏 */    
    }
    @media screen and (orientation:landscape) {
        .swiper-container,#preloadarea{display:none;}/* 横屏状态 隐藏的内容*/
        #tips{ display:block !important; position:absolute; top:0; right:0; bottom:0; left:0; margin:auto; /*水平垂直居中*/font-size:2em; color:#fff; width:40%; height:10%; text-align:center;}/* 提示竖屏显示 */
    }
    <div id="tips">竖屏才是最佳浏览体验</div>
    

    纯CSS打造,你!值得拥有!

  • 相关阅读:
    Python 学习笔记 11.模块(Module)
    Python 学习笔记 8.引用(Reference)
    Python 学习笔记 9.函数(Function)
    Python 学习笔记 6.List和Tuple
    Python 学习笔记 4.if 表达式
    Python 学习笔记 2.自省
    Python 学习笔记 3.简单类型
    Python 学习笔记 7.Dictionary
    Python 学习笔记 5.对象驻留
    Python 学习笔记 10.类(Class)
  • 原文地址:https://www.cnblogs.com/crafts/p/4602329.html
Copyright © 2011-2022 走看看