gpt4 book ai didi

javascript - 当我将它们放在旋转木马外面时,按钮不起作用

转载 作者:行者123 更新时间:2023-12-03 06:55:21 25 4
gpt4 key购买 nike

我有这个代码,当我把 anchor (箭头img)放在外面时,.MultiCarousel div 它不起作用。我没有编写代码,但我认为我应该在 JavaScript 中更改一些内容。我想要容器 div 之外的 anchor ,但具有相同的功能。我对 JavaScript 不是很熟悉,所以我不知道要改变什么。有人可以帮忙吗?

$(document).ready(function () {
var itemsMainDiv = ('.MultiCarousel');
var itemsDiv = ('.MultiCarousel-inner');
var itemWidth = "";

$('.leftLst, .rightLst').click(function () {
var condition = $(this).hasClass("leftLst");
if (condition)
click(0, this);
else
click(1, this)
});

ResCarouselSize();

$(window).resize(function () {
ResCarouselSize();
});

//this function define the size of the items
function ResCarouselSize() {
var incno = 0;
var dataItems = ("data-items");
var itemClass = ('.item');
var id = 0;
var btnParentSb = '';
var itemsSplit = '';
var sampwidth = $(itemsMainDiv).width();
var bodyWidth = $('body').width();
$(itemsDiv).each(function () {
id = id + 1;
var itemNumbers = $(this).find(itemClass).length;
btnParentSb = $(this).parent().attr(dataItems);
itemsSplit = btnParentSb.split(',');
$(this).parent().attr("id", "MultiCarousel" + id);

if (bodyWidth >= 1200) {
incno = itemsSplit[3];
itemWidth = sampwidth / incno;
}
else if (bodyWidth >= 992) {
incno = itemsSplit[2];
itemWidth = sampwidth / incno;
}
else if (bodyWidth >= 768) {
incno = itemsSplit[1];
itemWidth = sampwidth / incno;
}
else {
incno = itemsSplit[0];
itemWidth = sampwidth / incno;
}
$(this).css({ 'transform': 'translateX(0px)', 'width': itemWidth * itemNumbers });
$(this).find(itemClass).each(function () {
$(this).outerWidth(itemWidth);
});

$(".leftLst").addClass("over");
$(".rightLst").removeClass("over");

});
}


//this function used to move the items
function ResCarousel(e, el, s) {
var leftBtn = ('.leftLst');
var rightBtn = ('.rightLst');
var translateXval = '';
var divStyle = $(el + ' ' + itemsDiv).css('transform');
var values = divStyle.match(/-?[\d\.]+/g);
var xds = Math.abs(values[4]);
if (e == 0) {
translateXval = parseInt(xds) - parseInt(itemWidth * s);
$(el).siblings(rightBtn).removeClass("over");

if (translateXval <= itemWidth / 2) {
translateXval = 0;
$(el).siblings(leftBtn).addClass("over");
}
}
else if (e == 1) {
var itemsCondition = $(el).find(itemsDiv).width() - $(el).width();
translateXval = parseInt(xds) + parseInt(itemWidth * s);
$(el).siblings(leftBtn).removeClass("over");

if (translateXval >= itemsCondition - itemWidth / 2) {
translateXval = itemsCondition;
$(el).siblings(rightBtn).addClass("over");
}
}
$(el + ' ' + itemsDiv).css('transform', 'translateX(' + -translateXval + 'px)');
}

//It is used to get some elements from btn
function click(ell, ee) {
var Parent = "#" + $(ee).siblings( '.MultiCarousel' ).attr("id");
var slide = $(Parent).attr("data-slide");
ResCarousel(ell, Parent, slide);
}
});
.MultiCarousel { 
float: left;
overflow: hidden;
padding: 15px;
width: 100%;
position:relative;
box-sizing: border-box;
}
.MultiCarousel .MultiCarousel-inner {
transition: 1s ease all;
float: left;
}
.MultiCarousel .MultiCarousel-inner .item {
float: left;
}
.MultiCarousel .MultiCarousel-inner .item > div {
text-align: center;
padding:10px;
margin:10px;
color:#666;
}
.leftLst, .rightLst {
position:absolute;
border-radius:50%;
top:calc(50% - 20px);
background: red;
width: 50px;
height: 50px;
}
.leftLst {
left:0;
}
.rightLst {
right:0;
}

.leftLst.over, .rightLst.over {
pointer-events: none;
background:#ccc;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container">
<div class="row">
<div class="MultiCarousel" data-items="1,2,3,4" data-slide="1" id="MultiCarousel" data-interval="1000">
<div class="MultiCarousel-inner">
<div class="item">
<div class="col-xl-3 pad15">
<img src="images/cert4.png" class="d-block w-100" alt="...">
<div class="onhover"><span>Certifikata per Atesimin e Kaldajes Elektrike</span></div>
</div>
</div>
<div class="item">
<div class="col-xl-3 pad15">
<img src="images/cert2.png" class="d-block w-100" alt="...">
<div class="onhover"><span>Certifikata per Atesimin e Kaldajes Elektrike</span></div>
</div>
</div>
<div class="item">
<div class="col-xl-3 pad15">
<img src="images/cert3.png" class="d-block w-100" alt="...">
<div class="onhover"><span>Certifikata per Atesimin e Kaldajes Elektrike</span></div>
</div>
</div>
<div class="item">
<div class="col-xl-3 pad15">
<img src="images/cert7.png" class="d-block w-100" alt="...">
<div class="onhover"><span>Certifikata per Atesimin e Kaldajes Elektrike</span></div>
</div>
</div>
<div class="item">
<div class="col-xl-3 pad15">
<img src="images/cert4.png" class="d-block w-100" alt="...">
<div class="onhover"><span>Certifikata per Atesimin e Kaldajes Elektrike</span></div>
</div>
</div>
<div class="item">
<div class="col-xl-3 pad15">
<img src="images/cert2.png" class="d-block w-100" alt="...">
<div class="onhover"><span>Certifikata per Atesimin e Kaldajes Elektrike</span></div>
</div>
</div>
<div class="item">
<div class="col-xl-3 pad15">
<img src="images/cert3.png" class="d-block w-100" alt="...">
<div class="onhover"><span>Certifikata per Atesimin e Kaldajes Elektrike</span></div>
</div>
</div>
<div class="item">
<div class="col-xl-3 pad15">
<img src="images/cert7.png" class="d-block w-100" alt="...">
<div class="onhover"><span>Certifikata per Atesimin e Kaldajes Elektrike</span></div>
</div>
</div>
</div>
<a class=" leftLst"><img src="images/left_arrow.svg" alt=""></a>
<a class=" rightLst"><img src="images/right_arrow.svg" alt=""></a>
</div>

</div>

</div>

最佳答案

问题是 click()函数正在搜索箭头键按钮的父元素,它预计是 .MultiCarousel div 以使其余代码正常工作。因此,当您将箭头键放在 .MultiCarousel 之外时div 将找到不同的父级,并且您的 slider 将不起作用。
在这种情况下,您可以使用 jQuery siblings()查找.MultiCarousel的函数div,因为箭头键按钮现在是兄弟。ResCarousel() 也是如此。功能。它希望箭头键按钮是 .MultiCarousel 的子级。分区。所以你也可以使用siblings()这里。

$(document).ready(function () {
var itemsMainDiv = ('.MultiCarousel');
var itemsDiv = ('.MultiCarousel-inner');
var itemWidth = "";

$('.leftLst, .rightLst').click(function () {
var condition = $(this).hasClass("leftLst");
if (condition)
click(0, this);
else
click(1, this)
});

ResCarouselSize();

$(window).resize(function () {
ResCarouselSize();
});

//this function define the size of the items
function ResCarouselSize() {
var incno = 0;
var dataItems = ("data-items");
var itemClass = ('.item');
var id = 0;
var btnParentSb = '';
var itemsSplit = '';
var sampwidth = $(itemsMainDiv).width();
var bodyWidth = $('body').width();
$(itemsDiv).each(function () {
id = id + 1;
var itemNumbers = $(this).find(itemClass).length;
btnParentSb = $(this).parent().attr(dataItems);
itemsSplit = btnParentSb.split(',');
$(this).parent().attr("id", "MultiCarousel" + id);

if (bodyWidth >= 1200) {
incno = itemsSplit[3];
itemWidth = sampwidth / incno;
}
else if (bodyWidth >= 992) {
incno = itemsSplit[2];
itemWidth = sampwidth / incno;
}
else if (bodyWidth >= 768) {
incno = itemsSplit[1];
itemWidth = sampwidth / incno;
}
else {
incno = itemsSplit[0];
itemWidth = sampwidth / incno;
}
$(this).css({ 'transform': 'translateX(0px)', 'width': itemWidth * itemNumbers });
$(this).find(itemClass).each(function () {
$(this).outerWidth(itemWidth);
});

$(".leftLst").addClass("over");
$(".rightLst").removeClass("over");

});
}


//this function used to move the items
function ResCarousel(e, el, s) {
var leftBtn = ('.leftLst');
var rightBtn = ('.rightLst');
var translateXval = '';
var divStyle = $(el + ' ' + itemsDiv).css('transform');
var values = divStyle.match(/-?[\d\.]+/g);
var xds = Math.abs(values[4]);
if (e == 0) {
translateXval = parseInt(xds) - parseInt(itemWidth * s);
$(el).siblings(rightBtn).removeClass("over");

if (translateXval <= itemWidth / 2) {
translateXval = 0;
$(el).siblings(leftBtn).addClass("over");
}
}
else if (e == 1) {
var itemsCondition = $(el).find(itemsDiv).width() - $(el).width();
translateXval = parseInt(xds) + parseInt(itemWidth * s);
$(el).siblings(leftBtn).removeClass("over");

if (translateXval >= itemsCondition - itemWidth / 2) {
translateXval = itemsCondition;
$(el).siblings(rightBtn).addClass("over");
}
}
$(el + ' ' + itemsDiv).css('transform', 'translateX(' + -translateXval + 'px)');
}

//It is used to get some elements from btn
function click(ell, ee) {
var Parent = "#" + $(ee).siblings( '.MultiCarousel' ).attr("id");
var slide = $(Parent).attr("data-slide");
ResCarousel(ell, Parent, slide);
}
});
.MultiCarousel { 
float: left;
overflow: hidden;
padding: 15px;
width: 100%;
position:relative;
box-sizing: border-box;
}
.MultiCarousel .MultiCarousel-inner {
transition: 1s ease all;
float: left;
}
.MultiCarousel .MultiCarousel-inner .item {
float: left;
}
.MultiCarousel .MultiCarousel-inner .item > div {
text-align: center;
padding:10px;
margin:10px;
color:#666;
}
.leftLst, .rightLst {
position:absolute;
border-radius:50%;
top:calc(50% - 20px);
background: red;
width: 50px;
height: 50px;
}
.leftLst {
left:0;
}
.rightLst {
right:0;
}

.leftLst.over, .rightLst.over {
pointer-events: none;
background:#ccc;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container">
<div class="row">
<div class="MultiCarousel" data-items="1,2,3,4" data-slide="1" id="MultiCarousel" data-interval="1000">
<div class="MultiCarousel-inner">
<div class="item">
<div class="col-xl-3 pad15">
<img src="https://www.fillmurray.com/640/360" class="d-block w-100" alt="...">
<div class="onhover"><span>Certifikata per Atesimin e Kaldajes Elektrike</span></div>
</div>
</div>
<div class="item">
<div class="col-xl-3 pad15">
<img src="https://www.fillmurray.com/640/360" class="d-block w-100" alt="...">
<div class="onhover"><span>Certifikata per Atesimin e Kaldajes Elektrike</span></div>
</div>
</div>
<div class="item">
<div class="col-xl-3 pad15">
<img src="https://www.fillmurray.com/640/360" class="d-block w-100" alt="...">
<div class="onhover"><span>Certifikata per Atesimin e Kaldajes Elektrike</span></div>
</div>
</div>
<div class="item">
<div class="col-xl-3 pad15">
<img src="https://www.fillmurray.com/640/360" class="d-block w-100" alt="...">
<div class="onhover"><span>Certifikata per Atesimin e Kaldajes Elektrike</span></div>
</div>
</div>
<div class="item">
<div class="col-xl-3 pad15">
<img src="https://www.fillmurray.com/640/360" class="d-block w-100" alt="...">
<div class="onhover"><span>Certifikata per Atesimin e Kaldajes Elektrike</span></div>
</div>
</div>
<div class="item">
<div class="col-xl-3 pad15">
<img src="https://www.fillmurray.com/640/360" class="d-block w-100" alt="...">
<div class="onhover"><span>Certifikata per Atesimin e Kaldajes Elektrike</span></div>
</div>
</div>
<div class="item">
<div class="col-xl-3 pad15">
<img src="https://www.fillmurray.com/640/360" class="d-block w-100" alt="...">
<div class="onhover"><span>Certifikata per Atesimin e Kaldajes Elektrike</span></div>
</div>
</div>
<div class="item">
<div class="col-xl-3 pad15">
<img src="https://www.fillmurray.com/640/360" class="d-block w-100" alt="...">
<div class="onhover"><span>Certifikata per Atesimin e Kaldajes Elektrike</span></div>
</div>
</div>
</div>
</div>

<a class=" leftLst"><img src="images/left_arrow.svg" alt=""></a>
<a class=" rightLst"><img src="images/right_arrow.svg" alt=""></a>

</div>
</div>

替代解决方案
您的问题明确指出您想将箭头键按钮放在 .MultiCarousel 之外。 div 但我认为将它们保留在 div 中可能会更好,因为它可以作为整个轮播的包装器。
然后您可以使用 closest()查找 .MultiCarousel的函数 click() 中的 div函数和 find()查找 ResCarousel() 中的按钮的功能功能。
这样,按钮始终连接到正确的 slider ,例如,当您想要使用多个彼此相邻的 slider 时,如下例所示。

$(document).ready(function () {
var itemsMainDiv = ('.MultiCarousel');
var itemsDiv = ('.MultiCarousel-inner');
var itemWidth = "";

$('.leftLst, .rightLst').click(function () {
var condition = $(this).hasClass("leftLst");
if (condition)
click(0, this);
else
click(1, this)
});

ResCarouselSize();

$(window).resize(function () {
ResCarouselSize();
});

//this function define the size of the items
function ResCarouselSize() {
var incno = 0;
var dataItems = ("data-items");
var itemClass = ('.item');
var id = 0;
var btnParentSb = '';
var itemsSplit = '';
var sampwidth = $(itemsMainDiv).width();
var bodyWidth = $('body').width();
$(itemsDiv).each(function () {
id = id + 1;
var itemNumbers = $(this).find(itemClass).length;
btnParentSb = $(this).parent().attr(dataItems);
itemsSplit = btnParentSb.split(',');
$(this).parent().attr("id", "MultiCarousel" + id);

if (bodyWidth >= 1200) {
incno = itemsSplit[3];
itemWidth = sampwidth / incno;
}
else if (bodyWidth >= 992) {
incno = itemsSplit[2];
itemWidth = sampwidth / incno;
}
else if (bodyWidth >= 768) {
incno = itemsSplit[1];
itemWidth = sampwidth / incno;
}
else {
incno = itemsSplit[0];
itemWidth = sampwidth / incno;
}
$(this).css({ 'transform': 'translateX(0px)', 'width': itemWidth * itemNumbers });
$(this).find(itemClass).each(function () {
$(this).outerWidth(itemWidth);
});

$(".leftLst").addClass("over");
$(".rightLst").removeClass("over");

});
}


//this function used to move the items
function ResCarousel(e, el, s) {
var leftBtn = ('.leftLst');
var rightBtn = ('.rightLst');
var translateXval = '';
var divStyle = $(el + ' ' + itemsDiv).css('transform');
var values = divStyle.match(/-?[\d\.]+/g);
var xds = Math.abs(values[4]);
if (e == 0) {
translateXval = parseInt(xds) - parseInt(itemWidth * s);
$(el).find(rightBtn).removeClass("over");

if (translateXval <= itemWidth / 2) {
translateXval = 0;
$(el).find(leftBtn).addClass("over");
}
}
else if (e == 1) {
var itemsCondition = $(el).find(itemsDiv).width() - $(el).width();
translateXval = parseInt(xds) + parseInt(itemWidth * s);
$(el).find(leftBtn).removeClass("over");

if (translateXval >= itemsCondition - itemWidth / 2) {
translateXval = itemsCondition;
$(el).find(rightBtn).addClass("over");
}
}
$(el + ' ' + itemsDiv).css('transform', 'translateX(' + -translateXval + 'px)');
}

//It is used to get some elements from btn
function click(ell, ee) {
var Parent = "#" + $(ee).closest( '.MultiCarousel' ).attr("id");
var slide = $(Parent).attr("data-slide");
ResCarousel(ell, Parent, slide);
}
});
.MultiCarousel { 
float: left;
overflow: hidden;
margin: 5%;
width: 40%;
position:relative;
box-sizing: border-box;
}
.MultiCarousel .MultiCarousel-inner {
transition: 1s ease all;
float: left;
}
.MultiCarousel .MultiCarousel-inner .item {
float: left;
}
.MultiCarousel .MultiCarousel-inner .item > div {
text-align: center;
padding:10px;
margin:10px;
color:#666;
}
.leftLst, .rightLst {
/*position:absolute;*/
border-radius:50%;
/*top:calc(50% - 20px);*/
background: red;
width: 50px;
height: 50px;
display: block;
}
.leftLst {
float: left;
}
.rightLst {
float: right;
}

.leftLst.over, .rightLst.over {
pointer-events: none;
background:#ccc;
}

.buttons {
width: 100%;
clear:both;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container">
<div class="row">

<div class="MultiCarousel" data-items="1,2,3,4" data-slide="1" id="MultiCarousel" data-interval="1000">
<div class="MultiCarousel-inner">
<div class="item">
<div class="col-xl-3 pad15">
<img src="https://www.fillmurray.com/200/200" class="d-block w-100" alt="...">
<div class="onhover"><span>Certifikata per Atesimin e Kaldajes Elektrike</span></div>
</div>
</div>
<div class="item">
<div class="col-xl-3 pad15">
<img src="https://www.fillmurray.com/200/200" class="d-block w-100" alt="...">
<div class="onhover"><span>Certifikata per Atesimin e Kaldajes Elektrike</span></div>
</div>
</div>
<div class="item">
<div class="col-xl-3 pad15">
<img src="https://www.fillmurray.com/200/200" class="d-block w-100" alt="...">
<div class="onhover"><span>Certifikata per Atesimin e Kaldajes Elektrike</span></div>
</div>
</div>
<div class="item">
<div class="col-xl-3 pad15">
<img src="https://www.fillmurray.com/200/200" class="d-block w-100" alt="...">
<div class="onhover"><span>Certifikata per Atesimin e Kaldajes Elektrike</span></div>
</div>
</div>
<div class="item">
<div class="col-xl-3 pad15">
<img src="https://www.fillmurray.com/200/200" class="d-block w-100" alt="...">
<div class="onhover"><span>Certifikata per Atesimin e Kaldajes Elektrike</span></div>
</div>
</div>
<div class="item">
<div class="col-xl-3 pad15">
<img src="https://www.fillmurray.com/200/200" class="d-block w-100" alt="...">
<div class="onhover"><span>Certifikata per Atesimin e Kaldajes Elektrike</span></div>
</div>
</div>
<div class="item">
<div class="col-xl-3 pad15">
<img src="https://www.fillmurray.com/200/200" class="d-block w-100" alt="...">
<div class="onhover"><span>Certifikata per Atesimin e Kaldajes Elektrike</span></div>
</div>
</div>
<div class="item">
<div class="col-xl-3 pad15">
<img src="https://www.fillmurray.com/200/200" class="d-block w-100" alt="...">
<div class="onhover"><span>Certifikata per Atesimin e Kaldajes Elektrike</span></div>
</div>
</div>
</div>
<div class="buttons">
<a class=" leftLst"><img src="images/left_arrow.svg" alt=""></a>
<a class=" rightLst"><img src="images/right_arrow.svg" alt=""></a>
</div>
</div>

<div class="MultiCarousel" data-items="1,2,3,4" data-slide="1" id="MultiCarousel" data-interval="1000">
<div class="MultiCarousel-inner">
<div class="item">
<div class="col-xl-3 pad15">
<img src="https://www.fillmurray.com/200/200" class="d-block w-100" alt="...">
<div class="onhover"><span>Certifikata per Atesimin e Kaldajes Elektrike</span></div>
</div>
</div>
<div class="item">
<div class="col-xl-3 pad15">
<img src="https://www.fillmurray.com/200/200" class="d-block w-100" alt="...">
<div class="onhover"><span>Certifikata per Atesimin e Kaldajes Elektrike</span></div>
</div>
</div>
<div class="item">
<div class="col-xl-3 pad15">
<img src="https://www.fillmurray.com/200/200" class="d-block w-100" alt="...">
<div class="onhover"><span>Certifikata per Atesimin e Kaldajes Elektrike</span></div>
</div>
</div>
<div class="item">
<div class="col-xl-3 pad15">
<img src="https://www.fillmurray.com/200/200" class="d-block w-100" alt="...">
<div class="onhover"><span>Certifikata per Atesimin e Kaldajes Elektrike</span></div>
</div>
</div>
<div class="item">
<div class="col-xl-3 pad15">
<img src="https://www.fillmurray.com/200/200" class="d-block w-100" alt="...">
<div class="onhover"><span>Certifikata per Atesimin e Kaldajes Elektrike</span></div>
</div>
</div>
<div class="item">
<div class="col-xl-3 pad15">
<img src="https://www.fillmurray.com/200/200" class="d-block w-100" alt="...">
<div class="onhover"><span>Certifikata per Atesimin e Kaldajes Elektrike</span></div>
</div>
</div>
<div class="item">
<div class="col-xl-3 pad15">
<img src="https://www.fillmurray.com/200/200" class="d-block w-100" alt="...">
<div class="onhover"><span>Certifikata per Atesimin e Kaldajes Elektrike</span></div>
</div>
</div>
<div class="item">
<div class="col-xl-3 pad15">
<img src="https://www.fillmurray.com/200/200" class="d-block w-100" alt="...">
<div class="onhover"><span>Certifikata per Atesimin e Kaldajes Elektrike</span></div>
</div>
</div>
</div>
<div class="buttons">
<a class=" leftLst"><img src="images/left_arrow.svg" alt=""></a>
<a class=" rightLst"><img src="images/right_arrow.svg" alt=""></a>
</div>
</div>

</div>
</div>

关于javascript - 当我将它们放在旋转木马外面时,按钮不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64118656/

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