Trigger event on Individual Component Without triggering others with Shared Class in jQuery

huangapple go评论54阅读模式
英文:

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:

    &lt;div class=&quot;slider&quot;&gt;
    &lt;div class=&quot;arrow-left&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;slide&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;slide on&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;slide&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;arrow-right&quot;&gt;&lt;/div&gt;
    &lt;/div&gt;

    &lt;div class=&quot;slider&quot;&gt;
        &lt;div class=&quot;arrow-left&quot;&gt;&lt;/div&gt;
    &lt;div class=&quot;slide&quot;&gt;&lt;/div&gt;
    &lt;div class=&quot;slide on&quot;&gt;&lt;/div&gt;
    &lt;div class=&quot;slide&quot;&gt;&lt;/div&gt;
    &lt;div class=&quot;arrow-right&quot;&gt;&lt;/div&gt;
        &lt;/div&gt;

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(&#39;../images/arrowleft.svg&#39;);
    width: 50px;
    cursor: pointer;
    height: 50px;
    background-repeat: no-repeat;
    z-index: 5;
}

.arrow-right {
    background: url(&#39;../images/arrowright.svg&#39;);
    width: 50px;
    height: 50px;
    cursor: pointer;
    background-repeat: no-repeat;
    z-index: 5;
}

.on {
    height: 500px;
   min-width: 300px;
    opacity: 1;
}

Javascript

    $(&#39;.slider&#39;).each(function() {
    $(this).find(&#39;.arrow-left&#39;).on(&quot;click&quot;, function() {
        $(&#39;.slide&#39;).animate({
            left: &quot;-=33%&quot;
        })
        $(&#39;.on&#39;).next().addClass(&#39;on&#39;)
        $(&#39;.on&#39;).prev().removeClass(&#39;on&#39;)
    })

    $(this).find(&#39;.arrow-right&#39;).on(&quot;click&quot;, function() {
        $(&#39;.slide&#39;).animate({
            left: &quot;+=33%&quot;
        })
        $(&#39;.on&#39;).prev().addClass(&#39;on&#39;)
        $(&#39;.on&#39;).next().removeClass(&#39;on&#39;)
    })
})

答案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')
    })
})

未经测试,但应该可以工作。

注意:您还应该能够链接addClassremoveClass。因为一旦添加了类,接下来的代码行现在选择了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

$(&#39;.slider&#39;).each(function() {
    var _slider = $(this);
    
    _slider.find(&#39;.arrow-left&#39;).on(&quot;click&quot;, function() {
        _slider.find(&#39;.slide&#39;).animate({
            left: &quot;-=33%&quot;
        })
        _slider.find(&#39;.on&#39;).next().addClass(&#39;on&#39;)
        _slider.find(&#39;.on&#39;).prev().removeClass(&#39;on&#39;)
    })

    _slider.find(&#39;.arrow-right&#39;).on(&quot;click&quot;, function() {
        _slider.find(&#39;.slide&#39;).animate({
            left: &quot;+=33%&quot;
        })
        _slider.find(&#39;.on&#39;).prev().addClass(&#39;on&#39;)
        _slider.find(&#39;.on&#39;).next().removeClass(&#39;on&#39;)
    })
})

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(&#39;.on&#39;).removeClass(&#39;on&#39;).next().addClass(&#39;on&#39;)

huangapple
  • 本文由 发表于 2023年5月24日 19:38:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76323114.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定