WordPress中我的Flexlider的活动类

时间:2015-03-19 作者:ccbar

我用wordpress循环调用图像以显示在主页上的flexslider中。

然而,滑块缩略图的活动类并没有与幻灯片同步工作——我无法单击缩略图来查看相应的较大幻灯片图像。相反,当我单击缩略图时,滑块中的所有图像都会消失。

我相信这与在缩略图上设置活动类有关,而且由于它不是硬编码的,所以我正在尝试修改flexslider jquery以将活动类添加到缩略图中

test site

jQuery(document).ready(function() {

// Default FlexSlider parameters
if (typeof fs_params == \'undefined\') {
    fs_params = {animation: \'fade\', controlNav: true, directionNav: true, slideshow: false, pauseOnAction: true, pauseOnHover: false, animationSpeed: 600, slideshowSpeed: 7000};
}

// Homepage FlexSlider parameters
if (typeof fs_params_homepage == \'undefined\') {
    fs_params_homepage = {animation: \'fade\', controlNav: true, directionNav: true, slideshow: true, pauseOnAction: false, pauseOnHover: true, animationSpeed: 600, slideshowSpeed: 7000};
}

// Note: fs_params / fs_params_homepage can be overwritten by setting its value in an html file


// *** FLEXSLIDER INITIALIZATION
$(\'.flexslider\').each(function() {
    var $this = $(this);

    if ($this.is(\'#home-slider\')) {

        // Callback function: fires asynchronously with each slider animation
        fs_params_homepage.before = function(slider){
            var next_index = slider.animatingTo + 1,
                current_index = slider.currentSlide + 1,
                $next_caption = $(\'.slider-caption div[id=caption\'+next_index+\']\'),
                $current_caption = $(\'.slider-caption div[id=caption\'+current_index+\']\');

            $(\'.slider-menu li\').removeClass(\'active\').siblings().find(\'a\')
                    .filter(function () {
                        return $(this).attr(\'href\') == \'#slide\'+next_index;
                    }).parent().addClass(\'active\');

            if ($(\'html\').hasClass(\'ie8\')) { // IE8 hack
                $current_caption.hide();
            }
            $current_caption.fadeOut(400, \'swing\', function() {
                if ($(\'html\').hasClass(\'ie8\')) { // IE8 hack
                    $next_caption.show();
                } else {
                    $next_caption.fadeIn(300, \'swing\');
                }
                setCaptionHeight($next_caption);
            });
        };

        // Callback function: fires after each slider animation completes
        fs_params_homepage.after = function(slider){

        };

        // Init homepage slider:

        // full-width slider with center-aligned image
        if ($this.parent().parent().parent().parent().hasClass(\'full-width-image\')) {
            if ($this.attr(\'data-slideshow\') == \'yes\') { fs_params_homepage.slideshow = true; }
            if ($this.attr(\'data-slideshow\') == \'no\') { fs_params_homepage.slideshow = false; }
            if ($this.attr(\'data-animation\') == \'fade\') { fs_params_homepage.animation = \'fade\'; }
            if ($this.attr(\'data-animation\') == \'slide\') { fs_params_homepage.animation = \'slide\'; }
            if ($this.attr(\'data-control-nav\') == \'yes\') { fs_params_homepage.controlNav = true; }
            if ($this.attr(\'data-control-nav\') == \'no\') { fs_params_homepage.controlNav = false; }
            if ($this.attr(\'data-direction-nav\') == \'yes\') { fs_params_homepage.directionNav = true; }
            if ($this.attr(\'data-direction-nav\') == \'no\') { fs_params_homepage.directionNav = false; }
            if ($this.attr(\'data-pause-on-action\') == \'yes\') { fs_params_homepage.pauseOnAction = true; }
            if ($this.attr(\'data-pause-on-action\') == \'no\') { fs_params_homepage.pauseOnAction = false; }
        }

        // slider with right-aligned image
        else {
            fs_params_homepage.animation = \'fade\';
        }

        if ($this.attr(\'data-slideshow-speed\') > 0) { fs_params_homepage.slideshowSpeed = $this.attr(\'data-slideshow-speed\'); }
        $this.flexslider(fs_params_homepage);
    }

    else {
        // Init default slider
        var params = $.extend({}, fs_params);
        if ($this.attr(\'data-slideshow\') == \'yes\') { params.slideshow = true; }
        if ($this.attr(\'data-slideshow\') == \'no\') { params.slideshow = false; }
        if ($this.attr(\'data-slideshow-speed\') > 0) { params.slideshowSpeed = $this.attr(\'data-slideshow-speed\'); }
        if ($this.attr(\'data-animation\') == \'fade\') { params.animation = \'fade\'; }
        if ($this.attr(\'data-animation\') == \'slide\') { params.animation = \'slide\'; }
        if ($this.attr(\'data-control-nav\') == \'yes\') { params.controlNav = true; }
        if ($this.attr(\'data-control-nav\') == \'no\') { params.controlNav = false; }
        if ($this.attr(\'data-direction-nav\') == \'yes\') { params.directionNav = true; }
        if ($this.attr(\'data-direction-nav\') == \'no\') { params.directionNav = false; }
        if ($this.attr(\'data-pause-on-action\') == \'yes\') { params.pauseOnAction = true; }
        if ($this.attr(\'data-pause-on-action\') == \'no\') { params.pauseOnAction = false; }
        $this.flexslider(params);
    }

});


// *** HOMEPAGE SLIDER
$homeSlider = $(\'#home-slider\').data(\'flexslider\');

// Slider menu
$(\'.slider-menu li\').on(\'click\', function(e) {
    e.preventDefault();
    var slide_index = $(\'a\', this).attr(\'href\').substr(6) - 1;
    $homeSlider.flexslider(slide_index);
    return false; // IE9 hack
});

// Position the right side navigation ("right-side-nav" class)
function positionRightSideNav() {
    if ($(window).width() >= 768) {
        var homeHero_x = 0.5 * ($(window).width() - $(\'#home-hero\').width()),
            heroImage_marginLeft = parseInt($(\'#home-hero .hero-image\').css(\'margin-left\')),
            col_marginLeft = parseInt($(\'#home-hero-content .columns\').css(\'margin-left\')),
            rightSideNav_width = $(\'#home-slider.right-side-nav .flex-direction-nav a\').width(),
            navLeft = $(window).width() - (homeHero_x + col_marginLeft + heroImage_marginLeft) - rightSideNav_width;
        $(\'#home-slider.right-side-nav .flex-direction-nav a\').css(\'left\', navLeft + \'px\');
    } else {
        $(\'#home-slider.right-side-nav .flex-direction-nav a\').css(\'left\', \'\');
    }
}

wp-query for large slideshow images:

<div class="hero-image">
                    <div id="home-slider" class="flexslider right-side-nav">
                        <ul class="slides">

<?php    

$attachments = get_children(array(\'post_parent\' => 107, \'post_type\' => \'attachment\', \'post_mime_type\' => \'image\', \'post_status\' => \'inherit\', \'posts_per_page\' => 7, \'order\' => \'DESC\' ));

if ($attachments) {

    foreach ( $attachments as $attachment_id => $attachment ) {

        echo \'<li id="slide\'.$attachment_id.\'" >\';
        echo wp_get_attachment_image($attachment_id, \'home-slider\');
        echo \'</li>\';
    }

} // end see if images

 wp_reset_postdata();

  ?>
                        </ul>
                    </div>
                </div>

and then wp-query for thumbs:

<?php    

$thumbs = get_children(array(\'post_parent\' => 107, \'post_type\' => \'attachment\', \'post_mime_type\' => \'image\', \'post_status\' => \'inherit\', \'posts_per_page\' => 7, \'order\' => \'DESC\' ));

if ($thumbs) { // if there are images attached to posting, start the flexslider markup

    foreach ( $thumbs as $attachment_id => $thumb ) { // create the list items for images with captions

        echo \'<li>\';
        echo \'<a href="#slide\'.$attachment_id.\'" >\';
        echo wp_get_attachment_image($attachment_id, \'home-slider-thumb\');
        echo \'</a>\';
        echo \'</li>\';
    }

} // end see if images

wp_reset_postdata();

?>

2 个回复
最合适的回答,由SO网友:Milo 整理而成

这里的代码就是问题所在-

$(\'.slider-menu li\').on(\'click\', function(e) {
    e.preventDefault();
    var slide_index = $(\'a\', this).attr(\'href\').substr(6) - 1;
    $homeSlider.flexslider(slide_index);
    return false; // IE9 hack
});
在html中,您将ID设置为slide 加上附件ID,然后告诉flexslider为该幻灯片设置动画。除了flexslider需要的是幻灯片位置(第1张幻灯片、第2张幻灯片等),而不是元素ID。您应该使用单击的li的索引-

$(\'.slider-menu li\').on(\'click\', function(e) {
    e.preventDefault();
    $homeSlider.flexslider( $(this).index() );
    return false; // IE9 hack
});
还请注意,如果为拇指创建旋转木马并设置旋转木马,flexslider可以自动完成所有这些操作asNavFor 对于滑块。

SO网友:Mayeenul Islam

.active flexslider的类不是特定于WordPress的,所以这里不谈主题。顺便说一句,一个简单的WP_Query(), get_posts(), get_children() 等等。不会影响flexslider的性能。您必须遵循flexslider提供的语法。请注意:

<div class="flexslider">
  <ul class="slides">
    //loop starts
       <li>
         <img src="slide1.jpg" />
       </li>
    //loop ends
  </ul>
</div>
Theul.slides 这很重要。

以及active 类不存在,因为JS没有正确触发。因此,要调试JS问题(如果有),可以使用浏览器的开发人员控制台。在我的许多项目中,一个简单的jQuery对我很有用:

$(window).load(function() {
  $(\'.flexslider\').flexslider({
    animation: "slide",
    controlNav: "thumbnails"
  });
});
请检查:

AnoConflict wrapper 是否有必要enqueuedflexslider.min.js

  • 您的自定义JS代码已正确排队并在加载DOM后加载,并且,flexslider.css 也在<head> - 有时这很重要HEY! .flex-active 是加载到缩略图中的类(<img>), NOT<li> (check here)

    wp_reset_postdata() 对于get_children() 查询:)

  • 结束

    相关推荐

    jQuery function not working

    我有一个非常简单的jQuery函数,用来切换div的可见性:jQuery( \".signUp\" ).click(function() { $(\'.signUpForm\').toggle(); }); HTML是一种简单的注册表单(为简洁起见,省略了隐藏字段):<p><a class=\"signUp\" href=\"#\">Sign Up!</a></p> <div class=\"signUpFor