php - 将类应用于 :first-child once only when creating a list dynamically with PHP?

标签 php javascript jquery

好的,我正在使用 PHP 按目录加载图像列表:

<?php
    # fetch image details
    $images = getImages("marc/img/livingrooms/");

    # display on page
    foreach($images as $img) {
        echo "<li><img src=\"{$img['file']}\" title=\"\" alt=\"\"></li>\n";
    }
?>

然后我使用 Galleria JQuery 插件将这些项目设置为画廊的样式:

$(document).ready(function(){

    $('.dynolist').addClass('gallery_group'); // adds new class name to maintain degradability

    $('ul.gallery_group').galleria({
        history   : true, // activates the history object for bookmarking, back-button etc.
        clickNext : true, // helper for making the image clickable
        insert    : '#main_image', // the containing selector for our main image
        onImage   : function(image,caption,thumb) { // let's add some image effects for demonstration purposes

            // fade in the image & caption
            image.css('display','none').fadeIn(1000);
            caption.css('display','none').fadeIn(1000);

            // fetch the thumbnail container
            var _li = thumb.parents('li');

            // fade out inactive thumbnail
            _li.siblings().children('img.selected').fadeTo(500,0.3);

            // fade in active thumbnail
            thumb.fadeTo('fast',1).addClass('selected');

            // add a title for the clickable image
            image.attr('title','Next image >>');
        },
        onThumb : function(thumb) { // thumbnail effects goes here

            // fetch the thumbnail container
            var _li = thumb.parents('li');

            // if thumbnail is active, fade all the way.
            var _fadeTo = _li.is('.active') ? '1' : '0.3';

            // fade in the thumbnail when finnished loading
            thumb.css({display:'none',opacity:_fadeTo}).fadeIn(1500);

            // hover effects
            thumb.hover(
                function() { thumb.fadeTo('fast',1); },
                function() { _li.not('.active').children('img').fadeTo('fast',0.3); } // don't fade out if the parent is active
            )
        }
    });
});

问题是我需要为从该列表中提取的第一个项目提供“事件”类,因此第一个图像将加载到幻灯片中。除了加载第一个图像之外,其他一切现在都可以正常工作。

有什么建议吗?

最佳答案

$('selector:first').addClass('active');

也许?

或者怎么样

# display on page
$class = 'class="active" ';
foreach($images as $img) {
    echo "<li><img ".$class."src=\"{$img['file']}\" title=\"\" alt=\"\"></li>\n";
    $class="";
}

关于php - 将类应用于 :first-child once only when creating a list dynamically with PHP?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2313030/

相关文章:

php - 如何将 PHP 父对象转换/转换为子对象?

php - SQLSTATE[42S02] : Base table or view not found: 1146 Table

javascript - 在控制台中显示附加值

javascript - select2.js 获取事件目标

javascript - 如果我的脚本位于正文末尾,我应该使用 DOM 就绪函数吗?

php - 可以从mysql数据库读取数据但无法将数据写入数据库在Ubuntu 16.04上使用PHP 5.6?

php - 在 SilverStripe ModelAdmin 中搜索关系记录

javascript - 在带有 ejs 的 Node.js 中使用 AJAX

javascript - 控制台显示 Uncaught TypeError : object is not a function, 一切正常

jquery - 如何向 Colorbox 模态窗口添加填充?