[英]Menu Preview on Navbar
我正在为模拟电子商务网站构建一个简单的响应式下拉菜单,以供娱乐。 我目前有一个导航栏,它在最右边有一个列表项,它将检测用户何时使用我定义的 onmouseenter 侦听器将鼠标悬停在它上面。 我希望建立一个基本的下拉菜单,在 css 中的一个简单块,它将显示在我的所有其他 html 之上并且不会破坏页面。
我附上了我认为是基本代码和一些注释来解释发生了什么。 请让我知道我需要什么样式、教程或更多我需要的 typescript。 谢谢你。
页.html
<!-- inside of navbar, listener that invokes on mouse events through previewable id -->
<!-- list the item that will be clicked on as an item inside the navbar -->
<!-- hyperlink attached to listener that invokes on mouse events through cartable id -->
<div class="navbar-nav" id="previewable">
<li class="nav-item">
<a class="nav-link" routerLink="/cart" id="cartable">
<!-- the dropdown menu container -->
<!-- the dropdown menu -->
<div class="dropdown-cart-containter">
<div class="dropdown-cart">
页面.ts
/* attach event listeners that will respond when the mouse hovers over the html item */
ngOnInit(): void
document.getElementById('cartable').addEventListener('mouseenter', this.onmouseenter, false);
ngOnDestroy(): void
document.getElementById('cartable').removeEventListener('mouseenter', this.onmouseenter, false);
/* definition for onmouseenter that is attached to listener */
onmouseenter = (): void => {
const cartElement = document.getElementById('cartable');
const previewElement = document.getElementById('previewable');
previewElement.classList.add('dropdown-cart-containter');
previewElement.classList.add('dropdown-cart');
}
页.css
/* shopping cart on mouse enter preview */
.dropdown-cart-containter {
background-color: blueviolet;
}
.dropdown-cart {
background-color: red;
}
我找到了一个与您的问题非常吻合的解决方案。
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<style>
body {
font-family: Arial, Helvetica, sans-serif;
}
.navbar {
overflow: hidden;
background-color: #333;
}
.navbar a {
float: left;
font-size: 16px;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
.dropdown {
float: left;
overflow: hidden;
}
.dropdown .dropbtn {
font-size: 16px;
border: none;
outline: none;
color: white;
padding: 14px 16px;
background-color: inherit;
font-family: inherit;
margin: 0;
}
.navbar a:hover, .dropdown:hover .dropbtn {
background-color: red;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
.dropdown-content a {
float: none;
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content a:hover {
background-color: #ddd;
}
.dropdown:hover .dropdown-content {
display: block;
}
</style>
</head>
<body>
<div class="navbar">
<a href="#home">Home</a>
<a href="#news">News</a>
<div class="dropdown">
<button class="dropbtn">Dropdown
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-content">
<a href="#">Link 1</a>
<a href="#">Link 2</a>
<a href="#">Link 3</a>
</div>
</div>
</div>
<h3>Dropdown Menu inside a Navigation Bar</h3>
<p>Hover over the "Dropdown" link to see the dropdown menu.</p>
</body>
</html>
这应该是你正在寻找的。 你可以在这里寻找一堆代码示例
我希望这可以帮助你:)
创建指令并将@HostListener用于事件,将Renderer2用于类。 在 Angular 中,您不应直接触摸文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.