简要:最近做项目一些效果不能用淘宝kissy框架 所以代码得自己写啊 网上当然有很多组件 但是用他们的代码很多(有的是我不需要的代码) 且还要看API 还不如自己动手写个简单一个,是这么一种简单的效果,比如菜单项 有上周 本周 下周 等等项  那么对应的项 有相应的内容,上周项 的 内容为111 本周项的内容为222 下周项内容为333等等,当我点击上一页或者下一页时候 切换当前的项及项对应的内容,如果项的索引等于0的时候 那么上一页按钮灰掉 变得不可点击 同理 下一页索引等于最大项时候 也灰掉 不可点击等。

简单的HTML也贴上:(只是为了做个demo 容易说明而已)。

<div class="container">
<div class="menuItem">
<span class="prev">上一页</span>
<div class="wrapList">
<ul class="list">
<li class="menu">上周</li>
<li class="menu">本周</li>
<li class="menu">下周</li>
</ul>
</div>
<span class="next">下一页</span>
</div>
<div class="allContent">
<div class="inner">
<div class="content">11111</div>
<div class="content">22222</div>
<div class="content">33333</div>
</div>
</div>
</div>

CSS代码如下:

 <style>
*{margin:0;padding:0;}
ul,li{list-style:none;}
.container{width:100%;overflow:hidden;}
.menuItem {width:300px;height:22px;line-height:22px;}
.prev, .next{float:left;width:60px;cursor:pointer;overflow:hidden;}
.wrapList {float:left;width:60px;height:22px;line-height:22px;overflow:hidden;}
.list li{float:left;width:60px;height:22px;line-height:22px;text-align:center;overflow:hidden;}
.allContent{width:300px;height:300px;overflow:hidden;}
.content{float:left;width:300px;height:300px;overflow:hidden;}
.disable {cursor:default;color:gray;}
</style>

JS代码如下:

/**
* 另外一种类似tab切换效果
*/ function TabPage() { this.config = {
type : 'click', // 默认为点击 上一页按钮 及 下一页按钮
menuItemCls : '.menu', // 要移动的菜单项
menuWrapCls : '.wrapList', // 菜单项外部容器
panelCls : '.content', // 要滚动的内容
panelWarpCls : '.allContent',// 面板外部容器
prevCls : '.prev', // 上一页按钮
nextCls : '.next', // 下一页按钮
disabledCls : 'disable', // 当上一页达到最前面的一页 或者 下一页达到最后一页时候 让其不可点击 按钮变灰
switchTo : 0, // 默认切换到第几项 默认为第一项
callback : null // 切换完后添加回调函数
}; this.cache = {
index : 0 // 保存当前的索引
};
}
TabPage.prototype = {
init: function(options) {
this.config = $.extend(this.config,options || {});
var self = this,
_config = self.config,
_cache = self.cache;
// 初始化容器的宽度 使其左右动画移动操作
var menuParent = $(_config.menuItemCls).parent(),
menuWidth = $(_config.menuWrapCls).width(),
len = $(_config.menuItemCls).length,
contentParent = $(_config.panelCls).parent(),
contentWidth = $(_config.panelWarpCls).width();
$(menuParent).css("width",menuWidth * len);
$(contentParent).css('width',contentWidth * len); // 保存页面加载时候 切换到第几项索引 然后当上一页点击 或者 下一页点击 计算出当前的索引
_cache.index = _config.switchTo; // 页面初始化时候 先判断_cache.index= 0,或者 _cache.index 等于最大的时候 如果是的话 那么让其对应的按钮变灰不可点击
if(_cache.index == 0) {
!$(_config.prevCls).hasClass(_config.disabledCls) && $(_config.prevCls).addClass(_config.disabledCls);
return;
}else {
$(menuParent).animate({"marginLeft":-menuWidth*_cache.index});
$(contentParent).animate({"marginLeft":-contentWidth*_cache.index});
} if(_cache.index == len) {
!$(_config.nextCls).hasClass(_config.disabledCls) && $(_config.nextCls).addClass(_config.disabledCls);
return;
}
// prev点击
$(_config.prevCls).unbind(_config.type);
$(_config.prevCls).bind(_config.type,function(){ //判断当前的索引是否是第一项 如果是的话 上一页按钮是不可点击的状态
if(_cache.index == 0) {
!$(_config.prevCls).hasClass(_config.disabledCls) && $(_config.prevCls).addClass(_config.disabledCls);
return;
}else {
_cache.index--;
// 动画animate 移动操作
$(menuParent).animate({"marginLeft":-menuWidth*_cache.index});
$(contentParent).animate({"marginLeft":-contentWidth*_cache.index});
$(_config.nextCls).hasClass(_config.disabledCls) && $(_config.nextCls).removeClass(_config.disabledCls); //回调函数 _config.callback && $.isFunction(_config.callback) && _config.callback(_cache.index); // 如果已经是第一项的话 那么上一页按钮灰掉 不可点击状态
if(_cache.index == 0){
!$(_config.prevCls).hasClass(_config.disabledCls) && $(_config.prevCls).addClass(_config.disabledCls);
return;
} }
}); // click点击
$(_config.nextCls).unbind('click');
$(_config.nextCls).bind('click',function(){ //判断当前的索引是否是最后一项 如果是的话 下一页按钮是不可点击的状态
if(_cache.index == len-1) {
!$(_config.nextCls).hasClass(_config.disabledCls) && $(_config.nextCls).addClass(_config.disabledCls);
return;
}else {
_cache.index++;
// 动画animate 移动操作
$(menuParent).animate({"marginLeft":-menuWidth*_cache.index});
$(contentParent).animate({"marginLeft":-contentWidth*_cache.index});
$(_config.prevCls).hasClass(_config.disabledCls) && $(_config.prevCls).removeClass(_config.disabledCls); //回调函数
_config.callback && $.isFunction(_config.callback) && _config.callback(_cache.index); // 同理 如果已经是最后的话 那么下一页按钮灰掉 不可点击状态
if(_cache.index == len - 1){
!$(_config.nextCls).hasClass(_config.disabledCls) && $(_config.nextCls).addClass(_config.disabledCls);
return;
}
}
});
}
};

