当前位置: 代码迷 >> JavaScript >> 单击过滤器按钮时,同位素排水沟会塌陷
  详细解决方案

单击过滤器按钮时,同位素排水沟会塌陷

热度:64   发布时间:2023-06-13 11:45:02.0

我有一个使用WordPress的同位素过滤器来过滤帖子。 它使用砌体布局,其排水沟大小设置为CSS中的百分比。 直到我单击过滤器上的某些内容,然后在重新加载页面或调整浏览器窗口的大小之前,装订线间隙都消失了并且无法再次使用时,所有这些在页面加载时都非常有效。

任何帮助将不胜感激,我完全迷路了!

这是我初始化代码的方式:

$( function() {
  $('.container').isotope({
  itemSelector: '.item',
  percentPosition: true,
  layoutMode: 'masonry',
  masonry: {
    columnWidth: '.grid-sizer',
    gutter: '.gutter-sizer'
   }
  })
});

编辑:

这是CSS

.gutter-sizer {
width: 1.7241379%;
}


.item, .grid-sizer {
    width: 100%; // width of each brick less the padding inbetween
    margin-bottom: 20px;
    @include border-radius(2px);
    @include shadow;
    background-color: $white;
        @media only screen and (min-width: 600px) {
    width: 49.13793105%;
        }
    @media only screen and (min-width: 900px) {
        width: 23.7068966%;
    }
}

这是过滤器和砌体桩的代码:

    <!-- #filter-menu -->   <nav id="filter-navigation" class="main-filter-navigation item" role="navigation">

        <button class="filter-toggle"><?php _e( 'Filter', 'cambridgerules' ); ?><i class="fa fa-filter"></i></button>

        <ul class="filters">
            <li class="filter-all"><a href="#" data-filter="*" class="selected">All</a></li>
            <li class="interpret-filter-label">Interpreted Worldwide:</li>
            <?php
            $terms = get_terms("type"); // get all categories, but you can use any taxonomy
            $count = count($terms); //How many are they?
            if ( $count > 0 ){  //If there are more than 0 terms
                foreach ( $terms as $term ) {  //for each term:
                    echo "<li class='".$term->slug."'><a href='#' data-filter='.".$term->slug."'>" . $term->name . "</a></li>\n";
                    //create a list item with the current term slug for sorting, and name for label
                }
            }
            ?>
        </ul>
    </nav><!-- #filter-menu -->


    <div class="container">
        <div class="grid-sizer"></div>
        <div class="gutter-sizer"></div>

        <?php $the_query = new WP_Query( array(
            'showposts' => 50,
            'post_type' => array('interpreted')
            )
            ); //Check the WP_Query docs to see how you can limit which posts to display ?>
            <?php if ( $the_query->have_posts() ) : ?>
                <div id="isotope-list">


                <?php while ( $the_query->have_posts() ) : $the_query->the_post();
                    $termsArray = get_the_terms( $post->ID, "type" );  //Get the terms for this particular item
                    $termsString = ""; //initialize the string that will contain the terms
                    foreach ( $termsArray as $term ) { // for each term
                    $termsString .= $term->slug.' '; //create a string that has all the slugs
                    }
                ?>

                <div class="<?php echo $termsString; ?> item"> <?php // 'item' is used as an identifier (see Setp 5, line 6) ?>
                    <h3><?php the_title(); ?></h3>
                    <?php if ( has_post_thumbnail() ) {
                        the_post_thumbnail();
                    } ?>
                </div> <!-- end item -->
<?php endwhile;  ?>
</div> <!-- end isotope-list -->
<?php endif; ?>

</div> <!-- end container -->

好的,使用以下代码对其进行排序:

jQuery(function ($) {

var $container = $('.container'); //The ID for the list with all the blog posts
$container.isotope({ //Isotope options, 'item' matches the class in the PHP
    itemSelector : '.item',
    layoutMode : 'masonry',
    masonry: {
  columnWidth: '.grid-sizer',
  gutter: '.gutter-sizer'
}
});

//Add the class selected to the item that is clicked, and remove from the others
var $optionSets = $('.filters'),
$optionLinks = $optionSets.find('a');

$optionLinks.click(function(){
var $this = $(this);
// don't proceed if already selected
if ( $this.hasClass('selected') ) {
  return false;
}
var $optionSets = $this.parents('.filters');
$optionSets.find('.selected').removeClass('selected');
$this.addClass('selected');

//When an item is clicked, sort the items.
 var selector = $(this).attr('data-filter');
$container.isotope({ filter: selector });

return false;
});

});
  相关解决方案