简体   繁体   English

修复了导航栏隐藏网页内容且JS无法正常工作的问题

[英]Fixed navbar hides web content and JS is not working

This is what happen when I press the button "Contacto" 这是当我按下“ Contacto”按钮时发生的情况

在此处输入图片说明

and should be like this 并且应该像这样

在此处输入图片说明

and now I'm using this javascript code 现在我正在使用此javascript代码

    $(function(){
    $('a#boton-contacto').on('click',function(e){
        e.preventDefault();
        var strAncla = $(this).attr('href');

        $('body,html').stop(true ,true).animate({
            scrollTop: $(strAncla).offset().top - $('nav').height()
        }, 500);

    });
});

but it made the button stop working, I want to know why, what is wrong? 但是它使按钮停止工作,我想知道为什么,怎么了?

you can go to my site and try it http://genebi.net I hope you can help me, thanks. 您可以转到我的网站并尝试http://genebi.net ,希望您能为我提供帮助,谢谢。

This code from you jQuery: jQuery的以下代码:

var strAncla = $(this).attr('href'); is setting strAncla to be " http://genebi.net/#contacto " strAncla设置为“ http://genebi.net/#contacto

And since " http://genebi.net/#contacto " is not a valid selector, there is a javascript error that prevents the code from running. 并且由于“ http://genebi.net/#contacto ”不是有效的选择器,因此出现了JavaScript错误,导致代码无法运行。

To solve this, either: 要解决此问题,请执行以下任一操作:

  1. Change your url for the element from: <a id="boton-contacto" href="http://genebi.net/#contacto">CONTACTO</a> 将元素的网址更改为: <a id="boton-contacto" href="http://genebi.net/#contacto">CONTACTO</a>

to <a id="boton-contacto" href="http://genebi.net/#contacto">CONTACTO</a> <a id="boton-contacto" href="http://genebi.net/#contacto">CONTACTO</a>

or: 要么:

2: You could use a data attribute in your link: 2:您可以在链接中使用数据属性:

<a id="boton-contacto" href="http://genebi.net/#contacto" data-element="#contacto">CONTACTO</a>

and alter your jQuery as follows: 并按如下所示更改您的jQuery:

var strAncla = $(this).attr('data-element');

And it will work as you desire. 它会按您的意愿工作。

position: fixed; removes it from the normal flow of the document, and means that it doesn't occupy space. 将其从文档的常规流程中删除,这意味着它不占用空间。 You can fix this without Javascript (and it'll be much less janky). 您可以在不使用Javascript的情况下解决此问题(它会减少很多麻烦)。 It's a little awkward to work around this, but you can do it by having a blank div underneath it to fill up the space. 解决这个问题有点尴尬,但是您可以通过在其下面留一个空白div来填充空间来做到这一点。 Let's call it "header-spacer". 我们称它为“ header-spacer”。

<div class="header">
  ...
</div>
<div class="header-spacer"></div>

And some CSS: 还有一些CSS:

.header-spacer {
    height: 70px;
}

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

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