调用方式如下:

<script>
new TabPage().init({
switchTo : 1,
callback: function(index){
console.log(index);
}
});
</script>

jquery另外一种类似tab切换效果的更多相关文章

  1. vue-cli 路由 实现类似tab切换效果(vue 2.0)

    1,更改main.js 2,在App.vue中,写入两个跳转链接(router-link),分别跳转到"home""About" (home.About即分别是 ...

  2. jquery写的tab切换效果 非常简单

    自己写的一款 tab切换效果,比较简单,适合新手 <style type="text/css">*{margin:0; padding:0; font-size:12p ...

  3. 案例(拖拽对话框、高清放大镜、自制滚动条、元素的隐藏方式、表格隔行变色、tab切换效果、字符串拼接、刷新评论)

    一.拖拽对话框 <style> .of{ width: 500px; } #link,#close{ text-decoration: none; margin: 0 10px; font ...

  4. 又一Tab切换效果(js实现)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  5. CSS3 :target伪类实现Tab切换效果

    用:target伪类实现Tab切换效果真的非常简单!简单到什么程度呢?它只需要下面这些代码. style.css: .song-info { position: absolute; backgroun ...

  6. 实用CSS3属性之 :target伪类实现Tab切换效果

    CSS3 :target伪类用来改变页面中锚链接URL所指向的ID样式,例如你要改变描链接指向#tab的元素字体颜色为蓝色,哪么你可以这样写成#tab:target {color:blue} 浏览器支 ...

  7. CSS3 :target伪类实现Tab切换效果 BY commy

    http://www.shejidaren.com/examples/css3-target/css3-target.html#tab1 标签一 标签二 标签三 欢迎加设计达人Q群:50063010设 ...

  8. 100种不同图片切换效果插件pageSwitch

    分享100种不同图片切换效果插件pageSwitch.这是一款适用于全屏切换场景,即一切一屏,并且实现了超过一百种切换效果,支持自定义切页动画.效果图如下: 在线预览   源码下载 实现的代码. ht ...

  9. 基于jquery鼠标点击图片翻开切换效果

    基于jquery鼠标点击图片翻开切换效果是一款基于jQuery+CSS3实现的点击图片切换特效.效果图如下: 在线预览   源码下载 实现的代码. html代码: <div class=&quo ...

随机推荐

  1. Mybatis标签bind用法

    Mybatis使用bind元素进行模糊查询,不用在乎数据库是mysql还是oracle从而提高可移植性 使用bind元素传递多个参数 public List<Student> findSt ...

  2. Java生成代码(字节码)

    一.方式 代码生成器 & IDE 编译时代码生成: Pluggable Annotation Processing API 运行时代码生成: Compiler API 运行时生成字节码: cg ...

  3. php命令行生成与读取配置文件

    接着之前的文章:php根据命令行参数生成配置文件 ghostinit.php <?php class ghostinit{ static $v = 'ghost version is 1.1'; ...

  4. JavaScript--动态加载脚本和样式(23)

    一 动态脚本 // 当网站需求变大,脚本的需求也逐步变大;我们不得不引入太多的JS脚本而降低了整站的性能; // 所以就出现了动态脚本的概念,在适时的时候加载相应的脚本; 1.动态引入js文件 var ...

  5. 优雅高效的免费在线APP原型工具

    前往:xiaopiu 打开浏览器即可享受软件级的流畅体验!云端组件库.交互动效自定义.高效友好的操作方式让您的创意即刻呈现!

  6. Linux下安装VSCode

    进行下载 64位的包:地址: https://code.visualstudio.com/docs/?dv=linux64&build=insiders 1.解压: tar -zxvf cod ...

  7. Expo大作战(十四)--expo中消息推送的实现

    简要:本系列文章讲会对expo进行全面的介绍,本人从2017年6月份接触expo以来,对expo的研究断断续续,一路走来将近10个月,废话不多说,接下来你看到内容,讲全部来与官网 我猜去全部机翻+个人 ...

  8. Visual Studio 2012 Update 1 离线升级包(相当于VS2012 SP1离线补丁包)

    Visual Studio 2012 Update 1 发布也有一段时间了,吾乐吧尝试了好几次在线升级,但是网络不给力啊,结果都失败了.于是一直都想找到官方提供的VS2012 SP1完整离线升级包,不 ...

  9. 美图DPOS以太坊教程(Docker版)

    一.前言 最近,需要接触区块链项目的主链开发,在EOS.BTC.ethereum.超级账本这几种区块链技术当中,相互对比后,最终还是以go-ethereum为解决方案. 以ethereum为基准去找解 ...

  10. CSS样式----CSS属性:字体属性和文本属性(图文详解)

    本文最初于2015-10-04发表于博客园,并在GitHub上持续更新前端的系列文章.欢迎在GitHub上关注我,一起入门和进阶前端. 以下是正文. 本文重要内容 CSS的单位 字体属性 文本属性 定 ...