iis服务器助手广告广告
返回顶部
首页 > 资讯 > 前端开发 > html >如何使用jQuery实现滑动开关按钮效果
  • 198
分享到

如何使用jQuery实现滑动开关按钮效果

2024-04-02 19:04:59 198人浏览 薄情痞子
摘要

这篇文章主要为大家展示了“如何使用Jquery实现滑动开关按钮效果”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“如何使用jQuery实现滑动开关按钮效果”这篇文

这篇文章主要为大家展示了“如何使用Jquery实现滑动开关按钮效果”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“如何使用jQuery实现滑动开关按钮效果”这篇文章吧。

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

如何使用jQuery实现滑动开关按钮效果

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><!--容器 结束-->

初始化函数:


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:


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

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


function loadUrl(iframe, url) {
 $(iframe).attr('src', url);
}


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


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


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实现滑动开关按钮效果”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注编程网html频道!

--结束END--

本文标题: 如何使用jQuery实现滑动开关按钮效果

本文链接: https://www.lsjlt.com/news/67647.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • 如何使用jQuery实现滑动开关按钮效果
    这篇文章主要为大家展示了“如何使用jQuery实现滑动开关按钮效果”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“如何使用jQuery实现滑动开关按钮效果”这篇文...
    99+
    2024-04-02
  • 原生JS如何实现滑动按钮效果
    小编给大家分享一下原生JS如何实现滑动按钮效果,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!利用Js制作的滑动按钮的具体代码,供大家参考,具体内容如下首先贴上效果...
    99+
    2023-06-25
  • 原生JS实现滑动按钮效果
    利用Js制作的滑动按钮的具体代码,供大家参考,具体内容如下 首先贴上效果图 再贴上源码 <!DOCTYPE html> <html lang="en">...
    99+
    2024-04-02
  • label+input如何实现按钮开关切换效果
    这篇文章主要介绍label+input如何实现按钮开关切换效果,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!代码如下所示:<!DOCTYPE html> <...
    99+
    2024-04-02
  • C#实现滑动开关效果
    C#重绘checkbox生成滑动开关,供大家参考,具体内容如下 通过调用checkbox控件的paint事件,在重绘事件里判断checked属性,如果选中绘制选中图形,如果未选中绘制...
    99+
    2024-04-02
  • jquery实现移动端按钮组左右滑动
    本文实例为大家分享了jquery实现移动端按钮组左右滑动的具体代码,供大家参考,具体内容如下 学习移动端触摸相关功能时写了一个例子,共享一下,其中最不好理解的是screen、page...
    99+
    2024-04-02
  • 如何使用CSS实现开关按钮
    这篇文章给大家分享的是有关如何使用CSS实现开关按钮的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。   HTML   需要用到的HTML并不是我们之前没见过的,也就是一个标准...
    99+
    2024-04-02
  • 如何使用jQuery实现鼠标点击左右按钮滑动切换
    这篇文章主要为大家展示了“如何使用jQuery实现鼠标点击左右按钮滑动切换”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“如何使用jQuery实现鼠标点击左右按钮...
    99+
    2024-04-02
  • jQuery如何实现图片滑动效果
    这篇文章主要为大家展示了“jQuery如何实现图片滑动效果”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“jQuery如何实现图片滑动效果”这篇文章吧。思路:当鼠...
    99+
    2024-04-02
  • 如何使用纯CSS实现3D按钮效果
    小编给大家分享一下如何使用纯CSS实现3D按钮效果,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!css巧妙利用了box-shadow来实现3D物体的立体感,当按钮...
    99+
    2023-06-08
  • CSS3如何模拟IOS实现滑动开关效果
    这篇文章主要介绍CSS3如何模拟IOS实现滑动开关效果,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!前言H5站点需要IOS滑动按钮的效果,想了想似乎CSS3能搞起,就折腾出来了......
    99+
    2024-04-02
  • 如何使用jquery实现图片轮播和滑动效果
    这篇文章主要介绍了如何使用jquery实现图片轮播和滑动效果,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。具体内容如下实习做了一个简易的图片轮播效果下图是做出来的效果源码ht...
    99+
    2023-06-26
  • jquery怎么实现移动端按钮组左右滑动
    本文小编为大家详细介绍“jquery怎么实现移动端按钮组左右滑动”,内容详细,步骤清晰,细节处理妥当,希望这篇“jquery怎么实现移动端按钮组左右滑动”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。学习移动端触摸...
    99+
    2023-06-29
  • jquery实现滑动楼梯效果
    本文实例为大家分享了jquery实现滑动楼梯效果的具体代码,供大家参考,具体内容如下 思路:鼠标滚动的时候页面跟随变化,点击模块时候,实现指哪打哪效果 代码的实现 1.html和cs...
    99+
    2024-04-02
  • 如何使用CSS制作平滑过渡效果的按钮
    CSS是网页开发中不可或缺的一部分,通过运用CSS可以实现各种各样的效果,其中之一就是平滑过渡效果的按钮。当用户鼠标悬停在按钮上时,按钮能够以某种方式呈现平滑的过渡效果,这可以增强用户体验和网页的视觉吸引力。本文将介绍如何使用CSS实现平滑...
    99+
    2023-10-21
    CSS 按钮 平滑过渡
  • 如何使用CSS实现发光的按钮效果
    这篇“如何使用CSS实现发光的按钮效果”文章,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要参考一下,对于“如何使用CSS实现发光的按钮效果”,小编整理了以下知识点,请大家跟着小编的...
    99+
    2024-04-02
  • 怎么用纯css3实现黑色炫酷开关按钮动画效果
    本文小编为大家详细介绍“怎么用纯css3实现黑色炫酷开关按钮动画效果”,内容详细,步骤清晰,细节处理妥当,希望这篇“怎么用纯css3实现黑色炫酷开关按钮动画效果”文章能帮助大家解决疑惑,下面跟着小编的思路慢...
    99+
    2024-04-02
  • CSS3怎么实现动画按钮效果
    这篇文章主要介绍“CSS3怎么实现动画按钮效果”,在日常操作中,相信很多人在CSS3怎么实现动画按钮效果问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”CSS3怎么实现动画按钮...
    99+
    2024-04-02
  • 如何用CSS实现平滑滚动到顶部按钮
    如何用CSS实现平滑滚动到顶部按钮在网页设计中,为了提高用户体验,让用户可以快速回到页面顶部是非常重要的。而通过实现一个平滑滚动到顶部的按钮,可以使用户回到顶部的过程更加流畅和美观。本文将介绍如何使用CSS来实现这个功能,并提供具体的代码示...
    99+
    2023-11-21
    CSS 平滑滚动 顶部按钮
  • 如何用CSS实现平滑滚动到底部按钮
    如何用CSS实现平滑滚动到底部按钮在网页设计中,为了提升用户体验,我们经常需要添加一些便捷的功能,比如回到页面顶部或滚动到底部的按钮。本文将详细介绍如何使用CSS实现一个平滑滚动到底部按钮,并提供具体的代码示例。首先,我们需要在HTML中添...
    99+
    2023-11-21
    滚动 CSS 底部按钮
软考高级职称资格查询
推荐阅读
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作