Navbar下的面包屑和购物车展示

发布于 2025-02-12 16:12:13 字数 2809 浏览 0 评论 0原文

我想在我的Navbar下添加两个元素。在右侧,您可以通过单击其单击内容和另一侧看到内容的手推车,面包屑都在同一行上对齐。 cf。图片相关

整个过程也将是动态的。我整天都试图找到解决方案而没有成功。最好的方法是什么?弹性显示?

预先感谢您的帮助。这是我的代码:

function myFunction() {
  document.getElementById("myDropdown").classList.toggle("show");
}
window.onclick = function(event) {
  if (!event.target.matches('.dropbtn')) {
    var dropdowns = document.getElementsByClassName("dropdown-content");
    var i;
    for (i = 0; i < dropdowns.length; i++) {
      var openDropdown = dropdowns[i];
      if (openDropdown.classList.contains('show')) {
        openDropdown.classList.remove('show');
      }
    }
  }
}
.breadcrumbs ul{
    list-style-type: none;
}
.dropbtn {
  background-color: #3498DB;
  color: white;
  padding: 16px;
  font-size: 16px;
  border: none;
  cursor: pointer;
}

.dropbtn:hover, .dropbtn:focus {
  background-color: #2980B9;
}

.dropdown {
  position: relative;
  display: inline-block;
}

.dropdown-content {
  display: none;
  position: absolute;
  background-color: #f1f1f1;
  min-width: 160px;
  overflow: auto;
  box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
  z-index: 1;
}

.dropdown-content a {
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
}

