Navbar下的面包屑和购物车展示
我想在我的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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
想到了两种解决方案。
保证金:按钮上的左
将其推到可用的进一步点。Flex-Grow:1
或flex:1
在面包板栏上,以便它使用所有可用空间,除了按钮使用的空间。工作codepen 示例在这里
Two solutions come to mind.
margin: left
on the button to push it to the further point available.flex-grow: 1
orflex: 1
on the breadcrumb bar so that it uses all the available space except for that used by your button.Working Codepen example here
好吧,您已经做到了,您只需要替换
&lt; span&gt; span&lt;/span&gt;
与您的实际项目一样,就您的格式而言,您不需要真正使用Flex由于每个项目都会被块范围div
这是您要生成动态内容的要做的事情:同样,您可以添加总量,然后在循环结束后转到CART按钮。
更新
我认为我遇到了一个问题,您希望您的购物车按钮走到极右边,让右侧占用整个空间,这是您应该做的:
添加``flex-grow'''''''
'现有样式:
更新&lt; 800px
只需使您的
.flex-container
blocked scoped,它将自动中心:工作代码段
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 scopeddiv
here's what you will have to do to generate the dynamic content: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:
Add ``flex-grow``` property to your stylesheet as:
and change the existing styles:
UPDATE <800px
Simply make your
.flex-container
block scoped and it will center automatically:Working Code Snippet