多Tab点击切换效果实现

效果:
在这里插入图片描述

代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>多Tab点击切换</title>
    <style>
        *{
            margin:0;
            padding:0;
        }
        #tab li{
            float: left;
            list-style: none;
            width: 80px;
            height: 40px;
            line-height: 40px;
            cursor:pointer;
            text-align: center;
            padding: 0 20px;
        }
        #tab1{
            background-color: teal;
        }
        #tab2{
            background-color: tomato;
        }
        #tab3{
            background-color: thistle;
        }
        
        
        #contenter{
            width: 360px;
            height: 200px;
            /* border: tan solid 2px; */
            position: relative; 
            top: 40px;
        }
        #contenter #content1{
            width: 100%;
            height: 100%;
            position: absolute;
            background-color: teal;
        }
        #contenter #content2{
            width: 100%;
            height: 100%;
            position: absolute;
            background-color: tomato;
            display: none;
        }
         #contenter #content3{
             width: 100%;
             height: 100%;
             position: absolute;
             background-color: thistle;
             display: none;
         }


    </style>
</head>
<body>
    <h2>多Tab点击切换</h2>
    <ul id='tab'>
        <li id='tab1' value='1'>10元套餐</li>
        <li id='tab2' value='2'>30元套餐</li>
        <li id='tab3' value='3'>50元套餐</li>
    </ul>
    <div id='contenter'>
        <div id = 'content1'>
            10元套餐详情: <br/>每月套餐内拨打100分钟,超出部分2毛/分钟
        </div>
        <div id = 'content2'>
            30元套餐详情: <br/>每月套餐内拨打300分钟,超出部分1.5毛/分钟
        </div>
        <div id = 'content3'>
            50元套餐详情: <br/>随便打
        </div>
    </div>

    <script type='text/javascript' src='./jquery库/jquery-1.12.4.js'></script>
    <script>
        var $contents = $('#contenter>div')
        // 给li添加监听
        // jQuery自动遍历给每一个li添加监听事件——隐式遍历
        $('#tab>li').click(function (){
            // 隐藏所有div
           $contents.css('display','none')
        
           var index =$(this).index()
           $contents[index].style.display = 'block'
        //或者使用jQuery:  $($contents[index]).css('display','block')
      
        })
    </script>

    
</body>
</html>

改进:没有必要所有页面进行隐藏,应该选择显示的页面进行隐藏。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>多Tab点击切换</title>
    <style>
        *{
            margin:0;
            padding:0;
        }
        #tab li{
            float: left;
            list-style: none;
            width: 80px;
            height: 40px;
            line-height: 40px;
            cursor:pointer;
            text-align: center;
            padding: 0 20px;
        }
        #tab1{
            background-color: teal;
        }
        #tab2{
            background-color: tomato;
        }
        #tab3{
            background-color: thistle;
        }
        
        
        #contenter{
            width: 360px;
            height: 200px;
            /* border: tan solid 2px; */
            position: relative; 
            top: 40px;
        }
        #contenter #content1{
            width: 100%;
            height: 100%;
            position: absolute;
            background-color: teal;
        }
        #contenter #content2{
            width: 100%;
            height: 100%;
            position: absolute;
            background-color: tomato;
            display: none;
        }
         #contenter #content3{
             width: 100%;
             height: 100%;
             position: absolute;
             background-color: thistle;
             display: none;
         }


    </style>
</head>
<body>
    <h2>多Tab点击切换</h2>
    <ul id='tab'>
        <li id='tab1' value='1'>10元套餐</li>
        <li id='tab2' value='2'>30元套餐</li>
        <li id='tab3' value='3'>50元套餐</li>
    </ul>
    <div id='contenter'>
        <div id = 'content1'>
            10元套餐详情: <br/>每月套餐内拨打100分钟,超出部分2/分钟
        </div>
        <div id = 'content2'>
            30元套餐详情: <br/>每月套餐内拨打300分钟,超出部分1.5/分钟
        </div>
        <div id = 'content3'>
            50元套餐详情: <br/>随便打
        </div>
    </div>

    <script type='text/javascript' src='./jquery库/jquery-1.12.4.js'></script>
    <script>
        var $contents = $('#contenter>div')
        var currentIndex = 0
        // 给li添加监听
        // jQuery自动遍历给每一个li添加监听事件——隐式遍历
        $('#tab>li').click(function (){       
           $contents[currentIndex].style.display = 'none'
           var index =$(this).index()
           $contents[index].style.display = 'block'
           currentIndex = index  
        })
    </script>   
</body>
</html>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值