[英]Apply a selector when element is in the viewport
Imagine I'm using the following code on my website 想象一下,我在我的网站上使用以下代码
<div class="wrapper">
<nav class="main-nav">
Hello, I am navigation
</nav>
</div>
When an object with the selector "wrapper"
is within the viewport, I would like to reference an additional selector in the <nav>
element. 当具有选择器"wrapper"
的对象在视口内时,我想在<nav>
元素中引用另一个选择器。
Therefore 因此
When an element with "wrapper" is in the viewport, the HTML is 当带有“包装器”的元素在视口中时,HTML就是
<div class="wrapper">
<nav class="main-nav in-view">
Hello, I am navigation
</nav>
</div>
Otherwise the code is 否则代码是
<div class="wrapper">
<nav class="main-nav">
Hello, I am navigation
</nav>
</div>
Have you tried using a jQuery
function like: 您是否尝试过使用jQuery
函数:
if ($(".wrapper").length) { /* Checks if wrapper exists */ $(".main-nav").addClass("in-view"); }
.in-view { color: red; }
<div class="wrapper"> <nav class="main-nav"> Hello, I am navigation </nav> </div> <!-- jQuery --> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.