jquery制作的 可切换Tab选项卡|菜单栏

 查看效果

 html

<!DOCTYPE html>
<head>
    <meta charset="utf-8" />
    <title>可切换Tab选项卡|菜单栏</title>
    <script type="text/javascript" src="https://code.jquery.com/jquery-3.4.0.min.js"></script>
    <!--引入jquery文件(必须呀)-->                  
</head>
<body>
    <div class="tbb">
	<p class="left" onclick="gdt(this,1)"><</p>
	<ul class="list">
            <!--选项卡列表 <li>.....</li> class="cur" 选中的-->
        </ul>
	<p class="right" onclick="gdt(this,2)">></p>
    </div>
    <div class="tabs">
         <!--选项卡内容 <div>....</div> class="show" 显示的-->
    </div>
</body>
</html>

css

*{
    padding:0px;
    margin: 0px;
    list-style: none;
}
body{
    padding: 10px;
}
.tbb{
    width: 100%;
    height: 40px;
    overflow: hidden;
    background: #494949;

    /*不能选中内的文字*/
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
    /*不能选中内的文字*/
}
.tbb .list{
    width: 99999999px;
    height: 40px;
    white-space: nowrap;
    overflow-y: hidden;
    position: relative;
    float: left;
    left: 0px;
    top: -40px;
    padding-left: 20px;   
}
.tbb .list li{
    min-width: 100px;
    height: 40px;
    line-height: 40px;
    text-align: center;
    color: white;
    float: left;
    padding: 0 10px;
    background: rgba( 32, 35, 64);
    cursor: pointer;
    transition: all 0.3s;
}
.tbb .list li:hover{
    background: rgba( 32, 35, 64);
    box-shadow: 0 0 10px white inset;
}
.tbb .list .cur{
    background:#494949;
    box-shadow: 0px 10px 0px 0px #ff0000 inset,   /*上边阴影  红色*/

               10px 0px 0px 0px #3bee17 inset,   /*左边阴影  绿色*/

              -10px 0px 0px 0px #2279ee inset,    /*右边阴影  蓝色*/

                0px 0px 0px 0px #eede15 inset;    /*下边阴影  黄色*/
}
.tbb .list .cur:hover{
    background:#494949;
    box-shadow: 0px 10px 0px 0px #ff0000 inset,   /*上边阴影  红色*/

                10px 0px 0px 0px #3bee17 inset,   /*左边阴影  绿色*/

               -10px 0px 0px 0px #2279ee inset,    /*右边阴影  蓝色*/

                0px -10px 0px 0px #eede15 inset;    /*下边阴影  黄色*/
}
.tbb .left,.tbb .right{
    cursor: pointer;
    height: 40px;
    line-height: 40px;
    width: 20px;
    font-size: 20px;
    overflow: hidden;
    z-index: 9999;
    background: #282923;
    box-shadow: 0 0 20px white;
    text-align: center;
    font-weight: bold;
    color: #fff;
    float: left; 
}
.tbb .left{
    position: relative;
    left: 0px;
    top: 0px;
}
.tbb .right{
    position: relative;
    float: right;
    right: 0px;
    top: -80px;
}
.tabs{
    width: 100%;
    background: #494949;
    box-sizing: border-box;
    padding: 10px;
    color: white;
}
.tabs div{
    width: 100%;
    display: none;
}
.tabs .show{
    display: block;
}

js

$(document).ready(function(){
    for(var i = 1; i < 40; i++){    //初始化Tab数据
        if(i == 1){   //默认选中
	    //可以变成图片 要调高
	    $(".tbb .list").append('<li class="cur">' +i+ '</li>');
	    $(".tabs").append("<div class='show'>" +i+ "</div>");
	}else{
	    $(".tbb .list").append('<li>' +i+ '</li>');
	    $(".tabs").append("<div>" +i+ "</div>");
	}
    }
    $(".tbb").contents(".list").contents("li").each(function(i,item){
        $(item).attr("onclick","liyd(this,3)");   //绑定事件
    });
});

function gdt(n , m){    //点击箭头左右
    var li = $(n).siblings(".list").contents(".cur")[0];   //当前li
    var li_index = $(li).index();   //当前li的下标
    var li_length = $(n).siblings(".list").contents("li").length;  //li 总个数
    if(m == 1){  //上一个
	if(li_index != 0){  //不是第一个
	    var now_li = $(li).prev();
	}else{ //不能往前了
	    return;
	}
    }else{   //下一个
	if(li_index < li_length - 1){   //不是最后一个
	    var now_li = $(li).next();
	}else{   //不能向后了
	    return;
	}
    }
    liyd(now_li[0], m);
}

