简体   繁体   English

Bootstrap导航栏固定顶部隐藏#content链接

[英]Bootstrap navbar-fixed-top hides #content links

Im a software development student and for my web development class I am creating a page. 我是一名软件开发专业的学生,​​在我的Web开发课程中,我正在创建一个页面。 I am using Bootstrap, I have a navbar-fixed-top and the body is a table-striped table where every row has a <a href = "#section" >Section</a>" link. 我正在使用Bootstrap,我有一个navbar-fixed-top ,主体是一个table-striped表,其中每行都有一个<a href = "#section" >Section</a>"链接。

The thing is that it is a very long list so I added a jQuery UI autocomplete and a button, so when the user hits the button (helped with the autocomplete) it redirects to the corresponding #section row. 事实是,这是一个非常长的列表,因此我添加了一个jQuery UI自动完成功能和一个按钮,因此当用户单击该按钮(自动完成功能有帮助)时,它将重定向到相应的#section行。

the autocomplete and the button work just fine, but when the page redirects happens the row that I want to see gets hidden behind the navbar. 自动完成和按钮正常工作,但是当页面重定向发生时,我要查看的行隐藏在导航栏后面。

I read a little into this and found that the quick and dirty way to do this is via css with: 我对此进行了一些阅读,发现执行此操作的快速而肮脏的方法是通过CSS与:

padding-top: 65px; 

Buuuuuuut I dont want to do this because it will result in a incredibly long table. Buuuuuuut我不想这样做,因为它将导致一个难以置信的长表。

Sorry if I didn't made myself clear, here is some code just in case: 抱歉,如果我没有明确说明,下面是一些代码,以防万一:

Example html 范例html

<script>

    //code for the redirects
    (function ($) {
        $.fn.goTo = function () {
            $('html, body').animate({
                scrollTop: $(this).offset().top + 'px'
            }, 'fast');
            return this; // for chaining...

        }
    })(jQuery);

</script>
<button class="btn btn-info" onclick="$('#' + document.getElementById('tags').value).goTo();" >Search</button>

<!-- I dont know if theres a way to optimize this  search code but right now its working fine-->  

<div class="table-responsive">
    <table class="table table-striped table-hover table-condensed">
        <thead>
            <tr>
                <th>Col 1</th>
                <th>Col 2</th>
                <th>Col 3</th>
            </tr>
        </thead>
        <tbody>
            <tr id = "Section1">
                <th>Col 1</th>
                <th>Col 2</th>
                <th>Col 3</th>
            </tr>
            <tr id = "Section2">
                <th>Col 1</th>
                <th>Col 2</th>
                <th>Col 3</th>
            </tr>
            <!-- code continues similarly for nearly 1000 rows -->
        </tbody>
    </table>
</div>

Simply add the offset of the navbar height into the scrollTop equation. 只需将导航栏高度的偏移量添加到scrollTop公式中即可。

//code for the redirects
(function ($) {
    $.fn.goTo = function () {

        var offset = $(this).offset().top - 65;

        $('html, body').animate({
            scrollTop: offset + 'px'
        }, 'fast');
        return this; // for chaining...
    }
})(jQuery);

You could even take it one step further and dynamically grab the height of the navbar too. 您甚至可以更进一步,也可以动态地获取导航栏的高度。

var navHeight = $('.nav').height();

A quick and better solution for your code since u know bootstrap jquery etc, use DataTable plugin, applying plugin u need not worry about the search,filtering,pagination and much more. 由于您知道引导jquery等,使用DataTable插件,应用插件,因此您的代码是一种快速,更好的解决方案,您无需担心搜索,过滤,分页等等。 Here is the reference link; 这是参考链接; https://datatables.net/ https://datatables.net/

    $(document).ready(function(){
    $('.table-responsive').DataTable({// Use id/ Class of html table to apply plugin
    // u can do stuffs here
      });
     });

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

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