首先来看看要实现的效果图:
HTML结构如下:
<div class="boxwrap fr"><!--容器 开始-->
<div class="switchBox fl" id="timeList" typeId="time">
<table cellpadding="0" cellspacing="0">
<tr>
<td class="switch_box_l"></td>
<td class="switch_box_c rel">
<span class="abs switchBtn" >
<i class="switchBtn-l"></i>
<i class="switchBtn-r"></i>
<span class="curTxt">24小时</span>
</span>
<a href="javascript:void(0);" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="swichTxt" id="24" >24小时</a>
<a href="javascript:void(0);" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="swichTxt" id="48">48小时</a>
<a href="javascript:void(0);" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="swichTxt" id="72">72小时</a>
</td>
<td class="switch_box_r"></td>
</tr>
</table>
</div>
<div class="switchBox fl" id="cityList" typeId="city">
<table cellpadding="0" cellspacing="0">
<tr>
<td class="switch_box_l"></td>
<td class="switch_box_c rel">
<span class="abs switchBtn" >
<i class="switchBtn-l"></i>
<i class="switchBtn-r"></i>
<span class="curTxt">城市</span>
</span>
<a href="javascript:void(0);" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="swichTxt" id="city">城市</a>
<a href="javascript:void(0);" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="swichTxt" id="station">站点</a>
</td>
<td class="switch_box_r"></td>
</tr>
</table>
</div>
</div><!--容器 结束-->
初始化函数:
/*@.boxwrap :滑动按钮父容器,同一界别的滑动按钮必须包含在同一个容器中
*@loadData :点击按钮后回调函数
*@#frameMain : 加载内容的iframe Id
*@tab.html : 提交参数的页面
*/
loadSwitchBox('.boxwrap',loadData,'#frameMain','tab.html');
完整demo:
HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>sitchBox</title>
<link href="css/style.css" rel="external nofollow" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="main-wraper">
<!--BEGIN topbar -->
<div class="topbar clearfix">
<div class="boxwrap fr">
<div class="switchBox fl" id="timeList" typeId="time">
<table cellpadding="0" cellspacing="0">
<tr>
<td class="switch_box_l"></td>
<td class="switch_box_c rel">
<span class="abs switchBtn" >
<i class="switchBtn-l"></i>
<i class="switchBtn-r"></i>
<span class="curTxt">24小时</span>
</span>
<a href="javascript:void(0);" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="swichTxt" id="24" >24小时</a>
<a href="javascript:void(0);" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="swichTxt" id="48">48小时</a>
<a href="javascript:void(0);" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="swichTxt" id="72">72小时</a>
</td>
<td class="switch_box_r"></td>
</tr>
</table>
</div>
<div class="switchBox fl" id="cityList" typeId="city">
<table cellpadding="0" cellspacing="0">
<tr>
<td class="switch_box_l"></td>
<td class="switch_box_c rel">
<span class="abs switchBtn" >
<i class="switchBtn-l"></i>
<i class="switchBtn-r"></i>
<span class="curTxt">城市</span>
</span>
<a href="javascript:void(0);" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="swichTxt" id="city">城市</a>
<a href="javascript:void(0);" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="swichTxt" id="station">站点</a>
</td>
<td class="switch_box_r"></td>
</tr>
</table>
</div>
</div>
</div>
<!-- END topbar -->
<div class="main-conent">
<iframe src="tab.html" frameborder="0" width="100%" frameborder="0" height="500" id="frameMain"></iframe>
</div>
</div>
<script type="text/javascript" src="script/jquery-1.11.1.min.js"></script>
<script type="text/javascript" src="script/switchbox.js"></script>
<script type="text/javascript">
$(function(){
//初始化滑动div
loadSwitchBox('.boxwrap',loadData,'#frameMain','tab.html');
});
//点击回调函数
function loadData(){
var obj = arguments[0];
var params = "";
var url ="tab.html?";
if(typeof obj !="undefined" && obj !=null){
var value = obj.value;
var type = obj.type;
var param =type+"="+value;
params = param+"&";
$('[typeId="'+type+'"]').siblings().each(function(k){
var param = $(this).attr('typeId')+"="+$(this).attr('selVal');
params+=param+"&";
});
params = params.substring(0,params.length-1);
url = url + params;
alert("切换到..."+url);
loadUrl('#frameMain', url);
}
}
</script>
</body>
</html>
JS:
/**
* 滑动div初始化函数
* @wrap 所属顶层容器
* @fn 点击后回调函数
* @iframe 子页面加载
* @url 提交的页面
*/
function loadSwitchBox(wrap, fn, iframe, url) {
$(".switchBox").each(function() {
var id = $(this).attr("id");
var type = $(this).attr("typeId");
createSlideDiv(id, type, fn);
});
loadDataInit(wrap, url, iframe);
}
/**
*初始化数据
*@wrap 顶层容器
*@url 提交的页面
*@iframe 加载子页面
*/
function loadDataInit(wrap, url, iframe) {
var params = "";
var url = url + "?";
$(wrap).find('div[typeId]').each(function() {
var param = $(this).attr('typeId') + "=" + $(this).attr('selVal');
params += param + "&";
});
params = params.substring(0,params.length-1);
url = url + params;
alert("初始化.."+ url);
loadUrl(iframe, url);
}
/**
*@iframe 加载内容页面
*@url url
*/
function loadUrl(iframe, url) {
$(iframe).attr('src', url);
}
/**
* 创建一个滑动div容器
* @wrapperId 容器ID
* @type 切换标签的类型
* @fnCallBack 回调函数
*/
function createSlideDiv(wrapperId, type, fnCallBack) {
calTabWidth(wrapperId); //初始化容器宽度
var $wraper = $('#' + wrapperId);
//默认选中第一项
var default_padding = 5;
var default_BtnWidth = $wraper.find('.swichTxt').eq(0).width() + default_padding;
var $switchBtn = $wraper.find('.switchBtn');
$switchBtn.width(default_BtnWidth); //浮动按钮宽度
$wraper.find('.swichTxt').eq(0).addClass('cur'); //设置选中选项
$wraper.attr('selVal', $wraper.find('.swichTxt').eq(0).attr('id'));
//添加click事件
$wraper.find('.swichTxt').click(function() {
var default_padding = 5;
var newIndex = $(this).index(),
oldIndex = $wraper.find('.cur').index(),
curTxt = $(this).html(),
eleWidth = $(this).width() + default_padding,
value = $(this).attr('id');
distant = -1;
$wraper.attr('selVal', value);
distant = moveWidth(wrapperId, oldIndex, newIndex); //计算滑动距离
//回调函数参数 点击标签的类型 和 值
var obj = {
type: type,
value: value
};
//左移动
if (newIndex > oldIndex) {
$wraper.find(".switchBtn").animate({
'left': '+=' + distant + 'px'
}, function() {
$wraper.find('.curTxt').html(curTxt);
$wraper.find("#switchBtn").width(eleWidth);
//执行回调函数
if ($.isFunction(fnCallBack)) fnCallBack(obj);
});
oldIndex = newIndex;
} else if (newIndex < oldIndex) { //右移动
$wraper.find(".switchBtn").animate({
'left': '-=' + distant + 'px'
}, function() {
$(this).find('.curTxt').html(curTxt);
$wraper.find(".switchBtn").width(eleWidth);
//执行回调函数
if ($.isFunction(fnCallBack)) fnCallBack(obj);
});
oldIndex = newIndex;
}
$wraper.find('.cur').removeClass('cur');
$(this).addClass('cur');
});
}
/**
*计算容器宽度
* @wrapperId 容器id
*/
function calTabWidth(wrapperId) {
var $wraper = $('#' + wrapperId);
var wrapperWidth = 0;
var tdSpace = 18; //左右圆角宽度
var oPadding = 5; //元素默认间距
$wraper.find('.swichTxt').each(function(i) {
wrapperWidth += $(this).outerWidth() + oPadding;
});
wrapperWidth = wrapperWidth + tdSpace;
$wraper.width(wrapperWidth);
}
/**
*计算按钮移动距离
* @wrapperId 容器id
* @oldIndex 之前选中的选项索引
* @newIndex 当前点击选项索引
*/
function moveWidth(wrapperId, oldIndex, newIndex) {
var $wraper = $('#' + wrapperId);
var width = 0;
//向右移动
if (oldIndex < newIndex) {
var $s_btn = $wraper.find(".switchBtn");
var $a_btn = $wraper.find('.swichTxt').eq(newIndex - 1);
//当前span左侧位置离左边的距离
var curBtn = $s_btn.offset().left;
//目标标签左侧位置离左边的距离
var einA = $a_btn.offset().left;
width = parseInt(einA - curBtn + 9); //?
} else { //向左移动
var $s_btn = $wraper.find(".switchBtn");
var $a_btn = $wraper.find('.swichTxt').eq(newIndex - 1);
//当前span左侧位置离左边的距离
var curBtn = $s_btn.offset().left;
//目标标签左侧位置离左边的距离
var einA = $a_btn.offset().left;
width = parseInt(curBtn - einA);
}
return width;
}
实例下载:点击此处
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流。
# jquery
# 滑动开关
# 滑动按钮
# jquery左右滑动开关
# jQuery 如何实现一个滑动按钮开关
# jQuery+PHP打造滑动开关效果
# jQuery实现滑动开关效果
# 回调
# 加载
# 来看看
# 点击此处
# 这篇文章
# 创建一个
# 切换到
# 圆角
# 界别
# 在同一个
# 第一项
# 有疑问
# tab
# Id
# html
# DOCTYPE
# demo
# loadSwitchBox
# iframe
# loadData
相关文章:
黑客如何通过漏洞一步步攻陷网站服务器?
C++如何将C风格字符串(char*)转换为std::string?(代码示例)
建站之星安装后界面空白如何解决?
家庭服务器如何搭建个人网站?
高端建站如何打造兼具美学与转化的品牌官网?
动图在线制作网站有哪些,滑动动图图集怎么做?
详解一款开源免费的.NET文档操作组件DocX(.NET组件介绍之一)
如何在IIS中新建站点并配置端口与物理路径?
网站制作网站,深圳做网站哪家比较好?
如何在云主机快速搭建网站站点?
,南京靠谱的征婚网站?
微网站制作教程,我微信里的网站怎么才能复制到浏览器里?
nginx修改上传文件大小限制的方法
建站上市公司网站建设方案与SEO优化服务定制指南
Python lxml的etree和ElementTree有什么区别
建站之星安装提示数据库无法连接如何解决?
php条件判断怎么写_ifelse和switchcase的使用区别【对比】
如何通过虚拟主机快速完成网站搭建?
如何通过wdcp面板快速创建网站?
c++怎么使用类型萃取type_traits_c++ 模板元编程类型判断【方法】
如何选择可靠的免备案建站服务器?
建站之星如何快速更换网站模板?
学校建站服务器如何选型才能满足性能需求?
如何选择建站程序?包含哪些必备功能与类型?
如何通过虚拟主机空间快速建站?
建站之星安全性能如何?防护体系能否抵御黑客入侵?
表情包在线制作网站免费,表情包怎么弄?
免费的流程图制作网站有哪些,2025年教师初级职称申报网上流程?
网站设计制作公司地址,网站建设比较好的公司都有哪些?
宝塔新建站点为何无法访问?如何排查?
如何通过建站之星自助学习解决操作问题?
,网页ppt怎么弄成自己的ppt?
香港服务器网站测试全流程:性能评估、SEO加载与移动适配优化
建站主机与服务器功能差异如何区分?
建站之星后台密码如何安全设置与找回?
如何在建站之星网店版论坛获取技术支持?
网站制作怎么样才能赚钱,用自己的电脑做服务器架设网站有什么利弊,能赚钱吗?
广东企业建站网站优化与SEO营销核心策略指南
mc皮肤壁纸制作器,苹果平板怎么设置自己想要的壁纸我的世界?
c# 在ASP.NET Core中管理和取消后台任务
建站之家VIP精选网站模板与SEO优化教程整合指南
如何快速搭建高效服务器建站系统?
购物网站制作费用多少,开办网上购物网站,需要办理哪些手续?
专业制作网站的公司哪家好,建立一个公司网站的费用.有哪些部分,分别要多少钱?
行程制作网站有哪些,第三方机票电子行程单怎么开?
香港服务器租用费用高吗?如何避免常见误区?
如何解决ASP生成WAP建站中文乱码问题?
建站之星会员如何解锁更多建站功能?
制作门户网站的参考文献在哪,小说网站怎么建立?
在线ppt制作网站有哪些,请推荐几个好的课件下载的网站?
*请认真填写需求信息,我们会在24小时内与您取得联系。