平常做移动端会用到tab选项卡,这和PC端有些区别,移动端是触摸滑动切换,PC端是点击、移入切换。

  这里滑动切换就是一个移动端事件的应用,这里主要用到的触摸事件:touchstart、touchmove、touchend。

  和做其他的效果一样,先有html结构,css样式修饰,再写JS代码。

  html:

  1. <div class="mtabs" id="tabs">
  2. <ul class="mhead">
  3. <li>tab1</li>
  4. <li>tab2</li>
  5. <li>tab3</li>
  6. </ul>
  7. <div class="mcontent">
  8. <ul>
  9. <li>tab1内容内容内容内容</li>
  10. <li>tab1内容内容内容内容</li>
  11. <li>tab1内容内容内容内容</li>
  12. <li>tab1内容内容内容内容</li>
  13. <li>tab1内容内容内容内容</li>
  14. </ul>
  15. <ul>
  16. <li>tab2内容内容内容内容</li>
  17. <li>tab2内容内容内容内容</li>
  18. <li>tab2内容内容内容内容</li>
  19. <li>tab2内容内容内容内容</li>
  20. <li>tab2内容内容内容内容</li>
  21. </ul>
  22. <ul>
  23. <li>tab3内容内容内容内容</li>
  24. <li>tab3内容内容内容内容</li>
  25. <li>tab3内容内容内容内容</li>
  26. <li>tab3内容内容内容内容</li>
  27. <li>tab3内容内容内容内容</li>
  28. </ul>
  29. </div>
  30. </div><!-- End .mtabs -->

  css:

  1. body,div,ul,li{
  2. margin:;
  3. padding:;
  4. }
  5. ul,li {
  6. list-style:none;
  7. }
  8. body {
  9. font-size:100%;
  10. font-family:Helvetica,STHeiti,Droid Sans Fallback;
  11. }
  12. .mtabs {
  13. width:100%;
  14. overflow:hidden;
  15. }
  16. .mhead {
  17. height:38px;
  18. border-top:2px solid #9ac7ed;
  19. background:#ECF2F6;
  20. -webkit-tap-highlight-color:rgba(0,0,0,0);
  21. }
  22. .mhead li {
  23. position:relative;
  24. font-size:1.125em;
  25. text-align:center;
  26. float:left;
  27. width:64px;
  28. height:38px;
  29. line-height:38px;
  30. color:#2a70be;
  31. }
  32. .mhead li.current {
  33. border-top:2px solid #2a70be;
  34. margin-top:-2px;
  35. background:#FFF;
  36. color:#c14545;
  37. }
  38. .mcontent {
  39. width:100%;
  40. overflow:hidden;
  41. }
  42. .mcontent ul {
  43. width:100%;
  44. float:left;
  45. }
  46. .mcontent li {
  47. height:35px;
  48. line-height:35px;
  49. font-size:1.125em;
  50. padding:0 10px;
  51. }

  下面的截图是想要的一个效果预览:

  

  下面是实际效果,可以在Chrome的移动模式查看:

  • tab1
  • tab2
  • tab3
  • tab1内容内容内容内容
  • tab1内容内容内容内容
  • tab1内容内容内容内容
  • tab1内容内容内容内容
  • tab1内容内容内容内容
  • tab2内容内容内容内容
  • tab2内容内容内容内容
  • tab2内容内容内容内容
  • tab2内容内容内容内容
  • tab2内容内容内容内容
  • tab3内容内容内容内容
  • tab3内容内容内容内容
  • tab3内容内容内容内容
  • tab3内容内容内容内容
  • tab3内容内容内容内容

  先贴上JS代码,供参考

  1. /**
  2. * LBS mTabs
  3. * Date: 2014-5-10
  4. * ===================================================
  5. * opts.mtab tabs外围容器/滑动事件对象(一个CSS选择器)
  6. * opts.mhead tabs标题容器/点击对象(一个CSS选择器)
  7. * opts.mcontent tabs内容容器/滑动切换对象(一个CSS选择器)
  8. * opts.index tabs索引(默认0) 指定显示哪个索引的标题、内容
  9. * opts.current tabs当前项的类名(默认current)
  10. * ===================================================
  11. **/
  12. ;(function(){
  13. window.mTabs = function(opts){
  14. if(typeof opts === undefined) return;
  15.      //取得tabs外围容器、标题容器、内容容器
  16. this.mtab = document.querySelector(opts.mtab);
  17. this.mhead = document.querySelector(opts.mhead);
  18. this.mcontent = document.querySelector(opts.mcontent);
  19.      //取得标题容器内选项集合、内容容器内容集合
  20. this.mheads = this.mhead.children;
  21. this.mcontents = this.mcontent.children;
  22.  
  23. this.length = this.mheads.length;
  24. if(this.length < 1) return;
  25. if(opts.index > this.length-1) opts.index = this.length-1;
  26. this.index = this.oIndex = opts.index || 0;
  27. this.current = opts.current || 'current'; //当前活动选项类名
  28. this.touch = {};//自定义一个对象 用来保存手指触摸相关信息
  29.  
  30. this.init();
  31. }
  32. mTabs.prototype = {
  33. init: function(opts){
  34. this.set();
  35. this.initset();
  36. this.bind();
  37. },
  38. initset: function(){
  39. for(var i = 0; i < this.length; i++){
  40. this.mheads[i].index = i;//设置了一个属性 方便点击时判断是点了哪一项
  41. this.mheads[i].className = this.mheads[i].className.replace(this.current,'');
  42. this.mcontents[i].className = this.mcontents[i].className.replace(this.current,'');
  43. }//初始化设置、先清空手动加在标题或内容HTML标签的当前类名(this.current)、再设置哪一项为当前选项并设置类名
  44. this.mheads[this.index].className += ' '+this.current;
  45. this.mcontents[this.index].className += ' '+this.current;
              //对应的内容要显示在可视区域
  46. //this.mcontent.style.webkitTransform = this.mcontent.style.transform = "translateX(" + (-this.index * this.width) + "px)";
  47. //this.mcontent.style.webkitTransform = this.mcontent.style.transform = "translate3d(" + (-this.index * this.width) + "px,0,0)";
  48. },
  49. set: function(){//获取浏览器的视口宽度、并设置内容容器的宽度、每一项内容区域的宽度,屏幕旋转,浏览器窗口变换会再次设置这些值
  50. this.width = document.documentElement.clientWidth || document.body.clientWidth;
  51. this.mcontent.style.width = this.length * this.width + 'px';
  52. for(var i = 0; i < this.length; i++) this.mcontents[i].style.width = this.width + 'px';//调整在可视区域显示的内容项
  53. //this.mcontent.style.webkitTransform = this.mcontent.style.transform = "translateX(" + (-this.index * this.width) + "px)";
  54. this.mcontent.style.webkitTransform = this.mcontent.style.transform = "translate3d(" + (-this.index * this.width) + "px,0,0)";
  55. },
  56. bind: function(){
             //绑定各种事件
  57. var _this = this;
  58. this.mtab.addEventListener("touchstart",function(e){
  59. _this.touchStart(e);
  60. }, false);
  61. this.mtab.addEventListener("touchmove",function(e){
  62. _this.touchMove(e);
  63. }, false);
  64. this.mtab.addEventListener("touchend",function(e){
  65. _this.touchEnd(e);
  66. }, false);
  67. this.mtab.addEventListener("touchcancel",function(e){
  68. _this.touchEnd(e);
  69. }, false);
  70. this.mhead.addEventListener("click",function(e){
  71. _this.touchClick(e);
  72. }, false);
  73. this.mcontent.addEventListener('webkitTransitionEnd',function(){
  74. _this.transitionEnd();
  75. }, false);
  76. window.addEventListener("resize", function(){
  77. setTimeout(function(){
  78. _this.set();
  79. },100);
  80. }, false);
  81. window.addEventListener("orientationchange",function(){
  82. setTimeout(function(){
  83. _this.set();
  84. },100);
  85. }, false);
  86. },
  87. touchStart: function(e){
  88. this.touch.x = e.touches[0].pageX;
  89. this.touch.y = e.touches[0].pageY;
  90. this.touch.time = Date.now();
  91. this.touch.disX = 0;
  92. this.touch.disY = 0;
  93. this.touch.fixed = ''; //重要 这里采用了判断是滚动页面行为、还是切换选项行为 如果是滚动页面就在滑动时只滚动页面 相应的切换选项就切换不会滚动页面
  94. },
  95. touchMove: function(e){
  96. if(this.touch.fixed === 'up') return;
  97. e.stopPropagation();
  98. if(e.touches.length > 1 || e.scale && e.scale !== 1) return;
  99. this.touch.disX = e.touches[0].pageX - this.touch.x;
  100. this.touch.disY = e.touches[0].pageY - this.touch.y;
  101. if(this.touch.fixed === ''){//行为判断 采用这种方式 主要解决手指按下移动(左上、右上)时滑动切换和滚动页面同时执行的问题
  102. if( Math.abs(this.touch.disY) > Math.abs(this.touch.disX) ){
  103. this.touch.fixed = 'up';
  104. }else{
  105. this.touch.fixed = 'left';
  106. }
  107. }
  108. if(this.touch.fixed === 'left'){
  109. e.preventDefault();
  110. if( (this.index === 0 && this.touch.disX > 0) || (this.index === this.length-1 && this.touch.disX < 0) ) this.touch.disX /= 4; //在 第一项向右滑动、最后一项向左滑动 时
  111. //this.mcontent.style.webkitTransform = this.mcontent.style.transform = "translateX(" + ( this.touch.disX - this.index * this.width ) + "px)";
  112. this.mcontent.style.webkitTransform = this.mcontent.style.transform = "translate3d(" + ( this.touch.disX - this.index * this.width ) + "px,0,0)";
  113. }
  114. },
  115. touchEnd: function(e){
  116. if(this.touch.fixed === 'left'){
  117. var _this = this, X = Math.abs(this.touch.disX);
  118. this.mcontent.style.webkitTransition = this.mcontent.style.transition = 'all 100ms';
  119. if( (Date.now() - this.touch.time > 100 && X > 10) || X > this.width/2 ){
  120. this.touch.time = Date.now();
  121. this.touch.disX > 0 ? this.index-- : this.index++;
  122. this.index < 0 && (this.index = 0);
  123. this.index > this.length - 1 && (this.index = this.length - 1);
  124. if(this.index === this.oIndex) this.mcontent.style.webkitTransition = this.mcontent.style.transition = 'all 300ms';
  125. if(this.index !== this.oIndex) this.replace();
  126. }
  127. //this.mcontent.style.webkitTransform = this.mcontent.style.transform = "translateX(" + (-this.index * this.width) + "px)";
  128. this.mcontent.style.webkitTransform = this.mcontent.style.transform = "translate3d(" + (-this.index * this.width) + "px,0,0)";
  129. }
  130. },
  131. transitionEnd: function(){
  132. this.mcontent.style.webkitTransition = this.mcontent.style.transition = 'all 0ms';
  133. },
  134. touchClick: function(e){
  135. var target = e.target;
  136. if(target.nodeType === 1 && target.index !== undefined){
  137. if(target.index === this.index) return;
  138. e.preventDefault();
  139. e.stopPropagation();
  140. this.index = target.index;
  141. this.mcontent.style.webkitTransition = this.mcontent.style.transition = 'all 100ms';
  142. //this.mcontent.style.webkitTransform = this.mcontent.style.transform = "translateX(" + (-this.index * this.width) + "px)";
  143. this.mcontent.style.webkitTransform = this.mcontent.style.transform = "translate3d(" + (-this.index * this.width) + "px,0,0)";
  144. this.replace();
  145. }
  146. },
  147. replace: function(){
  148. this.mheads[this.index].className += ' '+this.current;
  149. this.mheads[this.oIndex].className = this.mheads[this.oIndex].className.replace(this.current,'').trim();
  150. this.mcontents[this.index].className += ' '+this.current;
  151. this.mcontents[this.oIndex].className = this.mcontents[this.oIndex].className.replace(this.current,'').trim();
  152. this.oIndex = this.index;
  153. }
  154. }
  155. }());

  使用方式很简单,如下

  1. document.addEventListener('DOMContentLoaded',function(){
  2. //use mTabs
  3. new mTabs({
  4. mtab: '#tabs',
  5. mhead: '#tabs .mhead',
  6. mcontent: '#tabs .mcontent'
  7. });
  8.  
  9. /*new mTabs({
  10. mtab: '#tabs',
  11. mhead: '#tabs .mhead',
  12. mcontent: '#tabs .mcontent',
  13. index: 1,
  14. current: 'active'
  15. });*/
  16.  
  17. },false);
  1. mtab:
  2. <div class="mtabs" id="tabs">
  3. //..
  4. </div>
  5. mhead:
  6. <ul class="mhead">
  7. //..
  8. </ul>
  9. mcontent:
  10. <div class="mcontent">
  11. //..
  12. </div> 

  在此说下思路:

    先获得一个tabs容器对象(mtab),它包含了两个对应的类集合容器,一个是标签栏(mhead)、一个是内容栏(mcontent),再分别取得类集合容器里面对应的选项mheads、mcontents。

  1. this.mtab = document.querySelector(opts.mtab);
  2. this.mhead = document.querySelector(opts.mhead);
  3. this.mcontent = document.querySelector(opts.mcontent);
  4.  
  5. this.mheads = this.mhead.children;
  6. this.mcontents = this.mcontent.children;

    获取设备浏览器窗口的宽,并更新内容容器(mcontent)的宽,内容项的宽,一般在页面都会有文档声明 <!DOCTYPE html> document.documentElement.clientWidth 就能获取浏览器窗口的宽。

  1. this.width = document.documentElement.clientWidth || document.body.clientWidth;
  2. this.mcontent.style.width = this.length * this.width + 'px';
  3. for(var i = 0; i < this.length; i++) this.mcontents[i].style.width = this.width + 'px';

    在手指触摸按上时(在tabs容器对象上), 获取手指按下时在页面的位置 ( e.touches[0].pageX)。 touchs想象成有几根手指,只需要第一根按下的手指( touches[0] )。初始化了一个行为判断 this.touch.fixed (当在tabs上滑动时是要滚动页面还是要切换选项卡)。

  1. this.touch.x = e.touches[0].pageX;
  2. //..
  3. this.touch.fixed = '';

  在移动手指时,做出行为的判断。先获得移动的距离(左右方向、上下方向),根据两个方向的值比较判断是哪种行为。

  1. this.touch.disX = e.touches[0].pageX - this.touch.x;
  2. this.touch.disY = e.touches[0].pageY - this.touch.y;
  3. //..
  4. if(this.touch.fixed === ''){
  5. if( Math.abs(this.touch.disY) > Math.abs(this.touch.disX) ){
  6. this.touch.fixed = 'up';
  7. }else{
  8. this.touch.fixed = 'left';
  9. }
  10. }

  在移动手指时,内容容器(mcontent)也会跟着移动,并且做了在处于第一项和最后一项时的移动限制。

  1. if( (this.index === 0 && this.touch.disX > 0) || (this.index === this.length-1 && this.touch.disX < 0) ) this.touch.disX /= 4;

  在手指离开屏幕的时候,做出切换判断,是向左还是向右。在第一项时,不能向左切换,最后一项时不能向右切换。

  1. this.touch.disX > 0 ? this.index-- : this.index++;
  2. this.index < 0 && (this.index = 0);
  3. this.index > this.length - 1 && (this.index = this.length - 1);

  最后是真正的移动切换,用了css3动画切换,translateX 或者 translate3d .

  1. //this.mcontent.style.webkitTransform = this.mcontent.style.transform = "translateX(" + (-this.index * this.width) + "px)";
  2. this.mcontent.style.webkitTransform = this.mcontent.style.transform = "translate3d(" + (-this.index * this.width) + "px,0,0)";

 代码中有个transitionEnd方法,配合webkitTransitionEnd事件在动画切换执行完成时调用。这里调用这个方法是用来清除动画定义的持续时间。

  1. transitionEnd: function(){
  2. this.mcontent.style.webkitTransition = this.mcontent.style.transition = 'all 0ms';
  3. }
  4.  
  5. this.mcontent.addEventListener('webkitTransitionEnd',function(){
  6. _this.transitionEnd();
  7. }, false);

  点击切换是判断点击了哪一项,在初始设置时已经为每一项保存了索引值(index),根据对应的索引值,切换选项,更新状态。可以循环绑定点击事件,也可以使用事件委托,这里使用的是事件委托。

  1. this.mheads[i].index = i;
  2.  
  3. touchClick: function(e){
  4. var target = e.target;
  5. if(target.nodeType === 1 && target.index !== undefined){
  6. //..
  7. this.index = target.index;
  8. //..
  9. this.mcontent.style.webkitTransform = this.mcontent.style.transform = "translate3d(" + (-this.index * this.width) + "px,0,0)";
  10.  
  11. }
  12. }
  13.  
  14. this.mhead.addEventListener("click",function(e){
  15. _this.touchClick(e);
  16. }, false);

  tab选项卡主要是获得两组对应的类似集合(一组标签,一组内容),两个类似数组都有索引值(数组下标),通过这个索引值,做出对应的切换。获取索引值是tab选项卡的关键,移动web端的选项卡主要是增加了触摸事件操作这个索引值。加上定时器,每隔多少时间增加或者减少这个索引值,自动播放也就完成了。会了tab选项卡也就会了图片的切换,焦点图什么的,原理都是一样。

   tab选项卡下载

