感谢您的帮助,请原谅我是个菜鸟。答案很可能非常简单;
我正在尝试为我的个人网站“EthanGreenspan.com”使用名为“ScrollNav.js”的 JQuery 插件。
我想使用这个插件制作一个简单的左 Pane ,其中包含交互式目录,让用户单击部分即可滚动到它们。正如您在 JSFiddle(底部链接)中看到的,左侧 Pane 中的 功能与 ScrollNav 站点不同。
这是我希望用作目录的 HTML。
<div class="leftpane" class="main" role="main">
<nav class="scroll-nav" role="navigation">
<span class="scroll-nav-heading">Sections</span>
<ol class="scroll-nav-list">
<li class="scroll-nav-item active top">
<a href="#jumpNav-0" class="scroll-nav-link">Top</a></li>
<li class="scroll-nav-item active Current Work">
<a href="#jumpNav-1" class="scroll-nav-link"></a>Current Work</li>
<li>Photography</li>
<li>Consulting</li>
<li>Blog</li>
<li>Contact Me</li>
</ol>
这是 Jquery(我将其内联到 JSFiddle)
<script>
$('.body').scrollNav({
sections: 'h3',
titletext: 'Scroll To',
fixedmargin: 40,
animation: true,
})
</script>
最后,这是 ScrollNav JQuery 插件;
(function(e){e.fn.scrollNav=function(t){e("body").addClass("sn-loading");var n={sections:"h3",titleText:"Scroll To",fixedMargin:40,animated:!0,speed:500,showHeadline:!0,showTopLink:!0,location:"insertBefore"};e.extend(n,t);var r=[],i=this,s=i.find(n.sections),o=e("<nav />",{"class":"scroll-nav",role:"navigation"}),u=function(){if(n.showTopLink===!1)return;var e=i.attr("id"),t=i.offset().top;if(e)r.push({id:e,offset:t,text:"Top"});else{i.attr("id","jumpNav-0");r.push({id:"jumpNav-0",offset:t,text:"Top"})}},a=function(){s.each(function(t){var n="jumpNav-"+(t+1),i=e(this).offset().top,s=e(this).text();e(this).attr("id",n);r.push({id:n,offset:i,text:s})})},f=function(){var t=e("<span />",{"class":"scroll-nav-heading",text:n.titleText}),i=e("<ol />",{"class":"scroll-nav-list"});e.each(r,function(t){var n=t===0?e("<li />",{"class":"scroll-nav-item active"}):e("<li />",{"class":"scroll-nav-item"}),r=e("<a />",{href:"#"+this.id,"class":"scroll-nav-link",text:this.text});i.append(n.append(r))});n.showHeadline===!0?o.append(t).append(i):o.append(i)},l=function(){var t=o.offset().top;e(window).resize(function(){e.each(r,function(){this.offset=e("#"+this.id).offset().top})});e(window).scroll(function(){var i=e(window).scrollTop(),s=e(window).height()*.5;i>t-n.fixedMargin?o.addClass("fixed"):o.removeClass("fixed");e.each(r,function(){if(this.offset>i-n.fixedMargin&&this.offset<i+s){o.find("li").removeClass("active");o.find('a[href="#'+this.id+'"]').parents("li").addClass("active")}})})};if(i.length!==0){u();a();f()}i.length!==0&&s.length!==0?o[n.location](i):i.length===0?console.log("Build failed, scrollNav could not find '"+i.selector+"'"):s.length===0&&console.log("Build failed, scrollNav could not find any '"+n.sections+"'s inside of '"+i.selector+"'");l();n.animated===!0&&e(".scroll-nav-link").click(function(){var t=e(this).attr("href"),r=e(t).offset().top;e("html:not(:animated),body:not(:animated)").animate({scrollTop:r-40},n.speed);return!1});e("body").removeClass("sn-loading").addClass("sn-active")}})(jQuery);
我了解 JQuery 的基础知识,但确实需要您的帮助!
这里是 JSFiddle 的链接与我的 HTML/CSS/JS。
请并谢谢您!
最佳答案
我是 Jimmy,scrollNav.js jQuery 插件的作者。您遇到的问题是由于您自己创建导航项造成的。使用scrollNav,你不需要这样做。该插件会扫描您的内容并自行创建导航项。
我 fork 了你的 fiddle 并将标记重新设计为scrollNav正在寻找的here 。我上周刚刚发布了该插件的 v2,因此您需要确保在使用我的示例代码之前更新您自己的源代码。最新版本允许您选择插入点以及每个部分的包装元素,您会看到我已经为您定义了这些。
$('.post-article').scrollNav({
sections: 'h3',
sectionElem: 'div',
insertTarget: '.leftpane',
insertLocation: 'appendTo'
});
请务必阅读change log查看新版本中还有哪些更改。
关于jquery - Scrollnav.js 侧边栏无法激活,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18153646/