答案:
要在按钮单击中创建下拉菜单,您可以通过以下方法来实现:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>下拉菜单示例</title>
<style>
/* 下拉菜单样式 */
.dropdown {
position: relative;
display: inline-block;
cursor: pointer;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f1f1f1;
min-width: 160px;
overflow: auto;
border: 1px solid #ccc;
z-index: 1;
}
.dropdown:hover .dropdown-content {
display: block;
}
</style>
</head>
<body>
<h2>下拉菜单示例</h2>
<div class="dropdown">
<button onclick="myFunction()">下拉菜单</button>
<div class="dropdown-content" id="myDropdown">
<a href="#">选项1</a>
<a href="#">选项2</a>
<a href="#">选项3</a>
</div>
</div>
<script>
// JavaScript 代码
function myFunction() {
var dropdown = document.getElementById("myDropdown");
dropdown.classList.toggle("show");
}
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>下拉菜单动画示例</title>
<style>
/* 下拉菜单样式 */
.dropdown {
position: relative;
display: inline-block;
cursor: pointer;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f1f1f1;
min-width: 160px;
overflow: auto;
border: 1px solid #ccc;
z-index: 1;
}
.dropdown:hover .dropdown-content {
display: block;
}
</style>
</head>
<body>
<h2>下拉菜单动画示例</h2>
<div class="dropdown">
<button onclick="myFunction()">下拉菜单</button>
<div class="dropdown-content" id="myDropdown">
<a href="#">选项1</a>
<a href="#">选项2</a>
<a href="#">选项3</a>
</div>
</div>
<script>
// JavaScript 代码
function myFunction() {
var dropdown = document.getElementById("myDropdown");
dropdown.classList.toggle("show");
}
document.addEventListener("click", function(event) {
var dropdown = document.getElementById("myDropdown");
if (event.target == dropdown) {
dropdown.classList.remove("show");
}
});
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>平滑动画示例</title>
<style>
/* 下拉菜单样式 */
.dropdown {
position: relative;
display: inline-block;
cursor: pointer;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f1f1f1;
min-width: 160px;
overflow: auto;
border: 1px
领取专属 10元无门槛券
手把手带您无忧上云