利用jQuery實(shí)現(xiàn)滑動(dòng)開關(guān)按鈕效果(附demo源碼下載)
首先來看看要實(shí)現(xiàn)的效果圖:
HTML結(jié)構(gòu)如下:
<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小時(shí)</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小時(shí)</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小時(shí)</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小時(shí)</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">站點(diǎn)</a> </td> <td class="switch_box_r"></td> </tr> </table> </div> </div><!--容器 結(jié)束-->
初始化函數(shù):
/*@.boxwrap :滑動(dòng)按鈕父容器,同一界別的滑動(dòng)按鈕必須包含在同一個(gè)容器中 *@loadData :點(diǎn)擊按鈕后回調(diào)函數(shù) *@#frameMain : 加載內(nèi)容的iframe Id *@tab.html : 提交參數(shù)的頁面 */ 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小時(shí)</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小時(shí)</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小時(shí)</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小時(shí)</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">站點(diǎn)</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(){ //初始化滑動(dòng)div loadSwitchBox('.boxwrap',loadData,'#frameMain','tab.html'); }); //點(diǎn)擊回調(diào)函數(shù) 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:
/** * 滑動(dòng)div初始化函數(shù) * @wrap 所屬頂層容器 * @fn 點(diǎn)擊后回調(diào)函數(shù) * @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); } /** *初始化數(shù)據(jù) *@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 加載內(nèi)容頁面 *@url url */ function loadUrl(iframe, url) { $(iframe).attr('src', url); } /** * 創(chuàng)建一個(gè)滑動(dòng)div容器 * @wrapperId 容器ID * @type 切換標(biāo)簽的類型 * @fnCallBack 回調(diào)函數(shù) */ function createSlideDiv(wrapperId, type, fnCallBack) { calTabWidth(wrapperId); //初始化容器寬度 var $wraper = $('#' + wrapperId); //默認(rèn)選中第一項(xiàng) var default_padding = 5; var default_BtnWidth = $wraper.find('.swichTxt').eq(0).width() + default_padding; var $switchBtn = $wraper.find('.switchBtn'); $switchBtn.width(default_BtnWidth); //浮動(dòng)按鈕寬度 $wraper.find('.swichTxt').eq(0).addClass('cur'); //設(shè)置選中選項(xiàng) $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); //計(jì)算滑動(dòng)距離 //回調(diào)函數(shù)參數(shù) 點(diǎn)擊標(biāo)簽的類型 和 值 var obj = { type: type, value: value }; //左移動(dòng) if (newIndex > oldIndex) { $wraper.find(".switchBtn").animate({ 'left': '+=' + distant + 'px' }, function() { $wraper.find('.curTxt').html(curTxt); $wraper.find("#switchBtn").width(eleWidth); //執(zhí)行回調(diào)函數(shù) if ($.isFunction(fnCallBack)) fnCallBack(obj); }); oldIndex = newIndex; } else if (newIndex < oldIndex) { //右移動(dòng) $wraper.find(".switchBtn").animate({ 'left': '-=' + distant + 'px' }, function() { $(this).find('.curTxt').html(curTxt); $wraper.find(".switchBtn").width(eleWidth); //執(zhí)行回調(diào)函數(shù) if ($.isFunction(fnCallBack)) fnCallBack(obj); }); oldIndex = newIndex; } $wraper.find('.cur').removeClass('cur'); $(this).addClass('cur'); }); } /** *計(jì)算容器寬度 * @wrapperId 容器id */ function calTabWidth(wrapperId) { var $wraper = $('#' + wrapperId); var wrapperWidth = 0; var tdSpace = 18; //左右圓角寬度 var oPadding = 5; //元素默認(rèn)間距 $wraper.find('.swichTxt').each(function(i) { wrapperWidth += $(this).outerWidth() + oPadding; }); wrapperWidth = wrapperWidth + tdSpace; $wraper.width(wrapperWidth); } /** *計(jì)算按鈕移動(dòng)距離 * @wrapperId 容器id * @oldIndex 之前選中的選項(xiàng)索引 * @newIndex 當(dāng)前點(diǎn)擊選項(xiàng)索引 */ function moveWidth(wrapperId, oldIndex, newIndex) { var $wraper = $('#' + wrapperId); var width = 0; //向右移動(dòng) if (oldIndex < newIndex) { var $s_btn = $wraper.find(".switchBtn"); var $a_btn = $wraper.find('.swichTxt').eq(newIndex - 1); //當(dāng)前span左側(cè)位置離左邊的距離 var curBtn = $s_btn.offset().left; //目標(biāo)標(biāo)簽左側(cè)位置離左邊的距離 var einA = $a_btn.offset().left; width = parseInt(einA - curBtn + 9); //? } else { //向左移動(dòng) var $s_btn = $wraper.find(".switchBtn"); var $a_btn = $wraper.find('.swichTxt').eq(newIndex - 1); //當(dāng)前span左側(cè)位置離左邊的距離 var curBtn = $s_btn.offset().left; //目標(biāo)標(biāo)簽左側(cè)位置離左邊的距離 var einA = $a_btn.offset().left; width = parseInt(curBtn - einA); } return width; }
實(shí)例下載:點(diǎn)擊此處
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。
相關(guān)文章
淺談EasyUi ComBotree樹修改 父節(jié)點(diǎn)選擇的問題
下面小編就為大家?guī)硪黄獪\談EasyUi ComBotree樹修改 父節(jié)點(diǎn)選擇的問題。2016-11-11不用錨點(diǎn)也可以平滑滾動(dòng)到頁面的指定位置實(shí)現(xiàn)代碼
在不適用錨點(diǎn)的情況下也可以平滑滾動(dòng)到頁面的指定位置,看樣子還不錯(cuò)嗎,具體處理程序如下,感興趣的朋友可以參考下哈,希望對你有所幫助2013-05-05jquery實(shí)現(xiàn)簡單實(shí)用的輪播器
這篇文章主要為大家詳細(xì)介紹了jquery實(shí)現(xiàn)簡單實(shí)用的輪播器制作方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05jQuery實(shí)現(xiàn)折疊、展開的菜單組效果代碼
這篇文章主要介紹了jQuery實(shí)現(xiàn)折疊、展開的菜單組效果代碼,涉及jQuery針對頁面元素屬性動(dòng)態(tài)操作的相關(guān)技巧,展開折疊效果平滑自然,需要的朋友可以參考下2015-09-09淺談原生JS實(shí)現(xiàn)jQuery的animate()動(dòng)畫示例
本篇文章主要介紹了淺談原生JS實(shí)現(xiàn)jQuery的animate()動(dòng)畫示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-03-03jQuery插件Echarts實(shí)現(xiàn)的漸變色柱狀圖
本文主要介紹了jQuery插件Echarts實(shí)現(xiàn)漸變色柱狀圖的實(shí)例。具有很好的參考價(jià)值。下面跟著小編一起來看下吧2017-03-03jQuery實(shí)現(xiàn)動(dòng)態(tài)添加tr到table的方法
這篇文章主要介紹了jQuery實(shí)現(xiàn)動(dòng)態(tài)添加tr到table的方法,涉及jQuery針對table表格元素的動(dòng)態(tài)操作相關(guān)技巧,需要的朋友可以參考下2016-12-12jQuery/$ is not defined報(bào)錯(cuò)的幾種解決方法
jQuery最常見的錯(cuò)誤之一是$ is not defined錯(cuò)誤,本文主要介紹了jQuery/$ is not defined報(bào)錯(cuò)的幾種解決方法,具有一定的參考價(jià)值,感興趣的可以了解一下2024-02-02