基于JQ的选项卡组件开发

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>JQ的选项卡组件DEMO</title>
<style>
#div1 div,#div2 div,#div3 div,#div4 div{ width:200px; height:200px; border:1px #000 solid; display:none;}
.active{ background:red;}
</style>
<script src="jquery-1.10.2.min.js"></script>
<script>

/**
    title : 基于JQ的选项卡组件

    Options : event   delay

    Methods : nowSel()   getContent()

    Events : beforeClick  afterClick

*/

//jQ中的主动触发 : trigger()

$(function(){
    //创建对象
    var t1 = new Tab();
    //父级 配置参数
    t1.init('div1',{});

    var t2 = new Tab();
    t2.init('div2',{
        event : 'mouseover'
    });

    var t3 = new Tab();
    t3.init('div3',{
        event : 'mouseover',
        delay : 200
    });

    var t4 = new Tab();
    t4.init('div4',{});
    t4.nowSel(2);

    $('#input1').click(function(){

        alert( t4.getContent() );

    });

    $(t4).on('beforeClick',function(){
        alert( t4.getContent() );
    });

    $(t4).on('afterClick',function(){
        alert( t4.getContent() );
    });

});

function Tab(){
    //属性
    this.oParent = null;
    this.aInput = null;
    this.aDiv = null;
    this.iNow = 0;
    //默认参数
    this.settings = {  //默认参数
        event : 'click', //默认点击
        delay : 0
    };
}
                            //父级 配置参数
Tab.prototype.init = function(oParent , opt){
            // 默认参数 配置参数
    $.extend( this.settings , opt );
    //获取父级及父级下的元素
    this.oParent = $('#'+oParent);
    this.aInput = this.oParent.find('input');
    this.aDiv = this.oParent.find('div');
    //调用选项卡切换的方法
    this.change();

};
//选项卡切换的方法
Tab.prototype.change = function(){

    var This = this;
    var timer = null;

    this.aInput.on(this.settings.event,function(){

        var _this = this;

        if( This.settings.event == 'mouseover' && This.settings.delay ){

            timer = setTimeout(function(){
                show(_this);
            },This.settings.delay);

        }
        else{
            show(this);
        }

    }).mouseout(function(){
        clearTimeout(timer);
    });

    function show(obj){

        $(This).trigger('beforeClick');

        This.aInput.attr('class','');
        This.aDiv.css('display','none');

        $(obj).attr('class','active');

        This.aDiv.eq( $(obj).index() ).css('display','block');

        This.iNow = $(obj).index();

        $(This).trigger('afterClick');

    }

};

Tab.prototype.nowSel = function(index){

    this.aInput.attr('class','');
    this.aDiv.css('display','none');

    this.aInput.eq(index).attr('class','active');
    this.aDiv.eq(index).css('display','block');

    this.iNow = index;

};

Tab.prototype.getContent = function(){

    return this.aDiv.eq(this.iNow).html();

};


</script>
</head>

<body>
<div id="div1">
    <input class="active" type="button" value="1">
    <input type="button" value="2">
    <input type="button" value="3">
    <div style="display:block">111111</div>
    <div>222222</div>
    <div>333333</div>
</div>

<div id="div2">
    <input class="active" type="button" value="1">
    <input type="button" value="2">
    <input type="button" value="3">
    <div style="display:block">111111</div>
    <div>222222</div>
    <div>333333</div>
</div>

<div id="div3">
    <input class="active" type="button" value="1">
    <input type="button" value="2">
    <input type="button" value="3">
    <div style="display:block">111111</div>
    <div>222222</div>
    <div>333333</div>
</div>

<div id="div4">
    <input class="active" type="button" value="1">
    <input type="button" value="2">
    <input type="button" value="3">
    <div style="display:block">111111</div>
    <div>222222</div>
    <div>333333</div>
</div>
<input type="button" value="点击" id="input1">
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值