jQuery循环遍历每个li并检查一个类

时间:2016-04-20 11:11:51

标签: javascript jquery html css

我正在尝试用jQuery做一些简单的事情但是很难。我想做以下

  1. 循环遍历每个li元素并检查它是否已应用特定类“timeline-inverted”

  2. 如果我正在寻找的课程存在于li上,那么我想再申请一些课程'可见动画fadeInRight'

  3. 如果该类不存在,那么我想应用另一个类'visible animated fadeInLeft'

  4. 这是我到目前为止所拥有的......

    HTML输出

    <ul class="timeline">
        <li class="hidden">
            <div class="timeline-image">
                <img class="img-circle img-responsive" src="assets/img/about/1.jpg" alt="">
            </div>
            <div class="timeline-panel">
                <div class="timeline-heading">
                    <h4>2009-2010</h4>
                    <h4 class="subheading">Our Humble Beginnings</h4>
                </div>
                <div class="timeline-body">
                    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Sunt ut voluptatum eius sapiente, totam reiciendis temporibus qui quibusdam, recusandae sit vero unde, sed, incidunt et ea quo dolore laudantium consectetur!</p>
                </div>
            </div>
        </li><!-- /timeline item -->
        <li class="timeline-inverted hidden">
            <div class="timeline-image">
                <img class="img-circle img-responsive" src="assets/img/about/1.jpg" alt="">
            </div>
            <div class="timeline-panel">
                <div class="timeline-heading">
                    <h4>March 2011</h4>
                    <h4 class="subheading">An Agency is Born</h4>
                </div>
                <div class="timeline-body">
                    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Sunt ut voluptatum eius sapiente, totam reiciendis temporibus qui quibusdam, recusandae sit vero unde, sed, incidunt et ea quo dolore laudantium consectetur!</p>
                </div>
            </div>
        </li><!-- /timeline item -->
        <li class="hidden">
            <div class="timeline-image">
                <img class="img-circle img-responsive" src="assets/img/about/1.jpg" alt="">
            </div>
            <div class="timeline-panel">
                <div class="timeline-heading">
                    <h4>July 2014</h4>
                    <h4 class="subheading">Phase Two Expansion</h4>
                </div>
                <div class="timeline-body">
                    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Sunt ut voluptatum eius sapiente, totam reiciendis temporibus qui quibusdam, recusandae sit vero unde, sed, incidunt et ea quo dolore laudantium consectetur!</p>
                </div>
            </div>
        </li><!-- /timeline item -->
        <li class="timeline-inverted hidden">
            <div class="timeline-image">
                <img class="img-circle img-responsive" src="assets/img/about/1.jpg" alt="">
            </div>
            <div class="timeline-panel">
                <div class="timeline-heading">
                    <h4>December 2014</h4>
                    <h4 class="subheading">Another great month</h4>
                </div>
                <div class="timeline-body">
                    <p>It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
                </div>
            </div>
        </li><!-- /timeline item -->
        <li class="timeline-inverted">
            <div class="timeline-image">
                <h4>
                    Be Part
                    <br>Of Our
                    <br>Story!
                </h4>
            </div>
        </li><!-- /.timeline -->
    </ul> 
    

    Jquery的

    $('.timeline li').each(function () {
        if ($(this).hasClass("timeline-inverted")) {
            $('.timeline li').removeClass("hidden").viewportChecker({
                classToAdd: 'visible animated fadeInRight', offset: 100
            });
    
        } else {
            $(this).removeClass("hidden").viewportChecker({
                classToAdd: 'visible animated fadeInLeft', offset: 100
            });
        }
    });
    

    如果li项具有时间轴反转类,我希望它从右侧飞入,如果它没有时间轴反转类,我希望它从左侧飞入!

    谢谢保罗

    *编辑 - 此代码适用于我:)

    $(function () {
        $('.timeline li').each(function () {
            if ($(this).hasClass("timeline-inverted")) {
                $(this).removeClass("hidden").viewportChecker({
                    classToAdd:'visible animated fadeInRight', offset: 100
                });
    
            } else {
                $(this).removeClass("hidden").viewportChecker({
                    classToAdd: 'visible animated fadeInLeft', offset: 100
                });
            }
        });
    });
    

2 个答案:

答案 0 :(得分:4)

您无需使用each()。你可以这样做。

$('.timeline li.timeline-inverted').removeClass("hidden").viewportChecker({
    classToAdd: 'visible animated fadeInRight',
    offset: 100
});

$('.timeline li').not('.timeline-inverted').removeClass("hidden").viewportChecker({
    classToAdd: 'visible animated fadeInLeft',
    offset: 100
});

答案 1 :(得分:1)

尝试这一点:将您的脚本放在$(function(){..});内并使用$(this)代替$('.timeline li')

$(function(){
  $('.timeline li').each(function () {
        if ($(this).hasClass("timeline-inverted")) {
            $(this).removeClass("hidden").addClass('visible animated fadeInRight').viewportChecker({
                offset: 100
            });

        } else {
            $(this).removeClass("hidden").addClass('visible animated fadeInLeft').viewportChecker({
                offset: 100
            });
        }
    });
});