【问题标题】:CSS Flexbox need accordian effect on hoverCSS Flexbox 在悬停时需要手风琴效果
【发布时间】:2022-01-18 11:48:22
【问题描述】:

我正在尝试在使用 css flex-box 构建的列字段悬停上构建一种手风琴效果。当我应用很少的样式时,列也在移动位置,而不是单独的底行。有人可以在这里帮忙吗。这是检查问题的小提琴。在类别悬停时,描述内容打开并推动下面的列。

https://jsfiddle.net/rc402/rx7gmbca/1/

.product-container {
  position: relative;
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  align-items: flex-start;
  list-style: none;
  width: 100vw;
  overflow: hidden;
  padding: 0;
  font-family: "Open Sans', sans-serif";
  letter-spacing: -0.005em;
  color: black;
  margin: 0 8rem;
  column-gap: 3.2rem;

  .l-product {
    flex: 0 0 auto;
    margin-top: 4rem;
    margin-top: 6rem;

    img {
      height: 29.6rem;
      width: 29.6rem;
      border: 1px solid gray;
      border-radius: 2px;
    }

    .name {
      margin: 2.4rem 0 0 0;
      width: 10.8rem;
      height: 1.8rem;
      font-style: normal;
      font-weight: 700;
      font-size: 16px;
      line-height: 18px;
      text-align: left;
    }

    .role {
      margin: 1.6rem 0 0 0;
      width: 20.9rem;
      height: 1.6rem;
      font-style: italic;
      font-weight: normal;
      font-size: 14px;
      line-height: 16px;
      text-decoration-line: underline;
      text-align: left;
    }
  }

  .hide {
    display: none;
  }

  .product-description:hover+.hide {
    margin-top: 162px;
    display: block;
    width: 1280px;
    height: 526px;
    background: #ffffff;
    border: 1px solid #d9d9d9;
    box-shadow: 0px 2px 8px rgba(0, 0, 0, 0.1);
    border-radius: 2px;
  }
}

另外,如果我需要点击任何关于如何做的指针来实现同样的事情?

【问题讨论】:

  • 我已添加解决方案,请查看jsfiddle.net/pczy8rhj/1
  • 您希望 .hide 的内容是全角还是图像/列的宽度?由于它只会在您将鼠标悬停在类别文本上时显示,如果您将光标移动到其他地方,内容将立即隐藏。这是预期的行为吗?
  • @Aman sharma 它不工作。产品 2 和产品 3 将进入下一行。
  • @Kathara,我想满足于全宽。在每个产品悬停时,描述应该以全宽可见,推动下面的行。啊!!感谢您指出,我的最终目的是在点击时完成这项工作,但我做了一个最小的小提琴,以便如果悬停有效,我可以将其转换为点击场景。
  • @Kumar 好的,那么这不适用于 flexbox。问题是 .hide 的内容在 flexbox-element 内。一旦 .hide-container 变得可见,它就会展开。所以整个 flexbox-element 被扩展到那个宽度,然后下推下一个项目。我会尝试创建您想要的效果,但您需要在 .hide-container 上使用绝对定位。

标签: css flexbox


【解决方案1】:

@库马尔 它还没有完全完成,但我想我创造了你想要的效果: Codepen: CSS Flexbox need accordion effect on hover (SO)

魔术的主要部分现在发生在 javascript 中,主要是在 initProductDesc 中。我使用了 jQuery,但您当然可以将其更改为 vanilla。 您仍然需要通过添加边距来调整容器的初始高度。

$('.hide').each(function() {
    $hide = $(this);
    $hide.attr('data-initial-height', $hide.height()); // change height to include margin-top too -> outerHeigth() or something for example.
    $hide.addClass('initialized');
});

这有帮助吗?

如果您必须在响应中更改容器的宽度和高度,这也应该可以工作,并且它也可以与 .hide-container 的动态高度一起工作(至少我认为,但未经测试)。


完整代码:

html(必须删除 base64 图像代码 -> 字符限制):

<ul class="product-container" id="">
    <li class="l-product">
        <img src="image.png" />
        <p class="name">Product 2</p>
        <p class="role product-description">category</p>
        <div class="hide">product description.</div>
    </li>
    <li class="l-product">
        <img src="image.png" />
        <p class="name">Product 5</p>
        <p class="role product-description">category</p>
        <div class="hide">product description.</div>
    </li>
    <li class="l-product">
        <img src="image.png" />
        <p class="name">Product 6</p>
        <p class="role leader-description">category</p>
        <div class="hide">product description.</div>
    </li>
    <li class="l-product">
        <img src="image.png" />
        <p class="name">Product 7</p>
        <p class="role leader-description">category</p>
        <div class="hide">product description.</div>
    </li>
    <li class="l-product">
        <img src="image.png" />
        <p class="name">Product</p>
        <p class="role product-description">category</p>
        <div class="hide">product description.</div>
    </li>
    <li class="l-product">
        <img src="image.png" />
        <p class="name">Product 8</p>
        <p class="role product-description">category</p>
        <div class="hide">product description.</div>
    </li>
    <li class="l-product">
        <img src="image.png" />
        <p class="name">Product 9</p>
        <p class="role product-description">category</p>
        <div class="hide">product description.</div>
    </li>
    <li class="l-product">
        <img src="image.png" />
        <p class="name">Product 10</p>
        <p class="role product-description">category</p>
        <div class="hide">product description.</div>
    </li>
    <li class="l-product">
        <img src="image.png" />
        <p class="name">Product 11</p>
        <p class="role product-description">category</p>
        <div class="hide">product description.</div>
    </li>
    <li class="l-product">
        <img src="image.png" />
        <p class="name">Product 12</p>
        <p class="role product-description">category</p>
        <div class="hide">product description.</div>
    </li>
