简体   繁体   English

jQuery 如果元素位于顶部,则激活 href

[英]jQuery activate href if element on top

I have a simple menu, everytime i click an element the others fadeOut and the clicked one goes up if it's not the first element, and after that i activate the href of the link.我有一个简单的菜单,每次我单击一个元素时,其他元素会淡出,如果它不是第一个元素,则单击的元素会上升,然后我激活链接的 href。

To activate the link i need to make sure that the element is in the top of the page, after the addClass slideUpSm the clicked item gets new position top but i think the position top needs to be refreshed because the addClass slideUpSm gets the css top 0要激活链接,我需要确保该元素位于页面顶部,在 addClass slideUpSm之后,单击的项目会获得新的 position 顶部,但我认为 position 顶部需要刷新,因为 addClass slideUpSm获取 ZC757FE07FD492A8BE0EA6A760D683D6EZ 顶部

setTimeout(function() {$('.main-nav li').addClass('slideUpSm')}, 1200);

After that, i can activate the window location, i have tried the hasClass methode and also tried to check if the new position top equals zero, here is my attempt https://jsfiddle.net/n37kgv4r/之后,我可以激活 window 位置,我尝试了 hasClass 方法,还尝试检查新的 position 顶部是否等于零,这是我的尝试Z5E056C500A1C4B6A7110B50D807BADEvZ4:///

setTimeout(function() {window.location = href}, 100);

If you just want to hide the other links and navigate to only the clicked node, we dont need to use the css top.如果您只想隐藏其他链接并仅导航到单击的节点,我们不需要使用 css 顶部。 We can achieve this with simple jquery code.我们可以通过简单的 jquery 代码来实现这一点。

 $(document).ready(function(){ $('.main-nav li a').click(function(e){ $(this).parent().addClass('show'); $('.main-nav li').not('.show').slideUp(1200); var obj = $(this); setTimeout(function(){ //remove the alert alert("Navigating to "+ obj.attr('href')); window.location.href=obj.attr('href');},2000) return false; }); });
 a { text-decoration: none; color: black; }.main-nav { position: relative; }.main-nav li { position: sticky; display: flex; font-size: 60px; line-height: 43px; letter-spacing: -3px; margin-bottom: 11px; transition: all 2s; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script> <ul class="main-nav"> <li><a href="page1.html">Page 1</a></li> <li><a href="page2.html">Page 2</a></li> <li><a href="page3.html">Page 3</a></li> </ul>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM