Tab切换组件(选项卡功能)实例代码

直接贴代码里面有注释:

代码如下:

/**
 * 简单的Tab切换
 * 支持可配置项 如下参数
 */
    function Tab(){
        this.config = {
            type            : 'mouseover',    //类型 默认为鼠标移上去
            autoplay        : true,           // 默认为自动播放
            triggerCls      : '.list',        // 菜单项
            panelCls        : '.tabContent',  // 内容项
            index           : 0,              // 当前的索引0
            switchTo        : 0,              // 切换到哪一项
            interval        : 3000,              // 自动播放间隔时间 默认为3 以s为单位
            pauseOnHover    : true,           // 鼠标放上去是否为暂停 默认为true
            current         : 'current',      // 当前项添加到类名
            hidden          : 'hidden',       // 类名 默认为hidden
            callback        : null            // callback函数
        };

this.cache = {
            timer : undefined,
            flag  : true
        };
    }

Tab.prototype = {

init: function(options){
            this.config = $.extend(this.config,options || {});
            var self = this,
                _config = self.config;
            self._handler();
        },
        _handler: function(){
            var self = this,
                _config = self.config,
                _cache = self.cache,
                len = $(_config.triggerCls).length;
            $(_config.triggerCls).unbind(_config.type);
            $(_config.triggerCls).bind(_config.type,function(){
                _cache.timer && clearInterval(_cache.timer);
                var index = $(_config.triggerCls).index(this);
                !$(this).hasClass(_config.current) &&
                $(this).addClass(_config.current).siblings().removeClass(_config.current);
                $(_config.panelCls).eq(index).removeClass(_config.hidden).siblings().addClass(_config.hidden);

// 切换完 添加回调函数
                _config.callback && $.isFunction(_config.callback) && _config.callback(index);
            });

// 默认情况下切换到第几项
            if(_config.switchTo) {
                $(_config.triggerCls).eq(_config.switchTo).addClass(_config.current).siblings().removeClass(_config.current);
                $(_config.panelCls).eq(_config.switchTo).removeClass(_config.hidden).siblings().addClass(_config.hidden);
            }

// 自动播放
            if(_config.autoplay) {
                start();
                $(_config.triggerCls).hover(function(){
                    if(_config.pauseOnHover) {
                        _cache.timer && clearInterval(_cache.timer);
                        _cache.timer = undefined;
                    }else {
                        return;
                    }
                },function(){
                    start();
                });
            }
            function start(){
                _cache.timer = setInterval(autoRun,_config.interval);
            }
            function autoRun() {
                if(_config.switchTo && (_config.switchTo == len-1)){
                    if(_cache.flag) {
                        _config.index = _config.switchTo;
                        _cache.flag = false;
                    }
                }
                _config.index++;
                if(_config.index == len) {
                    _config.index = 0;
                }
                $(_config.triggerCls).eq(_config.index).addClass(_config.current).siblings().removeClass(_config.current);
                $(_config.panelCls).eq(_config.index).removeClass(_config.hidden).siblings().addClass(_config.hidden);

}
        }
    };

页面上调用方式如下:

代码如下:

$(function(){
    new Tab().init({
        switchTo: 1,
        callback: function(index){
            console.log(index);
        }
    });
});

(0)

