如何使用多个相邻列表创建下拉列表'

发布于 2025-02-11 17:36:00 字数 3008 浏览 1 评论 0原文

我正在尝试为购物式式订单编写代码,当一个人徘徊在导航栏上的目标元素上时,它的功能与此网站

我已经花了两天时间来查看许多页面,包括以前的Stackoverflow上的帖子,并尝试了似乎无限数量的方法,但是我根本无法破解这一点。

我应该强调的是,我已经可以为下拉列表编写代码,该下拉列表会在目标元素上徘徊时生成单个链接列表的列(这很容易);我真的在说对齐相邻的多列。

几年前,这里发布了此类问题的解决方案,但该人建议使用浮动方法。我确实尝试了这个,但是结果对我的项目(非常)不满意。

如果您可以帮助我解决这个问题,请(立即)查看我的代码渲染和绘制时的样子,我在 codepen ,但代码也在下面发布:

html:html:

.nav-list {
  list-style: none;
  border: 1px solid blue;
  display: flex;
  flex-flow: row wrap;
  align-items: left;
  justify-content: space-around;
  color: white;
  background-color: #429CD9;
}

#hire-dropdown {
  position: absolute;
  cursor: pointer;
  background-color: #429CD9;
}

.the-drop {
  display: none;
}

#hire-dropdown:hover>.the-drop {
  display: grid;
  grid-template-areas: "drop-1-list drop-2-list";
  grid-template-columns: 1fr 1fr;
}

.drop-1-list,
.drop-2-list {
  display: none;
}

.drop-1-list {
  grid-area: "drop-1-list";
}

.drop-2-list {
  grid-area: "drop-2-list";
}

.the-drop:hover>.drop-1-list,
.the-drop:hover>.drop-2-list {
  background-color: white;
  border: 1px solid black;
  list-style: none;
}
<nav>
  <ul class="nav-list">
    <li>Nothing</li>
    <li class="to-hire">
      <div id="hire-dropdown">To Hire
        <div class=".the-drop">
          <ul class="drop-1-list">
            <li><a href="#">Access</a></li>
            <li><a href="#">Breaking</a></li>
            <li><a href="#">Compaction</a></li>
            <li><a href="#">Cooling</a></li>
            <li><a href="#">Drilling</a></li>
            <li><a href="#">Lifting</a></li>
            <li><a href="#">Sanding</a></li>
            <li><a href="#">Sawing</a></li>
          </ul>
          <ul class="drop-2-list">
            <li><a href="#">Product 1</a></li>
            <li><a href="#">Product 2</a></li>
            <li><a href="#">Product 3</a></li>
            <li><a href="#">Product 4</a></li>
            <li><a href="#">Product 5</a></li>
          </ul>
        </div>

      </div>
    </li>
  </ul>
</nav>

如果您还想查看我之前提到的float方法解决方案,则可以看到它在这里(但我不认为这是我项目的正确解决方案)。

您的(建设性)帮助将不胜感激。

I'm trying to write code for a shopping-website style dropdown that when one hovers over a target element on the navigation bar, it functions like the one depicted at this site.

I've spent two days on this, looking at so many pages, including previous postings on Stackoverflow, and tried what seems like an endless number of approaches, but I've simply not been able to crack this.

I should stress that I can already write the code for a dropdown that generates a single column of list of links when one hovers over the target element (that's easy); I'm really talking about aligned adjacent multiple columns.

A solution for a problem like this was posted here on Stackoverflow several years ago, but the person suggested the float method. I did actually try this, but the results were (very) unsatisfactory for my project.

If you can help me with this, to (instantly) see what my code looks like when rendered and painted, I've posted the necessary bits of code on Codepen, but the code is also posted below:

HTML:

.nav-list {
  list-style: none;
  border: 1px solid blue;
  display: flex;
  flex-flow: row wrap;
  align-items: left;
  justify-content: space-around;
  color: white;
  background-color: #429CD9;
}

#hire-dropdown {
  position: absolute;
  cursor: pointer;
  background-color: #429CD9;
}

.the-drop {
  display: none;
}

#hire-dropdown:hover>.the-drop {
  display: grid;
  grid-template-areas: "drop-1-list drop-2-list";
  grid-template-columns: 1fr 1fr;
}

.drop-1-list,
.drop-2-list {
  display: none;
}

.drop-1-list {
  grid-area: "drop-1-list";
}

.drop-2-list {
  grid-area: "drop-2-list";
}

.the-drop:hover>.drop-1-list,
.the-drop:hover>.drop-2-list {
  background-color: white;
  border: 1px solid black;
  list-style: none;
}
<nav>
  <ul class="nav-list">
    <li>Nothing</li>
    <li class="to-hire">
      <div id="hire-dropdown">To Hire
        <div class=".the-drop">
          <ul class="drop-1-list">
            <li><a href="#">Access</a></li>
            <li><a href="#">Breaking</a></li>
            <li><a href="#">Compaction</a></li>
            <li><a href="#">Cooling</a></li>
            <li><a href="#">Drilling</a></li>
            <li><a href="#">Lifting</a></li>
            <li><a href="#">Sanding</a></li>
            <li><a href="#">Sawing</a></li>
          </ul>
          <ul class="drop-2-list">
            <li><a href="#">Product 1</a></li>
            <li><a href="#">Product 2</a></li>
            <li><a href="#">Product 3</a></li>
            <li><a href="#">Product 4</a></li>
            <li><a href="#">Product 5</a></li>
          </ul>
        </div>

      </div>
    </li>
  </ul>
