简体   繁体   English

使用 jQuery .slideToggle() 和 display: grid;

[英]Using jQuery .slideToggle() with display: grid;

I have a set of collapsible/expandable elements, and I would like to have the inner content laid out in a display:grid format.我有一组可折叠/可展开的元素,我希望以display:grid格式布置内部内容。

In my current setup, I use a hook in the $().slideToggle() method to call an anonymous function that checks if the element is visible and forces the display to grid.在我当前的设置中,我在$().slideToggle()方法中使用一个钩子来调用一个匿名函数,该函数检查元素是否可见并强制显示为网格。

This isn't a good solution as it stands, because the content is not in grid format during the expansion animation the first time the content is shown.这不是一个好的解决方案,因为在第一次显示内容的扩展动画期间,内容不是网格格式。 It only applies the correct format after the animation is complete.它仅在动画完成后应用正确的格式。 See jsfiddle here .请参阅此处的 jsfiddle

What is the appropriate way to set this up in css/jquery so that the inner expandable content shows as grid even during the first animation?在 css/jquery 中进行设置以便内部可扩展内容即使在第一个动画期间也显示为网格的合适方法是什么?

HTML HTML

<div class="user-products main">
        <div class="user-product">
            <div class ="collapsible-header">
                <span class="user-product-expand-toggle"><h4>&#9654; Product 1 - 2020-02-03 1:58pm</h4></span>
            </div>
            <div class="user-product-details hide">
                <div class="user-product-left">
                <p>Download All Files as Zip</p>
                <p>View Preview</p>
                <p>Details</p>
                <p>Details</p>
                <p>Details</p>
                </div>
                <div class="user-product-mid">
                <p>Details</p>
                <p>Details</p>
                <p>Details</p>
                <p>Details</p>
                <p>Details</p>
                </div>
                <div class="user-product-right">
                <div class="unpurchased-product"><button class="purchase-product">Purchase</button></div>
                </div>
            </div>
        </div>
        <div class="user-product">
            <div class ="collapsible-header">
                <span class="user-product-expand-toggle"><h4>&#9654; Product 2 - 2020-02-07 5:01pm</h4></span>
            </div>
            <div class="user-product-details hide">
                <div class="user-product-left">
                <p>Download All Files as Zip</p>
                <p>View Preview</p>
                <p>Details</p>
                <p>Details</p>
                <p>Details</p>
                </div>
                <div class="user-product-mid">
                <p>Details</p>
                <p>Details</p>
                <p>Details</p>
                <p>Details</p>
                <p>Details</p>
                </div>
                <div class="user-product-right">
                <div class="unpurchased-product"><button class="purchase-product">Purchase</button></div>
                </div>
            </div>
        </div>
    </div>

jQuery jQuery

$(".user-product-expand-toggle").click(function(){
    parentDiv = $(this).closest(".collapsible-header");
    $(parentDiv.siblings(".user-product-details")[0]).slideToggle('slow', function() {
        if ($(this).is(':visible'))
            $(this).css('display','grid');
    });

});

CSS CSS

.hide.hide {
  display: none;
}

.user-products.main {
    overflow-y:auto;
}
.user-product-details {
    margin-left:50px;
    display: grid;
    grid-template-columns: repeat(3, 1fr);
}

.collapsible-header{
    margin-left:50px;
    text-align: left;
    cursor: pointer;

}

Instead of using slideToggle, use slideUp and slideDown.而不是使用slideToggle,使用slideUp 和slideDown。

Example:例子:

$(".user-product-expand-toggle").click(function(){

    parentDiv = $(this).closest(".collapsible-header");

    let $element = $(parentDiv.siblings(".user-product-details")[0]);

    if ($element.is(':visible')) {
        $element.slideUp();
    }
    else {
        $element.slideDown({
            start: function() {
              $(this).css('display','grid');
            }
        });
    }

});

Demo Fiddle演示小提琴

jQuery use display property with slideToggle method. jQuery 使用带有slideToggle方法的display属性。 This jQuery design is incompatible with the alignment of the display property.这种 jQuery 设计与display属性的对齐方式不兼容。

Therefore, this problems easily workaround is following:因此,这个问题很容易解决的方法如下:

  1. Use height property slide animation and switched display property when animation start and end.在动画开始和结束时使用height属性幻灯片动画和切换display属性。
  2. Divide into two parts, one to hide the elements and one to align the elements.分为两部分,一是隐藏元素,一是对齐元素。

Simply solition is wrap grid container by hidden container (2. solution):简单的解决方法是通过隐藏容器包装网格容器(2.解决方案):

 $(".user-product-expand-toggle").click(function() { parentDiv = $(this).closest(".collapsible-header"); $(parentDiv.siblings(".hide")[0]).slideToggle('slow'); });
 .hide.hide { display: none; } .user-products.main { overflow-y: auto; } .user-product-details { margin-left: 50px; display: grid; grid-template-columns: repeat(3, 1fr); } .collapsible-header { margin-left: 50px; text-align: left; cursor: pointer; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="user-products main"> <div class="user-product"> <div class="collapsible-header"> <span class="user-product-expand-toggle"> <h4>&#9654; Product 1 - 2020-02-03 1:58pm</h4> </span> </div> <div class="hide"> <div class="user-product-details"> <div class="user-product-left"> <p>Download All Files as Zip</p> <p>View Preview</p> <p>Details</p> <p>Details</p> <p>Details</p> </div> <div class="user-product-mid"> <p>Details</p> <p>Details</p> <p>Details</p> <p>Details</p> <p>Details</p> </div> <div class="user-product-right"> <div class="unpurchased-product"><button class="purchase-product">Purchase</button></div> </div> </div> </div> </div> <div class="user-product"> <div class="collapsible-header"> <span class="user-product-expand-toggle"> <h4>&#9654; Product 2 - 2020-02-07 5:01pm</h4> </span> </div> <div class="hide"> <div class="user-product-details "> <div class="user-product-left"> <p>Download All Files as Zip</p> <p>View Preview</p> <p>Details</p> <p>Details</p> <p>Details</p> </div> <div class="user-product-mid"> <p>Details</p> <p>Details</p> <p>Details</p> <p>Details</p> <p>Details</p> </div> <div class="user-product-right"> <div class="unpurchased-product"><button class="purchase-product">Purchase</button></div> </div> </div> </div> </div> </div>

Wrap your div into another div .将您的div包装到另一个div

 <div id="block_for_toggle"> <div style="display: grid"></div> </div>

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

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