.dropdown a:hover {background-color: #ddd;}

.show {display: block;}

.flex-container {
  display: flex;
  flex-direction: row;
  font-size: 30px;
  text-align: center;
}

.flex-item-left {
  background-color: #f1f1f1;
  padding: 10px;
  flex: 50%;
}

.flex-item-right {
  background-color: dodgerblue;
  padding: 10px;
  flex: 50%;
}

/* Responsive layout - makes a one column-layout instead of two-column layout */
@media (max-width: 800px) {
  .flex-container {
    flex-direction: column;
  }
}
<div class="flex-container">
    <div class="flex-item-left">
        <div class="breadcrumbs">
            <ul>
                <li>
                    <a href="index.php">Home</a>
                </li>
                <li>
                    <a></a>
                </li>
            </ul>
        </div>
    </div>
    <div class="flex-item-right">
        <div class="dropdown">
        <button onclick="myFunction()" class="dropbtn">Cart</button>
        <div id="myDropdown" class="dropdown-content">
            <span>Cart item</span>  
        </div>
        </div>
    </div>
</div>

I would like to add two elements under my navbar. On the right side the cart of which you can see the content by clicking on it and on the other side, the breadcrumbs both aligned on a same row. cf. pic related

The whole thing would be dynamic also. I spent the whole day trying to find solutions without success. What would be the best way to do it? Flex display?

Thank you in advance for your help. Here is my code:

function myFunction() {
  document.getElementById("myDropdown").classList.toggle("show");
}
window.onclick = function(event) {
  if (!event.target.matches('.dropbtn')) {
    var dropdowns = document.getElementsByClassName("dropdown-content");
    var i;
    for (i = 0; i < dropdowns.length; i++) {
      var openDropdown = dropdowns[i];
      if (openDropdown.classList.contains('show')) {
        openDropdown.classList.remove('show');
      }
    }
  }
}
.breadcrumbs ul{
    list-style-type: none;
}
.dropbtn {
  background-color: #3498DB;
  color: white;
  padding: 16px;
  font-size: 16px;
  border: none;
  cursor: pointer;
}

.dropbtn:hover, .dropbtn:focus {
  background-color: #2980B9;
}

.dropdown {
  position: relative;
  display: inline-block;
}

.dropdown-content {
  display: none;
  position: absolute;
  background-color: #f1f1f1;
  min-width: 160px;
  overflow: auto;
  box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
  z-index: 1;
}

.dropdown-content a {
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
}

.dropdown a:hover {background-color: #ddd;}

.show {display: block;}

.flex-container {
  display: flex;
  flex-direction: row;
  font-size: 30px;
  text-align: center;
}

.flex-item-left {
  background-color: #f1f1f1;
  padding: 10px;
  flex: 50%;
}

.flex-item-right {
  background-color: dodgerblue;
  padding: 10px;
  flex: 50%;
}

/* Responsive layout - makes a one column-layout instead of two-column layout */
@media (max-width: 800px) {
  .flex-container {
    flex-direction: column;
  }
}
<div class="flex-container">
    <div class="flex-item-left">
        <div class="breadcrumbs">
            <ul>
                <li>
                    <a href="index.php">Home</a>
                </li>
                <li>
                    <a></a>
                </li>
            </ul>
        </div>
    </div>
    <div class="flex-item-right">
        <div class="dropdown">
        <button onclick="myFunction()" class="dropbtn">Cart</button>
        <div id="myDropdown" class="dropdown-content">
            <span>Cart item</span>  
        </div>
        </div>
    </div>
</div>

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

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

发布评论

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

评论(2

高速公鹿 2025-02-19 16:12:13

想到了两种解决方案。

  1. 使用保证金:按钮上的左将其推到可用的进一步点。
  2. 使用Flex-Grow:1flex:1在面包板栏上,以便它使用所有可用空间,除了按钮使用的空间。

工作codepen 示例在这里

Two solutions come to mind.

  1. Use margin: left on the button to push it to the further point available.
  2. Use flex-grow: 1 or flex: 1 on the breadcrumb bar so that it uses all the available space except for that used by your button.

Working Codepen example here

凝望流年 2025-02-19 16:12:13

好吧,您已经做到了,您只需要替换&lt; span&gt; span&lt;/span&gt;与您的实际项目一样,就您的格式而言,您不需要真正使用Flex由于每个项目都会被块范围div这是您要生成动态内容的要做的事情:

<div class="flex-item-right">
    <div class="dropdown">
    <button onclick="myFunction()" class="dropbtn">Cart</button>
    <div id="myDropdown" class="dropdown-content">
        <?php
            foreach($items as $item) {
        ?>
           <div>
                <?php $item['name'].' '.$item['quantity'].'x'; ?>
           </div>
        <?php
            }
        ?>
    </div>
    </div>
</div>

同样,您可以添加总量,然后在循环结束后转到CART按钮。

更新

我认为我遇到了一个问题,您希望您的购物车按钮走到极右边,让右侧占用整个空间,这是您应该做的:

<div class="flex-container">
    <div class="flex-item-left flex-grow-1">
        ...
    </div>
    <div class="flex-item-right">
        ...
    </div>
</div>

添加``flex-grow'''''''

.flex-grow-1 {
    flex-grow:1;
}

'现有样式:

.flex-item-left {
  background-color: #f1f1f1;
  padding: 10px;
}

.flex-item-right {
  background-color: dodgerblue;
  padding: 10px;
}

更新&lt; 800px

只需使您的.flex-container blocked scoped,它将自动中心:

@media (max-width: 800px) {
  .flex-container {
    display:block;
   }

工作代码段

function myFunction() {
  document.getElementById("myDropdown").classList.toggle("show");
}
window.onclick = function(event) {
  if (!event.target.matches('.dropbtn')) {
    var dropdowns = document.getElementsByClassName("dropdown-content");
    var i;
    for (i = 0; i < dropdowns.length; i++) {
      var openDropdown = dropdowns[i];
      if (openDropdown.classList.contains('show')) {
        openDropdown.classList.remove('show');
      }
    }
  }
}
.breadcrumbs ul{
    list-style-type: none;
}
.dropbtn {
  background-color: #3498DB;
  color: white;
  padding: 16px;
  font-size: 16px;
  border: none;
  cursor: pointer;
}

.dropbtn:hover, .dropbtn:focus {
  background-color: #2980B9;
}

.dropdown {
  position: relative;
  display: inline-block;
}

.dropdown-content {
  display: none;
  position: absolute;
  background-color: #f1f1f1;
  min-width: 160px;
  overflow: auto;
  box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
  z-index: 1;
}

.dropdown-content a {
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
}

.dropdown a:hover {background-color: #ddd;}

.show {display: block;}

.flex-container {
  display: flex;
  flex-direction: row;
  font-size: 30px;
  text-align: center;
}

.flex-item-left {
  background-color: #f1f1f1;
  padding: 10px;
}

.flex-item-right {
  background-color: dodgerblue;
  padding: 10px;
}
.flex-grow-1{
    flex-grow:1;
}

/* Responsive layout - makes a one column-layout instead of two-column layout */
@media (max-width: 800px) {
  .flex-container {
    display:block;
  }
}
<div class="flex-container">
    <div class="flex-item-left flex-grow-1">
        <div class="breadcrumbs">
            <ul>
                <li>
                    <a href="index.php">Home</a>
                </li>
                <li>
                    <a></a>
                </li>
            </ul>
        </div>
    </div>
    <div class="flex-item-right">
        <div class="dropdown">
        <button onclick="myFunction()" class="dropbtn">Cart</button>
        <div id="myDropdown" class="dropdown-content">
            <span>Cart item</span>  
        </div>
        </div>
    </div>
</div>

Well, you are already doing it correctly, you just have to replace the <span>Cart Item</span> with your actual items, as of the formatting you don't really need to use flex since every item will be block scoped div here's what you will have to do to generate the dynamic content:

<div class="flex-item-right">
    <div class="dropdown">
    <button onclick="myFunction()" class="dropbtn">Cart</button>
    <div id="myDropdown" class="dropdown-content">
        <?php
            foreach($items as $item) {
        ?>
           <div>
                <?php $item['name'].' '.$item['quantity'].'x'; ?>
           </div>
        <?php
            }
        ?>
    </div>
    </div>
</div>

and similarly you can add the total amount and go to cart button after the loop ends.

UPDATE

I think I got the question wrong, you want your cart button to go extreme right and let the right side take whole space, here's what you should do:

<div class="flex-container">
    <div class="flex-item-left flex-grow-1">
        ...
    </div>
    <div class="flex-item-right">
        ...
    </div>
</div>

Add ``flex-grow``` property to your stylesheet as:

.flex-grow-1 {
    flex-grow:1;
}

and change the existing styles:

.flex-item-left {
  background-color: #f1f1f1;
  padding: 10px;
}

.flex-item-right {
  background-color: dodgerblue;
  padding: 10px;
}

UPDATE <800px

Simply make your .flex-container block scoped and it will center automatically:

@media (max-width: 800px) {
  .flex-container {
    display:block;
   }

Working Code Snippet

function myFunction() {
  document.getElementById("myDropdown").classList.toggle("show");
}
window.onclick = function(event) {
  if (!event.target.matches('.dropbtn')) {
    var dropdowns = document.getElementsByClassName("dropdown-content");
    var i;
    for (i = 0; i < dropdowns.length; i++) {
      var openDropdown = dropdowns[i];
      if (openDropdown.classList.contains('show')) {
        openDropdown.classList.remove('show');
      }
    }
  }
}
.breadcrumbs ul{
    list-style-type: none;
}
.dropbtn {
  background-color: #3498DB;
  color: white;
  padding: 16px;
  font-size: 16px;
  border: none;
  cursor: pointer;
}

.dropbtn:hover, .dropbtn:focus {
  background-color: #2980B9;
}

.dropdown {
  position: relative;
  display: inline-block;
}

.dropdown-content {
  display: none;
  position: absolute;
  background-color: #f1f1f1;
  min-width: 160px;
  overflow: auto;
  box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
  z-index: 1;
}

.dropdown-content a {
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
}

.dropdown a:hover {background-color: #ddd;}

.show {display: block;}

.flex-container {
  display: flex;
  flex-direction: row;
  font-size: 30px;
  text-align: center;
}

.flex-item-left {
  background-color: #f1f1f1;
  padding: 10px;
}

.flex-item-right {
  background-color: dodgerblue;
  padding: 10px;
}
.flex-grow-1{
    flex-grow:1;
}

/* Responsive layout - makes a one column-layout instead of two-column layout */
@media (max-width: 800px) {
  .flex-container {
    display:block;
  }
}
<div class="flex-container">
    <div class="flex-item-left flex-grow-1">
        <div class="breadcrumbs">
            <ul>
                <li>
                    <a href="index.php">Home</a>
                </li>
                <li>
                    <a></a>
                </li>
            </ul>
        </div>
    </div>
    <div class="flex-item-right">
        <div class="dropdown">
        <button onclick="myFunction()" class="dropbtn">Cart</button>
        <div id="myDropdown" class="dropdown-content">
            <span>Cart item</span>  
        </div>
        </div>
    </div>
</div>

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