</nav>

If you also want to see the float method solution I referred to earlier, you can see it here (but I do not believe this is the right solution for my project).

Your (constructive) help would be most appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凑诗 2025-02-18 17:36:00

不是100%确定您要如何处理所有狭窄的显示等。但是

  • &lt; div class =“。the -drop”&gt;删除标记中类上的
  • 期限在您有问题的情况下,与CSS在一起的时间删除了一些问题,以回到“基本”这项工作。我这样做了,并调整了一些CSS选择器。我认为不需要绝对
  • 借助弯曲,然后显示块,它会跳动一点,但我会把它留给您。
.nav-list {
  list-style: none;
  border: 1px solid blue;
  display: flex;
  flex-flow: row wrap;
  align-items: start;
  justify-content: space-around;
  color: white;
  background-color: #429CD9;
}

#hire-dropdown {
  cursor: pointer;
  background-color: #429CD9;
}

.the-drop,
.drop-1-list,
.drop-2-list {
  display: none;
}

#hire-dropdown:hover * {
  display: grid;
}

#hire-dropdown .the-drop {
  grid-template-areas: "drop-1-list drop-2-list";
  grid-template-columns: 1fr 1fr;
}

.drop-1-list,
.drop-2-list {
  background-color: white;
  border: 1px solid black;
  list-style: none;
}

.drop-1-list {
  grid-area: "drop-1-list";
}

.drop-2-list {
  grid-area: "drop-2-list";
}
<nav>
  <ul class="nav-list">
    <li>Nothing</li>
    <li class="to-hire">
      <div id="hire-dropdown">To Hire
        <div class="the-drop">
          <ul class="drop-1-list">
            <li><a href="#">Access</a></li>
            <li><a href="#">Breaking</a></li>
            <li><a href="#">Compaction</a></li>
            <li><a href="#">Cooling</a></li>
            <li><a href="#">Drilling</a></li>
            <li><a href="#">Lifting</a></li>
            <li><a href="#">Sanding</a></li>
            <li><a href="#">Sawing</a></li>
          </ul>
          <ul class="drop-2-list">
            <li><a href="#">Product 1</a></li>
            <li><a href="#">Product 2</a></li>
            <li><a href="#">Product 3</a></li>
            <li><a href="#">Product 4</a></li>
            <li><a href="#">Product 5</a></li>
          </ul>
        </div>

      </div>
    </li>
  </ul>
</nav>

Not 100% sure how you want to handle all the narrow displays etc. but

  • <div class=".the-drop"> remove the period on the class in the markup
  • Remove some CSS - most of the time with CSS when you have issues remove some to get back to "basic" this work your way to your goal. I did that and adjusted some CSS selectors. No need for the absolute I think.
  • with the flex and then display of the blocks it jumps around a bit but I will leave that to you as to how you address that.

.nav-list {
  list-style: none;
  border: 1px solid blue;
  display: flex;
  flex-flow: row wrap;
  align-items: start;
  justify-content: space-around;
  color: white;
  background-color: #429CD9;
}

#hire-dropdown {
  cursor: pointer;
  background-color: #429CD9;
}

.the-drop,
.drop-1-list,
.drop-2-list {
  display: none;
}

#hire-dropdown:hover * {
  display: grid;
}

#hire-dropdown .the-drop {
  grid-template-areas: "drop-1-list drop-2-list";
  grid-template-columns: 1fr 1fr;
}

.drop-1-list,
.drop-2-list {
  background-color: white;
  border: 1px solid black;
  list-style: none;
}

.drop-1-list {
  grid-area: "drop-1-list";
}

.drop-2-list {
  grid-area: "drop-2-list";
}
<nav>
  <ul class="nav-list">
    <li>Nothing</li>
    <li class="to-hire">
      <div id="hire-dropdown">To Hire
        <div class="the-drop">
          <ul class="drop-1-list">
            <li><a href="#">Access</a></li>
            <li><a href="#">Breaking</a></li>
            <li><a href="#">Compaction</a></li>
            <li><a href="#">Cooling</a></li>
            <li><a href="#">Drilling</a></li>
            <li><a href="#">Lifting</a></li>
            <li><a href="#">Sanding</a></li>
            <li><a href="#">Sawing</a></li>
          </ul>
          <ul class="drop-2-list">
            <li><a href="#">Product 1</a></li>
            <li><a href="#">Product 2</a></li>
            <li><a href="#">Product 3</a></li>
            <li><a href="#">Product 4</a></li>
            <li><a href="#">Product 5</a></li>
          </ul>
        </div>

      </div>
    </li>
  </ul>
</nav>

机场等船 2025-02-18 17:36:00

jQuery要处理事件,

$(function() {
  $(".to-hire").mouseenter(function() {
    $('.the-drop').show();
  }).mouseleave(function() {
    $('.the-drop').hide();
  });
});

您的类名称中有一个此处&lt; div class =“。the-drop”&gt;,将其删除。

从CSS中删除这些代码行

.drop-1-list,
.drop-2-list {
    display: none;
}

现在

Jquery to handle the events

$(function() {
  $(".to-hire").mouseenter(function() {
    $('.the-drop').show();
  }).mouseleave(function() {
    $('.the-drop').hide();
  });
});

You have a . in the class name here <div class=".the-drop">, remove it.

Remove these lines of code from your CSS

.drop-1-list,
.drop-2-list {
    display: none;
}

Now what should happen is, when your mouse goes onto the dropdown menu link, your dropdown should show, when it leaves it will hide

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文