英文:
Trigger event on Individual Component Without triggering others with Shared Class in jQuery
问题
我有两个滑块。
这些滑块由箭头操作。
滑块和箭头具有相同的类。
当按下滑块A上的箭头之一时,滑块A和滑块B都会移动。
是否可以点击滑块A上的箭头,并且只有滑块A移动,而不改变类或使用id。
以下是滑块的代码:
HTML:
<div class="slider">
<div class="arrow-left"></div>
<div class="slide"></div>
<div class="slide on"></div>
<div class="slide"></div>
<div class="arrow-right"></div>
</div>
<div class="slider">
<div class="arrow-left"></div>
<div class="slide"></div>
<div class="slide on"></div>
<div class="slide"></div>
<div class="arrow-right"></div>
</div>
CSS:
.slider {
display: flex;
justify-content: space-between;
margin-top: 100px;
padding-right: 10px;
padding-left: 10px;
width: 90%;
margin-left: auto;
margin-right: auto;
align-items: center;
}
.slide {
background-color: red;
width: 150px;
height: 50px;
margin-left: 50px;
margin-right: 50px;
position: relative;
opacity: 0.5;
}
.arrow-left {
background: url('../images/arrowleft.svg');
width: 50px;
cursor: pointer;
height: 50px;
background-repeat: no-repeat;
z-index: 5;
}
.arrow-right {
background: url('../images/arrowright.svg');
width: 50px;
height: 50px;
cursor: pointer;
background-repeat: no-repeat;
z-index: 5;
}
.on {
height: 500px;
min-width: 300px;
opacity: 1;
}
JavaScript:
$('.slider').each(function() {
$(this).find('.arrow-left').on("click", function() {
$('.slide').animate({
left: "-=33%"
});
$('.on').next().addClass('on');
$('.on').prev().removeClass('on');
});
$(this).find('.arrow-right').on("click", function() {
$('.slide').animate({
left: "+=33%"
});
$('.on').prev().addClass('on');
$('.on').next().removeClass('on');
});
});
英文:
I have two sliders.
The sliders are operated by arrows.
The sliders and arrows have the same class.
At the moment when one of the arrows on Slider A is pressed, BOTH slider A and slider B move.
Is it possible to click the arrow on slider A and only have slider A move WITHOUT changes classes or using ids.
Here is code for the sliders:
HTML:
<div class="slider">
<div class="arrow-left"></div>
<div class="slide"></div>
<div class="slide on"></div>
<div class="slide"></div>
<div class="arrow-right"></div>
</div>
<div class="slider">
<div class="arrow-left"></div>
<div class="slide"></div>
<div class="slide on"></div>
<div class="slide"></div>
<div class="arrow-right"></div>
</div>
CSS:
.slider {
display: flex;
justify-content: space-between;
margin-top: 100px;
padding-right: 10px;
padding-left: 10px;
width: 90%;
margin-left: auto;
margin-right: auto;
align-items: center;
}
.slide {
background-color: red;
width: 150px;
height: 50px;
margin-left: 50px;
margin-right: 50px;
position: relative;
opacity: 0.5;
}
.arrow-left {
background: url('../images/arrowleft.svg');
width: 50px;
cursor: pointer;
height: 50px;
background-repeat: no-repeat;
z-index: 5;
}
.arrow-right {
background: url('../images/arrowright.svg');
width: 50px;
height: 50px;
cursor: pointer;
background-repeat: no-repeat;
z-index: 5;
}
.on {
height: 500px;
min-width: 300px;
opacity: 1;
}
Javascript
$('.slider').each(function() {
$(this).find('.arrow-left').on("click", function() {
$('.slide').animate({
left: "-=33%"
})
$('.on').next().addClass('on')
$('.on').prev().removeClass('on')
})
$(this).find('.arrow-right').on("click", function() {
$('.slide').animate({
left: "+=33%"
})
$('.on').prev().addClass('on')
$('.on').next().removeClass('on')
})
})
答案1
得分: 1
以下是您要翻译的内容:
你可以这样做,将您正在操作的滑块的jQuery元素保存为函数局部变量,以限制使用find
选择类。
$('.slider').each(function() {
var _slider = $(this);
_slider.find('.arrow-left').on("click", function() {
_slider.find('.slide').animate({
left: "-=33%"
})
_slider.find('.on').next().addClass('on')
_slider.find('.on').prev().removeClass('on')
})
_slider.find('.arrow-right').on("click", function() {
_slider.find('.slide').animate({
left: "+=33%"
})
_slider.find('.on').prev().addClass('on')
_slider.find('.on').next().removeClass('on')
})
})
未经测试,但应该可以工作。
注意:您还应该能够链接addClass
和removeClass
。因为一旦添加了类,接下来的代码行现在选择了2个元素(其中一个是您刚刚添加了类的元素),这可能导致意外情况。链接调用应该解决这个问题。 (编辑:更简单的版本)
_slider.find('.on').removeClass('on').next().addClass('on')
英文:
You can do like this, saving the jQuery element of the slider you are working on as a function local variable to restrict the class selections with find
$('.slider').each(function() {
var _slider = $(this);
_slider.find('.arrow-left').on("click", function() {
_slider.find('.slide').animate({
left: "-=33%"
})
_slider.find('.on').next().addClass('on')
_slider.find('.on').prev().removeClass('on')
})
_slider.find('.arrow-right').on("click", function() {
_slider.find('.slide').animate({
left: "+=33%"
})
_slider.find('.on').prev().addClass('on')
_slider.find('.on').next().removeClass('on')
})
})
Not tested but it should work
NOTE: you should also be able to chain the addClass
and removeClass
too. Because once you added the class, the following line of code now selects 2 elements (with the one you just added the class on) and can lead to unexpected things. Chaining the calls should solve this. (EDIT: simpler version)
_slider.find('.on').removeClass('on').next().addClass('on')
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论