简体   繁体   English

jQuery-为图像滚动设置“活动”类

[英]JQuery - setting an “active” class for image-scroll

I have content loading dynamically (using new WP_query loop in WordPress) for a jQuery carousel or image-scroll function -- where the image-scroll is a li list of images, styled to look like a strip of images. 我有一个用于jQuery轮播或图像滚动功能的动态加载内容(在WordPress中使用新的WP_query循环)-其中图像滚动是图像的li列表,其样式看起来像一条图像。

When the image-scroll works properly, one of the images in the li tag has a class of active, which expands the image and makes it look like it's in front of the other images, 当图像滚动正常工作时,li标签中的其中一幅图像具有活动类别,该类别可以扩展该图像并使它看起来像在其他图像之前,

... and as the user clicks through this strip of images, the active class changes/moves to the targeted li tag, expanding that image. ...当用户单击该图像带时,活动类将更改/移动到目标li标签,从而扩展该图像。

What's happening is that none of the li tags are active when the page loads - since the content is dynamic through the WP loop (I didn't want all of the li tags to start with the active class, so I didn't add it to the loop), 发生的是,页面加载时没有li标签处于活动状态-因为内容是通过WP循环动态的(我不希望所有li标签都以活动类开头,所以我没有添加它循环),

...and so the images are just lined up in a consistent strip w/o one of the images being expanded (or having that active class). ...因此图像只是在一致的条带中排列,而没有正在扩展的图像之一(或具有该活动类的图像)。

It is only if the user happens to click on one of the images that it expands, 只有当用户碰巧点击它展开的图像之一时,

...but i need one of the images to be expanded (to have the class of active) before the user clicks so I need the active class added as/after the page loads. ...但是我需要在用户单击之前扩展其中一幅图像(以具有活动类),因此我需要在页面加载时/之后添加活动类。

I have tried through the jQuery code to target one of the li tags to add the active class, using filter() or closest() after the page loads, but that didn't work. 我已经尝试过通过jQuery代码来定位li标签之一来添加活动类,并在页面加载后使用filter()closest() ,但这是行不通的。

Or maybe I should write a new script to add the active class? 还是我应该编写一个新脚本来添加活动类?

Any help much appreciated! 任何帮助,不胜感激!

I have the abbreviated html and the jQuery function below. 我下面有缩写的html和jQuery函数。

_Cindy _辛迪

ps as the code indicates, I also have corresponding article titles that scroll with the images, so perhaps I need to adjust the jQuery there, too. ps如代码所示,我还具有与图像一起滚动的相应文章标题,因此也许我也需要在其中调整jQuery。

    <div class="articles-scroll">

<ul class="images-scroll">
    <li><!-- I need only one of these tags to have a class of active to start -->

        <a href="#">
            <span class="set-image-border" style="float: none;">
               <img class="setborder" src="image-set-by-new-wp_query">
            </span>
        </a>
    </li>
    <li>
        <a href="#">
            <span class="set-image-border">
               <img class="setborder" src="image-set-by-new-wp_query">
            </span>
        </a>
    </li>
</ul>

<div class="clear-float"></div>

<!-- in this section of html one of the article titles is active to coordinate with the active li image above to produce a corresponding clickable title, this works, but once again, only when user clicks on an image to begin the jQuery image-scroll function -->

        <div class="wrapper">

<ul class="images-content">
    <li>
            <div class="article-header">
                <h2>
                    <a href="link-set-by-new-wp_query">
                </h2>
            </div>
        </div>
    </li>
    <li>
        <div class="wrapper">
            <div class="article-header">
                <h2>
                    <a href="link-set-by-new-wp_query">
                </h2>
            </div>
        </div>
    </li>
</ul>

</div>


    jQuery(".images-scroll li a").click(function() {
    jQuery(this).parent().parent().find("li").removeClass("active");
// tried the following instruction as well as on next line, but no go
// jQuery(this).parent().parent().closest("li").addClass("active");
    jQuery(this).parent().addClass("active");
    jQuery(this).parent().parent().parent().find(".images-content > li").removeClass("active");
    jQuery(this).parent().parent().parent().find(".images-content > li").eq(jQuery(this).parent().index()).addClass("active");
    var step = ((-250)*(jQuery(this).parent().index()-1))-60;
    //alert(step);
    jQuery(this).parent().parent().css("margin-left", step+"px");
    return false;
});

The reason why the code you wrote didn't work is that you have it inside a click handler, so nothing happens until you click one of the targeted elements. 您编写的代码无法正常工作的原因是,您将其保存在单击处理程序中,因此,除非您单击目标元素之一,否则什么都不会发生。 If you want something to happen on page load you can use $(document).ready() (can be shortened as $() ) or $(window).load() . 如果您希望在页面加载时发生某些事情,可以使用$(document).ready() (可以缩写为$() )或$(window).load() Just add the following lines below or above your existing code. 只需在现有代码下方或上方添加以下几行。

jQuery(function(){
    var $listItems = jQuery('.images-scroll li');
    $listItems.first().addClass('active');
    // Second list item
    $listItems.eq(1).addClass('active');
    // Third list item
    $listItems.eq(2).addClass('active');
});

Also, please note that (unless it conflicts with a different plugin), writing $ is shorter than jQuery , and it should do the same. 另外,请注意(除非与其他插件冲突),编写$jQuery短,并且应该这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM