且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

手机设备中可滚动的水平导航

更新时间:2022-08-26 08:00:52

在手机web app开发中会涉及到水平导航,例如:
手机设备中可滚动的水平导航
上面的新闻栏目就是一个水平导航,并且还可以水平滚动,因为一行显示不完. 

那么如何实现呢?

先看下实现的效果

PC端浏览器中:
手机设备中可滚动的水平导航
 

手机中:
手机设备中可滚动的水平导航
 

在手机上面没有滚动条,因为可以通过手指触屏滚动.

页面代码如下:

Html代码  手机设备中可滚动的水平导航
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. <meta charset="utf-8">  
  5. <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1.0 ,user-scalable=no">  
  6. <link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.css">  
  7. <script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>  
  8. <script src="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.js"></script>  
  9.   
  10. <style>  
  11.     #overflow {  
  12.         border: 1px solid #000;  
  13.         overflow-x: scroll;  
  14.         overflow-y: hidden;  
  15.     }  
  16.     #overflow .container div {  
  17.         float: left;  
  18.         width: 60px;  
  19.         float: left;  
  20.     }  
  21.     @media only screen and (max-width : 1024px) {  
  22.         #overflow {width: 480;   
  23.         height: 60px;}          
  24.         #overflow .container div{  
  25.             height: 45px;  
  26.         }  
  27.     }          
  28.     @media only screen and (max-width : 768px) {  
  29.         #overflow {width: 480;   
  30.         height: 60px;}          
  31.         #overflow .container div{  
  32.             height: 45px;  
  33.         }  
  34.     }          
  35.     @media only screen and (max-width : 480px) {  
  36.         #overflow {width: 480;   
  37.         height: 35px;}          
  38.         #overflow .container div{  
  39.             height: 20px;  
  40.         }  
  41.     }  
  42.     @media only screen and (max-width : 320px) {  
  43.         #overflow {width: 320;   
  44.         height: 35px;}          
  45.         #overflow .container div{  
  46.             height: 20px;  
  47.         }  
  48.     }  
  49. </style>  
  50. </head>      
  51. <body>  
  52. <div data-role="page" id="currentorders">  
  53.    <header data-role="header" data-position="fixed">  
  54.         <nav data-role="navbar">  
  55.             <div id="overflow">  
  56.                 <div class="container">  
  57.                     <div><a href="" class="yellow">item 1</a>  
  58.                     </div>  
  59.                     <div><a href="" class="orange">item 2</a>  
  60.                     </div>  
  61.                     <div><a href="" class="red">item 3</a>  
  62.                     </div>  
  63.                     <div><a href="" class="yellow">item 4</a>  
  64.                     </div>  
  65.                     <div><a href="" class="orange">item 5</a>  
  66.                     </div>  
  67.                     <div><a href="" class="red">item 6</a>  
  68.                     </div>  
  69.                     <div><a href="" class="red">item 7</a>  
  70.                     </div>  
  71.                     <div><a href="" class="red">item 8</a>  
  72.                     </div>  
  73.                     <div><a href="" class="red">item 9</a>  
  74.                     </div>  
  75.                     <div><a href="" class="red">item 10</a>  
  76.                     </div>  
  77.                      <div><a href="" class="red">item 11</a>  
  78.                     </div>  
  79.                 </div>  
  80.             </div>  
  81.         </nav>  
  82.         <div data-role="header">  
  83.             <h3>这是一个水平导航栏,并且支持水平滚动</h3>  
  84.         </div>  
  85.     </header>  
  86.     <div data-role="content">              
  87.         这是网页的主体  
  88.     </div>  
  89.       
  90. </div>          
  91. </body>  
  92. <script>  
  93.  $('#currentorders').live("pageshow", function () {  
  94.   var width = 0;  
  95.   $('#overflow .container div').each(function () {  
  96.     width += $(this).outerWidth(true);  
  97.   });  
  98.   $('#overflow .container').css('width', width + "px");  
  99. })  
  100. /*  
  101. $("#overflow .container div a").live('touchstart', function () {  
  102.  var width = 0;  
  103.  $('#overflow .container div').each(function () {  
  104.     width += $(this).outerWidth(true);  
  105.  });  
  106.  $('#overflow .container').css('width', width + "px");  
  107. })  
  108. */  
  109. </script>      
  110. </html>  

 

应用场景:移动web app

注意:

(1)页面采用HTML5,推荐使用 HTML5 的文档类型申明: <!DOCTYPE html>.


(建议使用 text/html 格式的 HTML。避免使用 XHTML。XHTML 以及它的属性,比如 application/xhtml+xml 在浏览器中的应用支持与优化空间都十分有限)。

(2)采用jQuery mobile框架

 

参阅:http://***.com/questions/20851378/horizontal-scrolling-navbar-with-jquery-mobile-and-html5