gpt4 book ai didi

javascript - 如何固定下拉菜单中链接的位置

转载 作者:行者123 更新时间:2023-11-28 02:47:09 25 4
gpt4 key购买 nike

我正在尝试创建一个菜单,我将鼠标悬停在一个按钮上,然后会在下方显示一堆子链接。但是,当我尝试在一行中执行多个按钮时,假设我将鼠标悬停在第二个按钮上,它的链接仍显示在第一个按钮下方。

代码如下:

HTML

<!DOCTYPE html>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<html>
<head>
<title>Vezba</title>
<link rel="stylesheet" type="text/css" href="style/style.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="script/script.js"></script>
</head>
<body>
<h1>Website</h1>
<header>
<ul>
<li><a class="fb" href="https://Facebook.com"></a></li>
<li><a class="reddit" href="https://Reddit.com"></a></li>
<li><a class="yt" href="https://YouTube.com"></a></li>
</ul>
</header>
<nav>
<div class="dd">
<button id="btn">First</button>
<div class="ddb">
<a href="#">Link 1</a>
<a href="#">Link 2</a>
<a href="#">Link 3</a>
</div>
<button id="btn2">Second</button>
<div class="ddb2">
<a href="#">Link 4</a>
<a href="#">Link 5</a>
<a href="#">Link 6</a>
</div>
</div>
</nav>
<br />
<br />
<article>
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,
when an unknown printer took a galley of type and scrambled it to make a type specimen book.
It has survived not only five centuries, but also the leap into electronic typesetting,
remaining essentially unchanged.
It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages,
and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</article>


<input type="button" value="Submit" onclick="Message();"/>

</body>

</html>

CSS

body
{
background-color:beige;
}
header
{
text-align:center;
}
ul
{
display:inline-block;
}
li
{
list-style-type:none;
float:left;
padding:5px;
text-align:center;
}
a:link,a:visited,a:hover,a:active
{
background-size:cover;
background-repeat:no-repeat;
color: white;
padding: 15px 15px;
text-align: center;
text-decoration: none;
display: inline-block;
}
a.fb:link, a.fb:visited,a.fb:hover, a.fb:active {
background-image:url(../img/fb.png);
}
a.reddit:link, a.reddit:visited,a.reddit:hover, a.reddit:active {
background-image:url(../img/reddit.png);
}
a.yt:link, a.yt:visited,a.yt:hover, a.yt:active {
background-image:url(../img/yt.png);
}
nav
{
font-size:10px;
background-color:gray;
}
.dd
{
display: inline-block;
float:left;
position:relative;
}
.dd button
{
width:100px;
}
.ddb, .ddb2 {
display:none;
color: black;
text-decoration: none;
background-color:gray;
position:absolute;
}

.ddb a, .ddb2 a{

color: black;
padding: 12px 16px;
text-decoration: none;
text-align:center;
display: block;
}

/*.dd:hover .ddb {
display:grid;
padding-bottom:10px;
}*/
article
{
border: 1px solid black;
border-radius:10px;
font-size:9px;
margin-right:200px;
padding:2px;
}
button
{
border: 1px solid black;
background-color:black;
color:white;
}
h1
{
text-align:left;
font-size:15px;
font-family:Andalus;
}

JavaScript/JQuery

$(document).ready(function ()
{
$("#btn").on({
mouseenter: function () {
$(".ddb").show();
},
mouseleave: function () {
$(".ddb").hide();
}

});

$("#btn2").on({
mouseenter: function () {
$(".ddb2").show();
},
mouseleave: function () {
$(".ddb2").hide();
}

});

});


function Message()
{
alert("Ordered!");
}

或者如果它会让它变得更容易,我将它添加到 jsfiddle:

https://jsfiddle.net/gwwxuvka/

再次,麻烦的部分是下面的按钮,缺少图片菜单部分工作正常。我能摆脱这个困惑的最好办法是第一个按钮链接正常显示,第二个按钮链接在鼠标悬停时跨越两个按钮区域。

最佳答案

实际上,这个简单的任务不需要使用 jquery。像我一样这样做。这是 Jsfiddle 链接

https://jsfiddle.net/gwwxuvka/14/

关于javascript - 如何固定下拉菜单中链接的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46869021/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com