gpt4 book ai didi

jQuery 对多个对象具有相同的功能

转载 作者:行者123 更新时间:2023-12-01 06:10:00 26 4
gpt4 key购买 nike

我有两个选项卡导航,我喜欢并排运行它们。第一个本身效果很好,但是当我复制它时,我遇到了问题。

如何使此选项卡导航并排工作?

任何帮助将不胜感激。

$(document).ready(function() {

// configurator tabs
$('.tab-link').click(function() {
var tab_id = $(this).attr('data-tab');
var tab_id_input = $(this).children("input");

$('ul.tabs li').removeClass('current');
$('ul.tabs li input').prop('checked', false).removeAttr('checked');
$('.variant--group').removeClass('current');

$("." + tab_id).addClass('current');
$(tab_id_input).prop('checked', true).attr('checked', 'checked');
$("#" + tab_id).addClass('current');

$('.variant--group.current').prevAll().removeClass('slide-left slide-right').addClass('slide-left');
$('.variant--group.current').nextAll().removeClass('slide-left slide-right').addClass('slide-right');
})

// arrow navigation
$('#previous').click(function() {
$('.variant--group.current').prevAll().removeClass('slide-left slide-right').addClass('slide-left');
if ($('ul.tabs li.current').prev().length != 0) {
$('ul.tabs li.current').removeClass('current').prev().addClass('current').click();
}
})

$('#next').click(function() {
$('.variant--group.current').nextAll().removeClass('slide-left slide-right').addClass('slide-right');
if ($('ul.tabs li.current').next().length != 0) {
$('ul.tabs li.current').removeClass('current').next().addClass('current').click();
}
})


$('#resp-button').click(function() {
$('.product--detail-upper').addClass("show");
$('body').addClass("fixed");
})

$('.product--detail-upper').on('click', function(e) {
if (e.target !== this)
return;
$('.product--detail-upper').removeClass("show");
$('body').removeClass("fixed");
});

// keyboard navigation
$("body").keydown(function(e) {
if (e.keyCode == 37) { // left
$('.variant--group.current').prevAll().removeClass('slide-left slide-right').addClass('slide-left');
if ($('ul.tabs li.current').prev().length != 0) {
$('ul.tabs li.current').removeClass('current').prev().addClass('current').click();
}
} else if (e.keyCode == 39) { // right
$('.variant--group.current').nextAll().removeClass('slide-left slide-right').addClass('slide-right');
if ($('ul.tabs li.current').next().length != 0) {
$('ul.tabs li.current').removeClass('current').next().addClass('current').click();
}
}
});


});
.body {
background: #111;
}

.tabscontain {
width: 100%;
height: 60px;
}
.tabs li {
background: #f6f7f9;
border-bottom: 1px solid #e8e8e8;
display: inline-block;
padding: 0 30px;
margin-right: -5px;
font-weight: 700;
font-size: 14px;
line-height: 60px;
cursor: pointer;
text-transform: uppercase;
}
.tabs li.current {
background: #ffffff;
border-color: #ffffff;
color: #09c;
}
.arrows {
position: absolute;
right: 40px;
top: -130px;
}
.arrow {
background: #30373b;
color: #fff;
padding: 10px 12px;
font-size: 20px;
display: inline-block;
cursor: pointer;
}
.arrow:active {
background: #222222;
}
.variant--group {
display: none;
min-height: 190px;
padding: 60px 30px;
text-align: center;
}
.variant--group.current {
display: block;
opacity: 1;
}
.variant--group.slide-left.current {
opacity: 0;
transform: translateX(-100px);
animation: slide-left 0.4s ease forwards;
}
@-webkit-keyframes slide-left {
to {
transform: translateX(0);
opacity: 1;
}
}
@-moz-keyframes slide-left {
to {
transform: translateX(0);
opacity: 1;
}
}
@-o-keyframes slide-left {
to {
transform: translateX(0);
opacity: 1;
}
}
@keyframes slide-left {
to {
transform: translateX(0);
opacity: 1;
}
}
.variant--group.slide-right.current {
opacity: 0;
transform: translateX(100px);
animation: slide-right 0.4s ease forwards;
}
@-webkit-keyframes slide-right {
to {
transform: translateX(0);
opacity: 1;
}
}
@-moz-keyframes slide-right {
to {
transform: translateX(0);
opacity: 1;
}
}
@-o-keyframes slide-right {
to {
transform: translateX(0);
opacity: 1;
}
}
@keyframes slide-right {
to {
transform: translateX(0);
opacity: 1;
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<!-- First Tab -->
<div class="custom-slide">

<div class="arrows slide-left">
<div id="previous" class="arrow">Zurück</div>
<div id="next" class="arrow">Weiter</div>
</div>

<div class="tabscontain slide-left">
<ul class="tabs">

<li class="tab-link 1 current" data-tab="1">option 1</li>
<li class="tab-link 2" data-tab="2">option 2</li>
<li class="tab-link 3" data-tab="3">option 3</li>

</ul>
</div>

<div class="variant--group slide-left current" id="1">
<div class="descript">
<h2>option 1</h2>
<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam</p>
</div>
</div>

<div class="variant--group slide-left current" id="2">
<div class="descript">
<h2>option 2</h2>
<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam</p>
</div>
</div>

<div class="variant--group slide-left current" id="3">
<div class="descript">
<h2>option 3</h2>
<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam</p>
</div>
</div>

</div>



<!-- Second Tab -->
<div class="custom-slide">

<div class="arrows slide-left">
<div id="previous" class="arrow">Zurück</div>
<div id="next" class="arrow">Weiter</div>
</div>

<div class="tabscontain slide-left">
<ul class="tabs">

<li class="tab-link 4 current" data-tab="4">option 3</li>
<li class="tab-link 5" data-tab="5">option 4</li>
<li class="tab-link 6" data-tab="6">option 5</li>

</ul>
</div>

<div class="variant--group slide-left current" id="4">
<div class="descript">
<h2>option 4</h2>
<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam</p>
</div>
</div>

<div class="variant--group slide-left current" id="5">
<div class="descript">
<h2>option 5</h2>
<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam</p>
</div>
</div>

<div class="variant--group slide-left current" id="6">
<div class="descript">
<h2>option 6</h2>
<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam</p>
</div>
</div>

</div>

最佳答案

如果您想要两个独立的 slider ,则需要解决以下问题:

  1. 第二个 slider 的范围不正确
  2. 每个选项卡的内容最初应隐藏
  3. 关键事件将不可避免地影响两个 slider

但主要问题是您的代码需要每个 slider 的引用,以便独立管理它们。您可以使用 custom-slide 类作为包装器并仅为其子级设置动画:

var slide = $(this).closest('.custom-slide');

我修剪了您的代码并创建了一个小示例:

JSFIDDLE

关于jQuery 对多个对象具有相同的功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41173751/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com