jquery另外一种类似tab切换效果
简要:最近做项目一些效果不能用淘宝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切换效果的更多相关文章
- vue-cli 路由 实现类似tab切换效果(vue 2.0)
1,更改main.js 2,在App.vue中,写入两个跳转链接(router-link),分别跳转到"home""About" (home.About即分别是 ...
- jquery写的tab切换效果 非常简单
自己写的一款 tab切换效果,比较简单,适合新手 <style type="text/css">*{margin:0; padding:0; font-size:12p ...
- 案例(拖拽对话框、高清放大镜、自制滚动条、元素的隐藏方式、表格隔行变色、tab切换效果、字符串拼接、刷新评论)
一.拖拽对话框 <style> .of{ width: 500px; } #link,#close{ text-decoration: none; margin: 0 10px; font ...
- 又一Tab切换效果(js实现)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- CSS3 :target伪类实现Tab切换效果
用:target伪类实现Tab切换效果真的非常简单!简单到什么程度呢?它只需要下面这些代码. style.css: .song-info { position: absolute; backgroun ...
- 实用CSS3属性之 :target伪类实现Tab切换效果
CSS3 :target伪类用来改变页面中锚链接URL所指向的ID样式,例如你要改变描链接指向#tab的元素字体颜色为蓝色,哪么你可以这样写成#tab:target {color:blue} 浏览器支 ...
- CSS3 :target伪类实现Tab切换效果 BY commy
http://www.shejidaren.com/examples/css3-target/css3-target.html#tab1 标签一 标签二 标签三 欢迎加设计达人Q群:50063010设 ...
- 100种不同图片切换效果插件pageSwitch
分享100种不同图片切换效果插件pageSwitch.这是一款适用于全屏切换场景,即一切一屏,并且实现了超过一百种切换效果,支持自定义切页动画.效果图如下: 在线预览 源码下载 实现的代码. ht ...
- 基于jquery鼠标点击图片翻开切换效果
基于jquery鼠标点击图片翻开切换效果是一款基于jQuery+CSS3实现的点击图片切换特效.效果图如下: 在线预览 源码下载 实现的代码. html代码: <div class=&quo ...
随机推荐
- centos7 mysql8.0 RPM软件包方式安装
1下载安装包:https://dev.mysql.com/downloads/mysql/8.0.html 2.解压安装包后可以看下如下文件列表 3.在当前目录打开终端 查看并卸载 mariadbrp ...
- 为什么main方法是public static void?
Main方法是我们学习Java编程语言时知道的第一个方法,你是否曾经想过为什么main方法是public.static.void的.当然,很多人首先学的是C和C++,但是在Java中main方法与前者 ...
- OSGI企业应用开发(二)Eclipse中搭建Felix运行环境
上篇文章介绍了什么是OSGI以及使用OSGI构建应用的优点,接着介绍了两款常用的OSGI实现,分别为Apache Felix和Equinox,接下来开始介绍如何在Eclipse中使用Apache Fe ...
- javascript获取网页各种高宽及位置总结
screen对象 获取屏幕的高宽(分辨率) screen.width //屏幕的宽 screen.height //屏幕的高 screen.availWidth //屏幕可用宽度 屏幕的像素高度减去系 ...
- EFCore中SQLSERVER 2008 的分页问题
自SQLSERVER 2012起新增了 Offset Fetch 语法,因此EFCore默认是以此语法生成相应的分页语句的. 如果我们的目标数据库低于 2012,那么EFCore默认生成的语句在执行的 ...
- 蓝魔i7s刷机
,电脑管家,豌豆荚之类的PC工具 5. 安装MFT6.0.43.exe, 注意:MFT6.0.43需要对应的ISOC和USB驱动,不可使用其它版本 6. 将CUSTOM_CONFIG..INI文件复制 ...
- Gradle 'MYasprj' project refresh failed Error:CreateProcess error=216, 该版本的 %1 与您运行的 Windows 版本不兼容
Gradle ‘MYasprj’ project refresh failed Error:CreateProcess error=216, 该版本的 %1 与您运行的 Windows 版本不兼容.请 ...
- Oracle EBS AR应收核销取值
AR_RECEIVABLE_APPLICATIONS APP, AR_CASH_RECEIPTS CR, AR_PAYMENT_SCHEDULES PS_INV, HZ_CUST_ACCOUNTS C ...
- Grafana是一个可视化面板-安装配置介绍
Grafana是一个可视化面板(Dashboard),有着非常漂亮的图表和布局展示,功能齐全的度量仪表盘和图形编辑器,支持Graphite.zabbix.InfluxDB.Prometheus和Ope ...
- Yearning v1.3.0 发布,Web 端 SQL 审核平台
企业级MYSQL web端 SQL审核平台. Website 官网 www.yearning.io Feature 功能 数据库字典自动生成 SQL查询 查询工单 导出 自动补全,智能提示 查询语句审 ...