相关推荐

  • jquery多选项卡效果实例代码(附效果图)

    复制代码 代码如下: <script type="text/javascript">    $(function() {        $(".designerspanel").each(function(){            $(this).find(".tabspanel #show:not(:first)").hide();        }); $(".designerspanel li").clic

  • 自定义jQuery选项卡插件实例

    复制代码 代码如下: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8">

  • jQuery学习笔记(3)--用jquery(插件)实现多选项卡功能

    在Web中用到多选项卡功能的网站有很多,比如163和126邮箱,用过的人知道.本人在那么多的类似插件中,目前碰到这个比较好,花了点时间调试出来了,请看效果图: 这款插件叫jqueryMagicTabs,上图实现了基本功能,如添加选项卡,选择指定的选项卡.当添加的选项卡超过一定长度时会出现左右滑动的按钮,同时支持鼠标滑动选项卡. 这段代码如下所示: 复制代码 代码如下: <%@ page language="java"contentType="text/html; cha

  • JavaScript版TAB选项卡效果实例

    复制代码 代码如下: <!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="

  • android 选项卡(TabHost)如何放置在屏幕的底部

    今天写Tab的时候由于TAB的跳转问题去查资料,倒反而发现更有趣的问题,就是如何将TAB放置在屏幕的底端. 复制代码 代码如下: <?xml version="1.0" encoding="utf-8"?> <TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/tabhost" a

  • jq选项卡鼠标延迟的插件实例

    复制代码 代码如下: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"         "http://www.w3.org/TR/html4/loose.dtd"> <html> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

  • jQuery自动切换/点击切换选项卡效果的小例子

    复制代码 代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=GBK">&

  • jsp js鼠标移动到指定区域显示选项卡离开时隐藏示例

    -1.右侧弹出导航菜单完整代码 复制代码 代码如下: <!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> <met

  • Vue2.0 多 Tab切换组件的封装实例

    Vue2.0 多 Tab切换组件简单封装,满足自己简单的功能,可以直接拿去使用! 首先上效果图: 功能简单介绍: 1.支持tab切换 2.支持tab定位 3.支持tab自动化 仿React多Tab实现,总之可以正常使用满足日常需求, 1.使用方法: ==index.vue文件== <TabItems> <div name="买入" class="first"> <Content :isContTab = "0" /&

  • jQuery实现图片随机切换、抽奖功能(实例代码)

    效果图: 源代码: HTML: <body> <!-- 小像框 --> <div id="div1"> <img id="xiaoImgID" src="img/mei0.jpg"> </div> <!-- 开始按钮 --> <input id="startID" type="button" value="开始"

  • vue组件开发之tab切换组件使用详解

    本文实例为大家分享了vue组件开发之tab切换组件的具体使用代码,供大家参考,具体内容如下 代码: <template> <div class="tab-slider"> <div class="tab"> <span v-for="(item, index) in items" v-bind:class="{active: actived == index}" @click=&quo

  • JavaScript tab选项卡插件实例代码

    今天,先从最简单的开始,将已有的一个Tab选项卡切换功能改写成javascript插件形式. 原生函数写法 将一个javascript方法改写为js插件最简单的方式就是将这个方法挂载到window全局对象下面 我们先来看看最原始的使用函数写法的代码: tab.html <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <meta http

  • 微信小程序中的列表切换功能实例代码详解

    感觉这列表切换有点类似于轮播图,而且感觉这代码直接可以拿来用,稍微改一改样式什么的就OK了,列表切换也是用到的地方也很多 wxml中的代码如下: <!-- 标签页面标题 --> <view class="tab"> <view class="tab-item {{tab==0?'active':''}}" bindtap="changeItem" data-item="0">音乐推荐<

  • JavaScript实现简单的图片切换功能(实例代码)

    代码如下所示: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>图片切换</title> <style> *{ margin: 0; padding: 0; } .box{ width: 200px; height: 300px; margin: 50px auto; padding: 20p

  • Java FTP上传下载删除功能实例代码

    在没给大家上完整代码之前先给大家说下注意点: FTP上传下载,容易出现乱码,记得转换 package com.yinhai.team.action; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; impo

  • vue下拉列表功能实例代码

    最近在弄作品,做了个下拉列表.心想各位小哥哥.小姐姐可能会用到相同的需求,就把下拉列表封装一下,希望能对各位小哥哥,小姐姐有帮助 github地址: https://github.com/ClmPisces/vue-droplist 喜欢的请反手来个star,有issue的欢迎提出 安装 cnpm install vue-droplist --save 组件中导入 import DropList from 'vue-droplist' // 显示下拉列表 showDropList() { //

  • Android基于高德地图poi的仿微信获取位置功能实例代码

    1.打开页面自动定位,同时搜索周边所有poi 2.点击搜索按钮,输入关键子,获取关键字搜索结果 3.选取listview中的一项即可定位到该位置,或者获取任何消息 4.文件类 1.MapActivity public class MapActivity extends Activity implements PoiSearch.OnPoiSearchListener { private MapView mMapView = null; private AMap aMap; private MyL

  • 移动端滑动切换组件封装 vue-swiper-router实例详解

    具体代码如下所述: <strong>组件部分</strong> <template> <div class="main"> <div class="page-tab"> <div :class="nowPath == item.path ? 'tab-item tab-item_active' : 'tab-item'" v-for='(item, index) in tabLis

随机推荐