我设法结合了一些代码和样式来生成一个下拉菜单,向下滚动时该菜单会固定在页面的上部。当我将鼠标悬停在菜单的一个按钮上时,我想要的大部分功能都在那里:弹出一个不同颜色的子按钮 block ,当我悬停它们时,它们会改变颜色。但是,我不希望整个导航栏(菜单)的高度与已下降的子按钮的数量成比例地向下增加。
如您所见,整个菜单向下延伸。我只希望子按钮下拉(白色的,而不是它们周围的黑暗) 这是我的代码:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Chart.js</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
* {
box-sizing: border-box;
}
/* Style the body */
body {
font-family: Arial, Helvetica, sans-serif;
margin: 0;
}
/* Header/logo Title */
.header {
padding: 80px;
text-align: center;
background: #ff9933;
color: #000000;
font-family: Arial, Helvetica, sans-serif;
}
/* Increase the font size of the heading */
.header h1 {
font-size: 45px;
}
.navbar {
overflow: hidden;
background-color: #333;
position: sticky;
position: -webkit-sticky;
top: 0;
}
.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;
position: sticky;
}
/* Column container */
.row {
display: -ms-flexbox; /* IE10 */
display: flex;
-ms-flex-wrap: wrap; /* IE10 */
flex-wrap: wrap;
}
/* Create two unequal columns that sits next to each other */
/* Sidebar/left column */
.side {
-ms-flex: 30%; /* IE10 */
flex: 30%;
background-color: #f1f1f1;
padding: 20px;
}
/* Main column */
.main {
-ms-flex: 70%; /* IE10 */
flex: 70%;
background-color: #fff9e6;
padding: 20px;
}
.a {
background-color: #000000
}
/* Footer */
.footer {
padding: 20px;
text-align: center;
background: #ddd;
}
/* Responsive layout - when the screen is less than 700px wide, make the two columns stack on top of each other instead of next to each other */
@media screen and (max-width: 700px) {
.row {
flex-direction: column;
}
}
/* Responsive layout - when the screen is less than 400px wide, make the navigation links stack on top of each other instead of next to each other */
@media screen and (max-width: 400px) {
.navbar a {
float: none;
width: 100%;
}
}
</style>
</head>
<body>
<div class="header">
<h1>Chart.js</h1>
<img src="https://www.chartjs.org/img/chartjs-logo.svg" alt="Logo" style="width:320px;height:240px;">
</div>
<div class="navbar">
<a href="./Chartjs-main.html">Въведение</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>
<a href="#footer" class="right">За мен</a>
</div>
<div class="row">
<div class="main">
<h1 id="Intro">Въведение</h1>
<h2>Какво са Chart.js</h2>
<hr> <br>
<p>Прости, но гъвкави JavaScript графи за дизайнери и разработчици</p>
<p>Chart.js е библиотека на JavaScript, която ви позволява да рисувате различни видове диаграми, като използвате елемента на платното HTML5. Тъй като използва платно, трябва да включите полифил, за да поддържате по-стари браузъри.</p>
<br>
<h2 id="Installation">Инсталация</h2>
<hr> <br>
<p>Chart.js може да бъде инсталиран чрез <a href="https://www.npmjs.com/package/chart.js">npm</a> или <a href="https://libraries.io/bower/chartjs">bower</a>. Препоръчително е да получите Chart.js по този начин.</p>
<p> При инсталацията се стига през CLI до папката на проекта и се изпълнява следната команда: <a style="bold:true; color: red">npm install chart.js --save</a> за NPM и <a style="bold:true; color: red">bower install chart.js --save за bower.</a></p>
<p>Chart.js поддържа Line, (хоризонтална и вертикална) Bar, Radar, Polar, Area, Pie и Donut диаграми, отзивчив е и предоставя опции за разширяване на тези типове диаграми и писане на нови.</p>
<br>
<h1 id="Integration">Интеграция</h1>
<hr> <br>
<p>Chart.js може да бъде интегриран с обикновен JavaScript или с различни модулни товарачи. Примерите по-долу показват как да зареждате Chart.js</p>
<br>
<h2 id="Tags">Тагове за включване</h2>
<hr> <br>
<p><script src="path/to/chartjs/dist/Chart.js"></script><br>
<script><br>
var myChart = new Chart(ctx, {...}); <br>
</script></p>
<p>За да върви кода нататък, нужно е да бъде добавен първия ред горе, като path/to/ заменим с директорията където е инсталиран Chart.js </p>
<br>
<p>
<canvas id="myChart" width="400" height="400"></canvas>
<br><script>
<br>var ctx = document.getElementById('myChart').getContext('2d');
<br>var myChart = new Chart(ctx, {
<br> type: 'bar',
<br> data: {
<br> labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
<br> datasets: [{
<br> label: '# of Votes',
<br> data: [12, 19, 3, 5, 2, 3],
<br> backgroundColor: [
<br> 'rgba(255, 99, 132, 0.2)',
<br> 'rgba(54, 162, 235, 0.2)',
<br> 'rgba(255, 206, 86, 0.2)',
<br> 'rgba(75, 192, 192, 0.2)',
<br> 'rgba(153, 102, 255, 0.2)',
<br> 'rgba(255, 159, 64, 0.2)'
<br> ],
<br> borderColor: [
<br> 'rgba(255, 99, 132, 1)',
<br> 'rgba(54, 162, 235, 1)',
<br> 'rgba(255, 206, 86, 1)',
<br> 'rgba(75, 192, 192, 1)',
<br> 'rgba(153, 102, 255, 1)',
<br> 'rgba(255, 159, 64, 1)'
<br> ],
<br> borderWidth: 1
<br> }]
<br> },
<br> options: {
<br> scales: {
<br> yAxes: [{
<br> ticks: {
<br> beginAtZero: true
<br> }
<br> }]
<br> }
<br> }
<br>});
<br></script>
<br>
<p>
<canvas id="myChart" width="400" height="400"></canvas>
<script src="./node_modules/chart.js/dist/Chart.js"></script>
<script>
var ctx = document.getElementById('myChart').getContext('2d');
var myChart = new Chart(ctx, {
type: 'bar', data: {
labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
datasets: [{ label: '# of Votes', data: [12, 19, 3, 5, 2, 3],
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)'
],
borderColor: [
'rgba(255, 99, 132, 1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)'
],
borderWidth: 1
}]
},
options: { scales: { yAxes: [{ ticks: { beginAtZero: true } }] } } });
</script>
<table border="1">
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th>Default</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>duration</code></td>
<td><code>number</code></td>
<td><code>1000</code></td>
<td>The number of milliseconds an animation takes.</td>
</tr>
<tr>
<td><code>easing</code></td>
<td><code>string</code></td>
<td><code>'easeOutQuart'</code></td>
<td>Easing function to use.
</tr>
<tr>
<td><code>onProgress</code></td>
<td><code>function</code></td>
<td><code>null</code></td>
<td>Callback called on each step of an animation.
</tr>
<tr>
<td><code>onComplete</code></td>
<td><code>function</code></td>
<td><code>null</code></td>
<td>Callback called at the end of an animation.</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="footer" id="footer">
<h2>ФН:81477 e-mail: mister.senko@abv.bg</h2>
</div>
</body>
最佳答案
更新这些代码,它应该可以工作了:
.navbar {
background-color: #333;
position: sticky;
position: -webkit-sticky;
top: 0;
display: inline-block;
width: 100%;
}
.dropdown {
float: left;
}
.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;
top:100%;
}
.dropdown:hover .dropdown-content {
display: block;
}
注意:在处理下拉菜单时应避免使用 overflow: hidden。
关于html - 如何在悬停按钮时制作具有透明下拉背景的粘性下拉菜单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59661713/