gpt4 book ai didi

javascript - 如何使用 'hover' 选择器和 ':has()' 选择比子元素高几层的父元素?

转载 作者:行者123 更新时间:2023-11-30 12:02:03 25 4
gpt4 key购买 nike

我意识到这可能是不可能的,所以我也愿意为了功能而修改代码。

这是我的 CSS 和 HTML:

* {
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
box-sizing: border-box;
font-family: 'Raleway Webfont';
}

body {
margin: 0;
padding: 0;
height: 4000px;
font-size: 16px;
line-height: 1.5;
}

ul, li, h1, h2, p, a, .logo {
font-family: 'Raleway Webfont';
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: black;
font-weight: 200;
margin: 0;
padding: 0;
}

header {
position: fixed;
width: 100%;
padding-top: 0px;
background: #e5e5e5;
transition:.25s;
z-index: 100;
border-bottom-style: solid;
border-width: 1px;
box-shadow: 0 0 15px 0 rgba(0,0,0,0.9);
}

.inner {
width: 800px;
position: relative;
margin: 0 auto;
}

nav {
overflow: hidden;
}

nav ul {
float: right;
}

nav li {
display: inline;
float: left;
list-style: none;
}

nav a {
display: inline-block;
color: #fff;
text-decoration: none;
padding: 25px 15px;
transition:.25s;
}

nav a:hover {
margin: -8px 0 0 0;
}

nav ul li:first-child a {
background:#b7b7b7 !important;
}
nav ul li:nth-child(2) a {
background:#a0a0a0 !important;
}
nav ul li:nth-child(3) a {
background:#898989 !important;
}
nav ul li:nth-child(4) a {
background:#727272 !important;
}
nav ul li:nth-child(5) a {
background:#5b5b5b !important;
}

nav ul li:first-child:hover header {
background: #b7b7b7;
}
nav ul li:nth-child(2):hover header {
background: #a0a0a0;
}
nav ul li:nth-child(3):hover header {
background: #898989;
}
nav ul li:nth-child(4):hover header {
background: #727272;
}
nav ul li:nth-child(5):hover header {
background: #5b5b5b;
}

.logo {
float: left;
color: #000;
font-size: 30px;
font-weight: 300;
text-decoration: none;
line-height: 70px;
}

section .inner {
padding-top: 125px;
}

p {
padding-top: 25px;
font-weight: 400;
}

h1 {
text-align: center;
}
<header>
<div class="inner">
<div>
<a class="logo" href="Home.html">W </a>
</div>
<nav role='navigation'>
<ul>
<li><a href="me.html">ME</a></li>
<li><a href="cv.html">CV</a></li>
<li><a href="rd.html">RD</a></li>
<li><a href="av.html">AV</a></li>
<li><a href="rr.html">RR</a></li>
</ul>
</nav>
</div>
</header>
<section>
<div class="inner">
<h1>about me</h1>
<p><span> A work in progress.</span>
</p>
</div>
</section>

我真的认为我可以通过使用 :hover 来选择父元素。选择器,但是,显然我错了。我已经访问过"Is there a CSS parent selector?" ,以及引用此问题的大多数其他查询。然而,我的特殊需求仍然让我感到困惑。

本质上,是否可以操纵 background:<header>利用就地 -child安排,:hover选择器(我想要的样式),以及一些将该选择器扩展回 <header> 的方法? :has()方法似乎是一个可能的候选人,但我无法弄清楚如何在这里为我的生活实现它。如果在保持美感的同时可以/必须改变任何结构,那么我当然也愿意实现它。提前致谢!

最佳答案

我不知道使用纯 CSS 的方法,但是如果你想更改 <header>背景基于悬停在导航中的哪个链接,您可以在 jQuery 中使用它(确保在加载时运行,或将其放在正文末尾 </body> 之前):

$('[role=navigation] a').hover(function(){
$(this).closest('header').css('background-color', $(this).css('background-color'));
},function(){
$(this).closest('header').css('background-color', '#e5e5e5');
});

https://jsfiddle.net/5d1tvnwh/

关于javascript - 如何使用 'hover' 选择器和 ':has()' 选择比子元素高几层的父元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36413451/

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