移动web:tab选项卡的更多相关文章

  1. tab选项卡实例

    之前也见了不少的tab选项卡,但是下面这个选项卡是一个页面中有多个时互不影响的. <head> <meta charset="utf-8"> <met ...

  2. 基于CkEditor实现.net在线开发之路(4)快速布局,工具箱,模板载入,tab选项卡简单说明与使用

    上一章给常用的from表单控件属性页面,进行了简单说明和介绍,但是由于是在网页中做界面设计,操作肯定没有桌面应用程序方便,便捷,为了更方便的布局与设计,今天我主要说一下快速布局,工具箱,tab选项卡, ...

  3. 可轮播滚动的Tab选项卡

    前段时间有试着搭建个后台主题ui框架,有用到可支持滚动的Tab选项卡,模仿着H+后台主题ui框架中的代码造轮子改造了下,可惜代码在公司,不能把代码外发出来(感觉这样被限制了很多,对于这样的公司没办法, ...

  4. android tab选项卡的使用

    项目做完了,写写博客,在项目中遇到的一些问题,或者是自己觉得很不错的东西.这一篇主要是想和大家分享一下我在项目中封装的一个东西,就是tab选项卡.先看看效果图: 我在网上看了很多有关选项卡的demo, ...

  5. js基础练习一之tab选项卡

    最近在学习前端,当然包括js,css,html什么的,在听课时做的一些小练习,记录下来: 实例一: --Tab选项卡-- <script type="text/javascript&q ...

  6. :target伪类制作tab选项卡

    :target伪类的作用是突出显示活动的HTML锚,下面是一个简单的例子: HTML代码: <div> <a href="#demo1">点击此处</ ...

  7. 工作当中实际运用(1)——tab选项卡

    不废话 直接上代码: tab选项卡 window.onload=function(){ var titles= document.getElementById('header-dh').getElem ...

  8. 各种效果的tab选项卡

    ;(function($){ $.fn.tabso=function( options ){ var opts=$.extend({},$.fn.tabso.defaults,options ); r ...

  9. (2)WinForm中改变Tab选项卡的顺序

    Tab选项卡选中,在其属性中找到TabPages这个属性.点进去,可以通过上下移动标签卡改变标签卡的顺序.

随机推荐

  1. shell脚本查看网络配置

    #!/bin/bash ifconfig|grep -E 'eth|inet'|grep -Ev '(inet6|127.0.0.1)'|sed 's/ /\n/g'|awk NF|grep -Ev ...

  2. HDU 3571 N-dimensional Sphere(高斯消元 数论题)

    这道题算是比较综合的了,要用到扩展欧几里得,乘法二分,高斯消元. 看了题解才做出来orz 基本思路是这样,建一个n*(n-1)的行列式,然后高斯消元. 关键就是在建行列式时会暴long long,所以 ...

  3. CacheHelper工具类的使用

    package com.bbcmart.util; import net.sf.ehcache.Cache; import net.sf.ehcache.CacheManager; import ne ...

  4. hdu4513(manacher)

    传送门:吉哥系列故事——完美队形II 题意:求最长回文队伍且队伍由中间向两边递减. 分析:manach算法小应用,在判断回文子串向两边递减时加点限制使回文是由中间向两边递减的. #pragma com ...

  5. Dark Side of Cloud Storage —— 数据对像的分块消重

    数据对像(可以通俗地认为是文件)的分块存储具有久远的历史.长久以来,单机文件系统一直将文件切分为若干固定大小的小块.其主要目的是为了进行有效的空间管理.互联网时代,大规模数据存储逐步发展起来.出于降低 ...

  6. ssh 即使主机,同nohup背景脚本

    下面的脚本工具:先从本地副本的脚本到远程主机,然后ssh即使在远程主机,脚本的运行副本前(因为脚本需要运行很长,它运行在后台),该脚本仅用于备忘录,如果请指点不足! #!/bin/bash cd /t ...

  7. 轻松学习之Linux教程六 正則表達式具体解释

    本系列文章由@超人爱因斯坦出品.转载请注明出处. 作者:超人爱因斯坦    个人站点:http://www.hpw123.net          文章链接:http://hpw123.net/a/L ...

  8. Java中定时器的使用

    import java.text.SimpleDateFormat; import java.util.Date; import java.util.Timer; import java.util.T ...

  9. Python数据结构-序表

    序表解包: list=['aa','bb','cc'] [a1,a2,a3]=list

  10. WPF疑难杂症会诊

    原文:WPF疑难杂症会诊 为什么图片像素是模糊的? 容器边框设为非整数时,其内容中的像素图片会产生模糊,即使设置SnapsToDevicePixels="True"也无效. 以下是 ...