</ul>

scss:

.product-container {
    position: relative;
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    align-items: flex-start;
    list-style: none;
    width: 100vw;
    overflow: hidden;
    padding: 0;
    font-family: "Open Sans', sans-serif";
    letter-spacing: -0.005em;
    color: black;
    margin: 0 8rem;
    column-gap: 3.2rem;
    overflow: hidden;
    
    &, & * {
        box-sizing: border-box;
    }

    .l-product {
        flex: 0 0 auto;
        margin-top: 4rem;
        margin-top: 6rem;
        position: relative;
        overflow: hidden;
        padding-bottom: 0;
        transition: all 0.5s ease-in;       
    
        &.open {
            overflow: visible;
        }

        img {
            height: 29.6rem;
            width: 29.6rem;
            border: 1px solid gray;
            border-radius: 2px;
        }

        .name {
            margin: 2.4rem 0 0 0;
            width: 10.8rem;
            height: 1.8rem;
            font-style: normal;
            font-weight: 700;
            font-size: 16px;
            line-height: 18px;
            text-align: left;
        }

        .role {
            margin: 1.6rem 0 0 0;
            width: 20.9rem;
            height: 1.6rem;
            font-style: italic;
            font-weight: normal;
            font-size: 14px;
            line-height: 16px;
            text-decoration-line: underline;
            text-align: left;
        }
    
        .hide {
            display: block;
            width: 1280px;
            height: 526px;
            position: absolute;
            margin-top: 162px;
            background: #ffffff;
            border: 1px solid #d9d9d9;
            box-shadow: 0px 2px 8px rgba(0, 0, 0, 0.1);
            border-radius: 2px;
            transition: all 0.5s ease-in;
        }
    
        &:not(.open) {
            .hide {
                &.initialized {
                    height: 0;
                }
            }
        }
    }
}

javascript/jQuery:

var initProductDesc = function() {
    $('.hide').each(function() {
        $hide = $(this);
        $hide.attr('data-initial-height', $hide.height());
        $hide.addClass('initialized');
    });
    
    $('.product-description').on('click', function() {
        $thisDescription = $(this);
        $thisLi = $thisDescription.closest('.l-product');
        $thisHide = $thisDescription.siblings('.hide');
        toggledOpen = toggleClass($thisLi, 'open');
        
        if (toggledOpen) {
            $thisLi.css('padding-bottom', $thisDescription.siblings('.hide').attr('data-initial-height') + 'px');
        }
        else {
            $thisLi.css('padding-bottom', '');
        }
    });
}

var toggleClass = function($element, classname) {
    if ($element.hasClass(classname)) {
        $element.removeClass(classname);
        return false;
    }
    else {
        $element.addClass(classname);
        return true;
    }
}

$(function() {
    initProductDesc();
});

【讨论】:

  • 谢谢卡塔拉。您是否认为通过小调整我们可以始终为行中的所有项目显示从左 0 开始的描述框,而不是为每个项目打开每个框?
  • 如果它比产品容器宽,则不会。否则描述将重叠。但除此之外...是的,您可以通过简单地删除描述上的绝对定位来轻松更改它,如果在响应式布局中的某个时刻您不想再使用点击...或者您可以显示所有描述只是在展示它们时不解决一个问题。
  • 这个解决方案对我来说效果很好,只需进行一些调整。只有一个问题是焦点。您能否帮助如何使用填充底部技术将可见描述集中在点击上。我尝试了 element.focus() 和 window.scrollTop(element.offset.top - 10)。他们俩都有跳跃经验,但没有专注于可见的描述。
  • 你需要的是一个动画;如果您使用的是 jquery,您可以简单地使用 `$('html, body').animate({ scrollTop: element.offset().top }, 200);` 如果您不使用 jquery,它会有点困难,但是我找到了这篇文章pawelgrzybek.com/page-scroll-in-vanilla-javascript,我相信你在谷歌搜索时会找到其他人。
猜你喜欢
  • 2021-07-24
  • 2012-05-30
  • 2014-07-20
  • 2014-10-30
  • 2014-12-15
  • 1970-01-01
  • 1970-01-01
  • 2012-02-19
相关资源
最近更新 更多