所以,我有一个带有手风琴菜单的网站,该网站使用锚标签来浏览网站。
没有其他页面,只有一个页面,其中所有内容都由锚点分隔。
我正在寻找一个在滚动到特定锚定标签(嵌套)或点击锚定链接时显示的面包屑手风琴菜单(嵌套)。
如何在jQuery中创建这样的东西?
我正在考虑在名称标签中添加类似的东西,以便在jquery中使用viewport脚本中的isvisible生成面包屑。然而,当我考虑它时,当用户复制并粘贴带有锚点的URL并且它从该页面开始时会发生什么,然后它不会滚过其他锚点和breadcrumb无法正常显示。
我该如何创建呢?
这是否意味着我必须以某种方式在javascript / jQuery中创建层次结构?
以下jsfiddle显示了它应该如何工作的想法:https://jsfiddle.net/6dnxcoet/3/
<nav id="menu">
<ul>
<li><a href="#home">Home</a></li>
<li><a href="#information">Information</a>
<ul>
<lI><a href="#contact">Contact</a></lI>
<li><a href="#about-us">About Us</a></li>
<li><a href="#news">News</a></li>
</ul>
</li>
</ul>
</nav>
<nav id="breadcrumb">
<ul class="breadcrumb clear-initial-trail">
<li><div><a href="#home">Home</a></div></li>
</ul>
</nav>
<main>
<div class="page">
<h1><a name="home">Home</a></h1>
<p>Some content</p>
</div>
<h2><a name="information">Information</a></h2>
<div class="page">
<h3><a name="contact">Contact</a></h3>
<p>Some content</p>
</div>
<div class="page">
<h3><a name="about-us">About Us</a></h3>
<p>Some content</p>
</div>
<div class="page">
<h3><a name="news">News</a></h3>
<p>Some content</p>
</div>
</main>
.page {
height: 800px;
background-color:red;
width:400px;
margin-left:auto;
margin-right:auto;
}
main {
text-align:center;
}
#menu {
position:fixed;
}
#breadcrumb {
position:fixed;
right:200px;
top:0px;
}
.breadcrumb{
display: block;
list-style: none;
margin: 0;
padding: 0;
}
.breadcrumb > li {
display: inline-block;
list-style: none;
margin-right: -15px;
}
.breadcrumb.clear-initial-trail > li:first-child > div {
margin-left: 0;
padding-left: 10px;
border-top-left-radius: 2px;
border-bottom-left-radius: 2px;
}
.breadcrumb.clear-initial-trail > li:first-child > div::before {
display: none;
}
.breadcrumb > li > div{
display: inline-block;
background-color: #999;
margin: 2px;
padding: 0 8px 0 8px;
margin-right: 15px;
margin-left: 15px;
color: #fff;
position: relative;
height: 30px;
line-height: 30px;
}
.breadcrumb > li > div::after{
display: block;
position: absolute;
top:0;
left: 100%;
content: '';
border: 15px solid transparent;
border-left-width: 15px;
border-right-width: 0px;
z-index: 10;
border-left-color: #999;
}
.breadcrumb > li > div::before{
display: block;
position: absolute;
top:0;
right: 100%;
content: '';
background-color: #999;
border: 15px solid transparent;
border-left-width: 15px;
border-right-width: 0px;
border-left-color: #fff;
}
我希望在滚动到锚定标记时以及在导航中单击链接时更新面包屑。家应该永远在那里,信息应该是第二个,其他信息应该是第三个。
答案 0 :(得分:0)
好的,所以我试图让它工作,到目前为止似乎还可以,但我想添加一些动画。我想我应该为它添加一个隐藏类,然后添加.show()。animate()?无论如何这里是jsfiddle的代码:
(function($) {
/**
* Copyright 2012, Digital Fusion
* Licensed under the MIT license.
* http://teamdf.com/jquery-plugins/license/
*
* @author Sam Sehnert
* @desc A small plugin that checks whether elements are within
* the user visible viewport of a web browser.
* only accounts for vertical position, not horizontal.
*/
$.fn.visible = function(partial) {
var $t = $(this),
$w = $(window),
viewTop = $w.scrollTop(),
viewBottom = viewTop + $w.height(),
_top = $t.offset().top,
_bottom = _top + $t.height(),
compareTop = partial === true ? _bottom : _top,
compareBottom = partial === true ? _top : _bottom;
return ((compareBottom <= viewBottom) && (compareTop >= viewTop));
};
})(jQuery);
function updatecrumb(parent, page) {
var breadcrumb = $('.breadcrumb');
var current = '';
var active = '';
if (parent) {
if (parent == 'empty') {
if (breadcrumb.find('.currentpage').length) {
breadcrumb.find('.currentpage').replaceWith('');
}
}
else {
current += "<li class=\"currentpage\"><div><a href=\"#" + parent.children(':first-child')[0].name + "\"> " + parent.children(':first-child').children().text() + "</a></div></li>";
if (breadcrumb.find('.currentpage').length)
{
breadcrumb.find('.currentpage').replaceWith(current);
}
else {
breadcrumb.append(current);
}
}
}
if (page) {
if (page == 'empty') {
if (breadcrumb.find('.active').length) {
breadcrumb.find('.active').replaceWith('');
}
}
else {
if (page.attr('id') != "home") {
active += "<li class=\"active\"><div><a href=\"#" + page.children(':first-child')[0].name + "\"> " + page.children(':first-child').children().text() + "</a></div></li>";
if (breadcrumb.find('.active').length)
{
breadcrumb.find('.active').replaceWith(active);
}
else {
breadcrumb.append(active);
}
}
}
}
}
$(window).scroll(function() {
var page,
parent,
parentcount = 0,
pagecount = 0;
$('.parent-page').each(function() {
if ($(this).visible(true)) {
parent = $(this);
parentcount += 1;
updatecrumb(parent);
}
});
$('.page').each(function() {
if ($(this).visible(true)) {
page = $(this);
if (page.attr('id') != "home") {
pagecount +=1;
}
updatecrumb(null, page);
}
})
if (parentcount == 0) {
updatecrumb('empty');
}
if (pagecount == 0) {
updatecrumb(null, 'empty');
}
});
jsfiddle在这里:https://jsfiddle.net/6dnxcoet/4/ 当然,如果有人知道如何优化,改进代码,那么还有改进的余地。请......不要退缩。