利用jQuery实现滑动开关按钮效果(附demo源码下载)


Posted in Javascript onFebruary 07, 2017

首先来看看要实现的效果图:

利用jQuery实现滑动开关按钮效果(附demo源码下载)

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;
}

实例下载:点击此处

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流。

Javascript 相关文章推荐
突发奇想的一个jquery插件
Nov 19 Javascript
鼠标移到图片上变大显示而不是放大镜效果
Jun 15 Javascript
JavaScript中使用arguments获得函数传参个数实例
Aug 27 Javascript
jQuery进行组件开发完整实例
Dec 15 Javascript
js+flash实现的5图变换效果广告代码(附演示与demo源码下载)
Apr 01 Javascript
Vuex之理解Getters的用法实例
Apr 19 Javascript
vuejs2.0子组件改变父组件的数据实例
May 10 Javascript
require.js中的define函数详解
Jul 10 Javascript
详解vue2.0+axios+mock+axios-mock+adapter实现登陆
Jul 19 Javascript
微信小程序http连接访问解决方案的示例
Nov 05 Javascript
解决vue.js提交数组时出现数组下标的问题
Nov 05 Javascript
Nuxt的路由配置和参数传递方式
Nov 06 Javascript
原生js和css实现图片轮播效果
Feb 07 #Javascript
bootstrap输入框组使用方法
Feb 07 #Javascript
angularjs使用directive实现分页组件的示例
Feb 07 #Javascript
Bootstrap下拉菜单样式
Feb 07 #Javascript
基于JavaScript实现购物车功能
Feb 07 #Javascript
js实现类bootstrap模态框动画
Feb 07 #Javascript
canvas实现探照灯效果
Feb 07 #Javascript
You might like
php安装swoole扩展的方法
2015/03/19 PHP
3种php生成唯一id的方法
2015/11/23 PHP
PHP封装请求类实例分析【基于Yii框架】
2019/10/17 PHP
js常用函数 不错
2006/09/08 Javascript
jquery 插件学习(一)
2012/08/06 Javascript
json的定义、标准格式及json字符串检验
2014/05/11 Javascript
JavaScript字符串对象replace方法实例(用于字符串替换或正则替换)
2014/10/16 Javascript
javascript控制层显示或隐藏的方法
2015/07/22 Javascript
javascript中call apply 与 bind方法详解
2016/03/10 Javascript
Js获取图片原始宽高的实现代码
2016/05/17 Javascript
基于iscroll.js实现下拉刷新和上拉加载效果
2016/11/28 Javascript
jQuery插件FusionCharts实现的Marimekko图效果示例【附demo源码】
2017/03/24 jQuery
微信小程序中input标签详解及简单实例
2017/05/18 Javascript
关于TypeScript模块导入的那些事
2018/06/12 Javascript
javascript实现考勤日历功能
2018/11/29 Javascript
详解Jest结合Vue-test-utils使用的初步实践
2019/06/27 Javascript
微信小程序实现点击图片放大预览
2019/10/21 Javascript
javascript实现视频弹幕效果(两个版本)
2019/11/28 Javascript
JavaScript 绘制饼图的示例
2021/02/19 Javascript
Python中用pycurl监控http响应时间脚本分享
2015/02/02 Python
通过pycharm使用git的步骤(图文详解)
2019/06/13 Python
python 按钮点击关闭窗口的实现
2020/03/04 Python
解决运行django程序出错问题 'str'object has no attribute'_meta'
2020/07/15 Python
OpenCV图片漫画效果的实现示例
2020/08/18 Python
Python 实现国产SM3加密算法的示例代码
2020/09/21 Python
python实现无边框进度条的实例代码
2020/12/30 Python
Jogun Shop中文官网:韩国知名时尚男装网站
2016/10/12 全球购物
27个经典Linux面试题及答案,你知道几个?
2013/01/10 面试题
土建资料员岗位职责
2014/01/04 职场文书
汉语言文学职业规划
2014/02/14 职场文书
标准化管理实施方案
2014/02/25 职场文书
《美丽的南沙群岛》教学反思
2014/04/27 职场文书
联片教研活动总结
2014/07/01 职场文书
党代会心得体会
2014/09/04 职场文书
2019 入党申请书范文
2019/07/10 职场文书
mysql 生成连续日期及变量赋值
2022/03/20 MySQL