【问题标题】:Scrollspy Implementation With MVC 4使用 MVC 4 实现 Scrollspy
【发布时间】:2023-04-02 15:10:01
【问题描述】:

我有一个基于 MVC 4 框架和 Bootstrap(twitter) 的 Web 应用程序,我想让我的一个页面成为带有 scrollspy 的单页导航:

我在整个网站的顶部都有一个固定的导航栏,我希望在网站的一个页面中添加一个带有滚动间谍的侧栏(“nav nav-pills nav-stacked pull-left”)。

col-md-3 = 左侧将存储我的侧边栏

col-md-9 = 右侧将存储我的上下文

我尝试了不同的东西,但似乎没有任何效果,我注意到的一件事是 我需要添加到正文标签:

<body data-spy="scroll" data-target="#side-nav">

但在 MVC 中,body 标签在“_layout”上,我不想触碰这个共享的局部视图。

我能做什么?也许有人在 MVC 中实现了这个功能,可以给我一个小例子吗?

这是我的代码:

@{
    ViewBag.Title = "Designer";
}

    <div class="row">


    <div class="col-md-3">
        @* Index *@
        <div class="nav-pills pull-left affix navspy">
            <div id="side-nav">
                <ul class="nav">
                    <li><a href="#general">General</a></li>
                    <li><a href="#words">Words</a></li>
                    <li><a href="#filetype">File Type</a></li>
                    <li><a href="#routes">Routes</a></li>
                </ul>
            </div>
        </div>
    </div>

    <div class="col-md-9">

    <div id="general">
        @* Section Gereral *@
        <h2 class="page-header">Gereral</h2>
        <p>Lorem ipsum dolor sit amet, vel cu fugit vitae electram, </p>
    </div>

    <div class="active" id="words">
        @* Section Words *@
        <h2 class="page-header">Words</h2>
        <p>Lorem ipsum dolor sit amet, vel cu fugit vitae electram, </p>
    </div>

    <div id="filetype">
        @* Section File Type *@
        <h2 class="page-header">File Type</h2>
        <p>Lorem ipsum dolor sit amet, vel cu fugit vitae electram, </p>
    </div>

    <div id="routes">
        @* Section Routes *@
        <h2 class="page-header">Routes</h2>
        <p>Lorem ipsum dolor sit amet, vel cu fugit vitae electram, </p>
    </div>

</div>

</div>

<script>
    $(document).ready(function () {
        $('body').attr("data-spy", "scroll");
        $('body').attr("data-target", "#side-nav");

    });
</script>

我在这里缺少什么?

【问题讨论】:

    标签: html asp.net-mvc twitter-bootstrap scrollspy


    【解决方案1】:

    您可以使用 javascript 将属性动态添加到 &lt;body&gt; 标记。

    将此脚本添加到滚动间谍所在的页面。当它被加载到_Layout 中时,属性将被添加到&lt;body&gt; 标记中。

    yourPage.cshtml

    @{
        ViewBag.Title = "Designer";
    }
    
    @section scripts
    {
        <script>
                $(document).ready(function(){        
                    $( 'body' ).attr("data-spy","scroll");
                    $( 'body' ).attr("data-target","#side-nav");  
    
                });
        </script>
    }
    
    [...]
    

    共享\_Layout.cshtml

    <!DOCTYPE html>
    <html>
    <head>
    
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>@ViewBag.Title</title>
        @Styles.Render("~/Content/css")
        @Scripts.Render("~/bundles/modernizr")
    
    </head>
    <body>
    
        [...]
    
            @RenderBody()       
    
        [...]
    
        @Scripts.Render("~/bundles/jquery")
        @Scripts.Render("~/bundles/bootstrap")
        @RenderSection("scripts", false)
    </body>
    </html>
    

    【讨论】:

    • 我将您的脚本添加到了我的页面,并且它保持不变。我用我的代码更新了我的帖子,也许您注意到我的问题?
    • 在脚本部分的文档顶部声明脚本。查看我的编辑。
    猜你喜欢
    • 2019-01-15
    • 1970-01-01
    • 2012-11-16
    • 1970-01-01
    • 2018-06-07
    • 2014-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多