function liyd(now_li , m = 1){   //实现移动可选中
    now_li = $(now_li);   //当前的li
    var beffor_li = now_li.siblings(".cur");  //之前选中的li

    beffor_li.attr({"class": "", "onClick": "liyd(this,3)"});  //取消选中之前的li
    now_li.attr({"class": "cur", "onClick": ""});  //选中当前的li

    var now_li_width = now_li.outerWidth(true);  //当前li宽
    var now_li_left = now_li.position().left;  //当前li离左边(ul)的距离
    var now_left = parseInt(now_li.parent(".list").css('left').replace("-","").replace("px",""));
  //获取离左边的距离
    var now_li_index = now_li.index();   //当前li的下标

    var gj = $(".tbb .left").width() * 2;   //左右箭头的总宽
    var all_width = $(".tbb").width() - gj;  //tbb可视(除去左右箭头)的宽  

    if(now_li_left + now_li_width < all_width){  //如果小于 tbb可视宽就回到 0 
	var left = 0;  //离左边的居留
    }else{   //大于
	var left = ""; //离左边的居留为空不动
	if(m == 1){  //往前
	    if( now_left > now_li_left){  //判断上一个是否被遮住  是则 移动将它显示出来
	        left = "-"+ (now_li_left - gj);   离左边的居留
            }
	}else if(m == 2){   //往后
	    if(now_li_left - now_li_width <= all_width + now_left){   //判断下一个是否被遮住  是则 移动将它显示出来
                left = "-"+ (now_li_left - all_width + now_li_width);    //离左边的居留

	    }		
        }else{
	    var beffor_li_width = beffor_li.width();   //获取之前被选中li的宽
	    var beffor_li_left = beffor_li.position().left;  //获取之前被选中li离左边(li)的距离

	    if(now_left > now_li_left){     //往前 判断上一个是否被遮住  是则 移动将它显示出来
		left = "-"+ (now_li_left - gj);

	    }else if(now_li_left + now_li_width > beffor_li_width + beffor_li_left && now_li_left + now_li_width > all_width + now_left){   //往后
		//判断下一个是否被遮住 并且不完全在tbb可视内 是则 移动将它显示出来
                left = "-"+ (now_li_left - all_width + now_li_width);

	    }
	//没有就不动
	}
    }

    //显示底下的 Tabs  隐藏之前的 显示当前的
            now_li.parent(".list").parent('.tbb').siblings(".tabs").children().removeClass("show").eq(now_li_index).addClass("show");

    if(left !== ""){ //如果 left 被赋值就改变离左边的距离
	now_li.parent(".list").animate({left:left + "px"}, 100);  //当前滚动条离左边的距离
    }
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以用以下代码实现基本的Tab选项卡切换: HTML代码: ``` <div class="tab"> <ul class="tab-menu"> <li class="active"><a href="#tab-1">Tab 1</a></li> <li><a href="#tab-2">Tab 2</a></li> <li><a href="#tab-3">Tab 3</a></li> </ul> <div class="tab-content"> <div id="tab-1" class="tab-panel active">This is tab 1 content.</div> <div id="tab-2" class="tab-panel">This is tab 2 content.</div> <div id="tab-3" class="tab-panel">This is tab 3 content.</div> </div> </div> ``` CSS代码: ``` .tab-menu { list-style: none; margin: 0; padding: 0; display: flex; } .tab-menu li { margin-right: 10px; } .tab-menu li a { display: block; padding: 10px; background-color: #eee; color: #333; text-decoration: none; } .tab-menu li.active a { background-color: #333; color: #fff; } .tab-panel { display: none; padding: 10px; } .tab-panel.active { display: block; } ``` jQuery代码: ``` $(document).ready(function() { // 初始状态下,显示第一个选项卡 $('.tab-menu li:first').addClass('active'); $('.tab-panel:first').addClass('active'); // 点击选项卡时,切换显示内容 $('.tab-menu li').click(function() { var tab_id = $(this).find('a').attr('href'); $('.tab-menu li').removeClass('active'); $('.tab-panel').removeClass('active'); $(this).addClass('active'); $(tab_id).addClass('active'); }); }); ``` 当用户点击选项卡时,通过jQuery代码来切换显示内容。首先,我们在页面加载完成后,将第一个选项卡及其内容设为初始状态下的激活状态。然后,我们通过jQuery的click()方法来监控选项卡的点击事件。当用户点击某个选项卡时,我们将该选项卡及其对应的内容设为激活状态,同时将其他选项卡及其对应的内容设为非激活状态。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值