【发布时间】:2022-01-09 01:41:25
【问题描述】:
我在更改标签时遇到了不想要的视觉效果。我想要的是 .policyCentre__content 部分慢慢淡出,并且也慢慢淡入相关部分。但是,当您最初更改选项卡时,如果这是第一次加载匹配的policyCentre__content,它将闪烁而不是消失。
查看重现步骤:
- 运行以下演示(您将从激活“选项卡 1”开始。
- 单击“选项卡 2”并观察内容显示(“选项卡 1”内容淡出,但“选项卡 2”的内容仅出现。
- 点击回到“标签 1”,内容消失并按预期显示。
- 点击返回到“选项卡 2”,它现在可以正常工作了。
不确定为什么第二步最初只是让内容出现?
$(function() {
function showContent(val){
$(".policyCentre__content.active").fadeOut(500, function() {
$(this).removeClass("active");
$(window).scrollTop(0);
$(".policyCentre__content[data-item='" + val + "']").addClass('active').fadeIn(500, function() {
locked = false;
})
});
}
// prevent the UI from getting over-clicked
let locked = false;
$(".policyCentre__label:first, .policyCentre__content:first").addClass("active");
$('.policyCentre__label').click(function() {
if (locked) return;
locked = true;
var id = $(this).attr('data-item');
$(".policyCentre__label").removeClass("active");
$(this).addClass("active");
showContent(id);
});
});
.policyCentre {
border: 1px solid black;
padding: 60px 0;
}
.policyCentre__label {
display: inline-block;
cursor: pointer;
position: relative;
margin-bottom: 10px;
width: fit-content;
display: flex;
align-items: center;
}
.policyCentre__label:hover, .policyCentre__label.active {
color: orange;
}
.policyCentre__content {
display: none;
padding-left: 50px;
}
.policyCentre__content.active {
display: block;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<section class="policyCentre">
<div class="container">
<div class="row justify-content-between">
<div class="col-2">
<div class="policyCentre__tabs">
<span class="policyCentre__label" data-item="tab-1">Tab 1</span>
<span class="policyCentre__label" data-item="tab-2">Tab 2</span>
<span class="policyCentre__label" data-item="tab-3">Tab 3</span>
</div>
</div>
<div class="col-10">
<div class="policyCentre__content" data-item="tab-1">Copy for tab 1</div>
<div class="policyCentre__content" data-item="tab-2">Copy for tab 2</div>
<div class="policyCentre__content" data-item="tab-3">Copy for tab 3</div>
</div>
</div>
</div>
</section>
【问题讨论】:
标签: javascript html jquery css