gpt4 book ai didi

javascript - 图像在全宽上扩展了链接触发范围

转载 作者:行者123 更新时间:2023-11-30 14:25:50 24 4
gpt4 key购买 nike

我有一个移动菜单,里面有一些元素。每个元素都是一个链接。第一项是 Logo ,其他项是基于文本的链接。

基于文本的链接与其文本一样大。所以如果我点击 div 容器,链接将不会触发。

我为我的问题提供了一个完整的例子

$(document).ready(() => {
$("#btnMenu").click(() => {
toggleMenu();
});

$(".navbarLink").click(() => {
if ($("#navbarItems").hasClass("activeNavbar")) {
toggleMenu();
}
});
});

function toggleMenu() {
$("#navbarItems").toggleClass("activeNavbar");
toggleMenuBtn();
}

function toggleMenuBtn() {
$("#btnMenu").toggleClass("activeMenuBtn");
}
.link {
text-decoration: none;
}

body {
margin: 0;
}

#navbar {
height: 60px;
top: 0;
padding-left: 200px;
padding-right: 200px;
position: sticky;
background: #1e222a;
}

#navbarItems {
height: 100%;
display: flex;
align-items: center;
}

#logoLink {
display: flex;
align-items: center;
}

#navbarItems .navbarItemContainer:not(:first-child) {
margin-left: 30px;
}

.navbarItemContainer {
background: #1e222a;
}

.navbarLink {
font-weight: bold;
color: #ffffff;
}

.navbarLink:hover {
color: #3abcf3;
}

#btnMenuContainer {
height: 100%;
display: none;
}

#btnMenu {
cursor: pointer;
}

.menuBtnBar {
width: 35px;
height: 5px;
margin: 6px 0;
background-color: #ffffff;
transition: 0.4s;
}

.activeMenuBtn #barTop {
transform: rotate(-45deg) translate(-9px, 6px);
}

.activeMenuBtn #barCenter {
opacity: 0;
}

.activeMenuBtn #barBottom {
transform: rotate(45deg) translate(-8px, -8px);
}

@media(max-width: 1200px) {
#navbar {
padding-left: 150px;
padding-right: 150px;
}
}

@media(max-width: 1100px) {
#navbar {
padding-left: 0;
padding-right: 0;
}
#navbarItems .navbarItemContainer:not(:first-child) {
margin-left: 0;
}
#navbarItems .navbarItemContainer:not(:last-child) {
border-bottom: 1px solid #676767;
}
#btnMenuContainer {
display: flex;
align-items: center;
}
#btnMenu {
margin-left: 20px;
}
#navbarItems {
margin-left: 0;
display: none;
}
#logoLink {
display: block;
}
.navbarItem {
width: 100%;
text-align: center;
padding: 30px 0;
}
#navbarItems.activeNavbar {
display: block;
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="navbar">
<div id="btnMenuContainer">
<div id="btnMenu">
<div id="barTop" class="menuBtnBar"></div>
<div id="barCenter" class="menuBtnBar"></div>
<div id="barBottom" class="menuBtnBar"></div>
</div>
</div>

<div id="navbarItems">
<div class="navbarItemContainer">
<div class="navbarItem">
<a id="logoLink" class="link navbarLink" href="#">
<img class="img" src="http://icons.iconarchive.com/icons/ph03nyx/super-mario/128/Hat-Mario-icon.png">
</a>
</div>
</div>

<div class="navbarItemContainer">
<div class="navbarItem">
<a class="link navbarLink" href="#sectionTwo">
Link 2
</a>
</div>
</div>

<div class="navbarItemContainer">
<div class="navbarItem">
<a class="link navbarLink" href="#sectionThree">
Link 3
</a>
</div>
</div>
</div>
</div>

如果您检查 Logo 容器,您会看到链接将在整个 div 容器宽度上触发。通常我只希望在 img 单击时触发链接。我该如何解决此问题?

最佳答案

您看到该行为是因为 display: block 的 CSS 设置为 #logoLink

#logoLink {
display: block;
}

$(document).ready(() => {
$("#btnMenu").click(() => {
toggleMenu();
});

$(".navbarLink").click(() => {
if ($("#navbarItems").hasClass("activeNavbar")) {
toggleMenu();
}
});
});

function toggleMenu() {
$("#navbarItems").toggleClass("activeNavbar");
toggleMenuBtn();
}

function toggleMenuBtn() {
$("#btnMenu").toggleClass("activeMenuBtn");
}
.link {
text-decoration: none;
}

body {
margin: 0;
}

#navbar {
height: 60px;
top: 0;
padding-left: 200px;
padding-right: 200px;
position: sticky;
background: #1e222a;
}

#navbarItems {
height: 100%;
display: flex;
align-items: center;
}

#logoLink {
display: inline-block;
align-items: center;
}

#navbarItems .navbarItemContainer:not(:first-child) {
margin-left: 30px;
}

.navbarItemContainer {
background: #1e222a;
}

.navbarLink {
font-weight: bold;
color: #ffffff;
}

.navbarLink:hover {
color: #3abcf3;
}

#btnMenuContainer {
height: 100%;
display: none;
}

#btnMenu {
cursor: pointer;
}

.menuBtnBar {
width: 35px;
height: 5px;
margin: 6px 0;
background-color: #ffffff;
transition: 0.4s;
}

.activeMenuBtn #barTop {
transform: rotate(-45deg) translate(-9px, 6px);
}

.activeMenuBtn #barCenter {
opacity: 0;
}

.activeMenuBtn #barBottom {
transform: rotate(45deg) translate(-8px, -8px);
}

@media(max-width: 1200px) {
#navbar {
padding-left: 150px;
padding-right: 150px;
}
}

@media(max-width: 1100px) {
#navbar {
padding-left: 0;
padding-right: 0;
}
#navbarItems .navbarItemContainer:not(:first-child) {
margin-left: 0;
}
#navbarItems .navbarItemContainer:not(:last-child) {
border-bottom: 1px solid #676767;
}
#btnMenuContainer {
display: flex;
align-items: center;
}
#btnMenu {
margin-left: 20px;
}
#navbarItems {
margin-left: 0;
display: none;
}
#logoLink {
display: inline-block;
}
.navbarItem {
width: 100%;
text-align: center;
padding: 30px 0;
}
#navbarItems.activeNavbar {
display: block;
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="navbar">
<div id="btnMenuContainer">
<div id="btnMenu">
<div id="barTop" class="menuBtnBar"></div>
<div id="barCenter" class="menuBtnBar"></div>
<div id="barBottom" class="menuBtnBar"></div>
</div>
</div>

<div id="navbarItems">
<div class="navbarItemContainer">
<div class="navbarItem">
<a id="logoLink" class="link navbarLink" href="#">
<img class="img" src="http://icons.iconarchive.com/icons/ph03nyx/super-mario/128/Hat-Mario-icon.png">
</a>
</div>
</div>

<div class="navbarItemContainer">
<div class="navbarItem">
<a class="link navbarLink" href="#sectionTwo">
Link 2
</a>
</div>
</div>

<div class="navbarItemContainer">
<div class="navbarItem">
<a class="link navbarLink" href="#sectionThree">
Link 3
</a>
</div>
</div>
</div>
</div>

将其更改为 display: inline-block,您将看到更改,即只有图像可点击。 Here's阅读有关 display 属性的一个很好的引用。

关于javascript - 图像在全宽上扩展了链接触发范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51936679/

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