tab动态切换JS代码hover

本文介绍使用 jQuery 实现 tab 切换效果的方法,包括如何获取当前元素的索引、添加或移除 CSS 类以及控制元素的显示与隐藏。通过具体示例代码展示了 hover 事件在实现 tab 功能中的应用。

一、用到的函数

一般在hover里编写切换代码

$('').hover(function(){  

 })

二、常用的代码:

1、取得当前元素的index

代码示例:index = $('.tab-h3-item').index($(this));

2、添加或去掉css

代码示例:

$('.tab-h3-home').removeClass('sel');
$(this).addClass('sel');

3、隐藏或显示元素或DIV块

$('.tn-list').hide();
$('#list'+index).show();

三、完整的代码示例:

$('.tab-h3-item').hover(function(){
            index = $('.tab-h3-item').index($(this));
            
            //选中当前tab
            
            $('.tab-h3-item').removeClass('sel');
            $('.tab-h3-home').removeClass('sel');
            $(this).addClass('sel');
            
            $('.tn-list').hide();
            $('#list'+index).show();
        },function(){

    })


    $('.tab-h3-home').hover(function(){        
            //选中当前tab
            $('.tab-h3-item').removeClass('sel');
            $('.tab-h3-home').removeClass('sel');
            $(this).addClass('sel');
            
            $('.tn-list').hide();
            $('#list').show();
        },function(){

    })

以下是一个简单的 tab切换的示例代码HTML 代码: ```html <div class="tab"> <button class="tablinks" onclick="openTab(event, 'tab1')">Tab 1</button> <button class="tablinks" onclick="openTab(event, 'tab2')">Tab 2</button> <button class="tablinks" onclick="openTab(event, 'tab3')">Tab 3</button> </div> <div id="tab1" class="tabcontent"> <h3>Tab 1</h3> <p>This is the content of Tab 1.</p> </div> <div id="tab2" class="tabcontent"> <h3>Tab 2</h3> <p>This is the content of Tab 2.</p> </div> <div id="tab3" class="tabcontent"> <h3>Tab 3</h3> <p>This is the content of Tab 3.</p> </div> ``` CSS 代码: ```css .tab { overflow: hidden; border: 1px solid #ccc; background-color: #f1f1f1; } .tab button { background-color: inherit; float: left; border: none; outline: none; cursor: pointer; padding: 14px 16px; transition: 0.3s; } .tab button:hover { background-color: #ddd; } .tab button.active { background-color: #ccc; } .tabcontent { display: none; padding: 6px 12px; border: 1px solid #ccc; border-top: none; } ``` JavaScript 代码: ```javascript function openTab(evt, tabName) { var i, tabcontent, tablinks; tabcontent = document.getElementsByClassName("tabcontent"); for (i = 0; i < tabcontent.length; i++) { tabcontent[i].style.display = "none"; } tablinks = document.getElementsByClassName("tablinks"); for (i = 0; i < tablinks.length; i++) { tablinks[i].className = tablinks[i].className.replace(" active", ""); } document.getElementById(tabName).style.display = "block"; evt.currentTarget.className += " active"; } ``` 可以看到,以上代码首先定义了一个 `.tab` 的容器和多个 `.tablinks` 按钮,以及多个 `.tabcontent` 的内容区域。当点击某个按钮时,会触发 `openTab()` 函数,该函数会根据传入的 `tabName` 参数显示对应的内容区域,并将该按钮设置为 `active`,同时隐藏其他内容区域和按钮的 `active` 状态。具体实现方法是通过调整元素的 `display` 和 `className` 属性来实现。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值