固定位置不适用于移动浏览器

时间:2013-10-08 17:15:32

标签: javascript android html css position

如何在移动浏览器(ios和android)中修复元素位置? 元素仍然使用以下代码滚动ios< 5和android< 4

<html>
 <head>
<style>
     .fixed{
      position:fixed;
      top:0;
      left:0;
    }
</style>
</head>
<body>
     <div class="fixed">
     Hi I m Position Fixed 
     </div>
    <div>
       sample text<br/>
       sample text<br/>
       sample text<br/>
       sample text<br/>
       sample text<br/>
       sample text<br/>
       sample text<br/>
       sample text<br/>
       sample text<br/>
       sample text<br/>
       sample text<br/>
       sample text<br/>
       sample text<br/>
       sample text<br/>
       sample text<br/>
       sample text<br/>
       sample text<br/>
       sample text<br/>
       sample text<br/>
       sample text<br/>
       sample text<br/>
       sample text<br/>
       sample text<br/>
       sample text<br/>
       sample text<br/>
       sample text<br/>
       sample text<br/>
       sample text<br/>
       sample text<br/>
       sample text<br/>
       sample text<br/>
       sample text<br/>
       sample text<br/>
       sample text<br/>
       sample text<br/>
       sample text<br/>
       sample text<br/>
       sample text<br/>
       sample text<br/>
       sample text<br/>
       sample text<br/>
       sample text<br/>
       sample text<br/>
       sample text<br/>
       sample text<br/>
       sample text<br/>
       sample text<br/>
       sample text<br/>
       sample text<br/>

    </div>
</body>
</html>

3 个答案:

答案 0 :(得分:32)

position: fixediOSBlackberry的大多数旧版本中无效。我在大多数移动浏览器中尝试过此修复程序,并且在没有任何JavaScript插件的情况下运行顺利。

  

使用-webkit-backface-visibility: hidden;

.fixed {
  position: fixed;
  top: 0px;
  left: 0px;
  width: 320px;
  height: 50px;
  background: red;
  -webkit-backface-visibility: hidden;
  /*--^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Most Important*/
}
<div class="fixed">
  Hi I m Position Fixed
</div>
<div>
  sample text
  <br/>sample text
  <br/>sample text
  <br/>sample text
  <br/>sample text
  <br/>sample text
  <br/>sample text
  <br/>sample text
  <br/>sample text
  <br/>sample text
  <br/>sample text
  <br/>sample text
  <br/>sample text
  <br/>sample text
  <br/>sample text
  <br/>sample text
  <br/>sample text
  <br/>sample text
  <br/>sample text
  <br/>sample text
  <br/>sample text
  <br/>sample text
  <br/>sample text
  <br/>sample text
  <br/>sample text
  <br/>sample text
  <br/>sample text
  <br/>sample text
  <br/>sample text
  <br/>sample text
  <br/>sample text
  <br/>sample text
  <br/>sample text
  <br/>sample text
  <br/>sample text
  <br/>sample text
  <br/>sample text
  <br/>sample text
  <br/>sample text
  <br/>sample text
  <br/>sample text
  <br/>sample text
  <br/>sample text
  <br/>sample text
  <br/>sample text
  <br/>sample text
  <br/>sample text
  <br/>sample text
  <br/>sample text
  <br/>

</div>

答案 1 :(得分:-1)

你也可以使用position:absolute来固定标题试试IT ..

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0">
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <title>Web App Template</title>
    <style type="text/css" media="all">
        body,ul,li {padding:0;margin:0;border:0;}
        body {font-family:helvetica;}
        header{background-color:#deb500;position:absolute;z-index:2;top:0; left:0;width:100%;height:45px;padding:0;}
        footer {background-color:#c27b00;position:absolute;z-index:2;bottom:0; left:0;width:100%;height:48px;padding:0;border-top:1px solid #444;}
        header, footer{font-size:20px;text-align:center;color:#f3f3f3;font-weight:bold;text-shadow:0 -1px 0 rgba(0,0,0,0.5);line-height:45px;}
        #wrapper {position:absolute;z-index:1;top:45px; bottom:48px; left:0;width:100%;background:#aaa;overflow:auto;}
        #scroll-content {position:absolute;z-index:1;width:100%;padding:0;}
        ul {list-style:none;padding:0;margin:0;width:100%;text-align:left;}
        li {padding:0 10px;height:40px;line-height:40px;border-bottom:1px solid #ccc;border-top:1px solid #fff;background-color:#fafafa;font-size:14px;}
    </style>
</head>
<body>
    <header>Web App Template</header>
    <div id="wrapper">
        <div id="scroll-content">
            <ul>
                <li>Some Stuff</li>
                <li>More Stuff</li>
                <li>Big Stuff</li>
                <li>Small Stuff</li>
                <li>Geek Stuff</li>
                <li>Nerd Stuff</li>
                <li>Fast Stuff</li>
                <li>Slow Stuff</li>
                <li>Good Stuff</li>
                <li>Bad Stuff</li>
                <li>Your Stuff</li>
                <li>My Stuff</li>
                <li>Their Stuff</li>
                <li>Our Stuff</li>
                <li>Super Stuff</li>
                <li>Uber Stuff</li>
                <li>Stuff Stuff</li>
                <li>French Stuff</li>
                <li>German Stuff</li>
                <li>English Stuff</li>
                <li>American Stuff</li>
                <li>Stuff</li>
            </ul>
        </div>   
    </div>
    <footer>Some Footer Content</footer>

答案 2 :(得分:-2)

如果仅在移动浏览器中,并且您不需要向后兼容iOS和黑莓,则需要使用媒体查询。

@media only screen and (max-device-width : 730px) {
    .fixed {
        position:fixed;
        top:0;
        left:0;
    }
}