我是javascript语言的新手,而且我被我的代码卡住了,对如何解决我的问题没有任何想法,而且如果我不能解释问题,我提前表示歉意。
您可以找到我的app.js文件,我创建了一个页面部分的动态列表,但我尝试了很多方法,并观看了很多教程,介绍如何将一个活动类添加到所需列表中,它的href
具有与视口中显示的部分相同的数据导航
null
// select all sections
const sections = document.querySelectorAll('section');
// select the navbar id
const navBar = document.getElementById('navbar__list');
// create a document fragment to hold all the lists
const frag = document.createDocumentFragment();
/**
* End Global Variables
* Start Helper Functions
*
*/
/**
* End Helper Functions
* Begin Main Functions
*
*/
// build the nav
for (let section of sections) {
const sectionId = section.getAttribute('id');
const sectionTitle = section.getAttribute('data-nav');
const list = document.createElement('li');
const links = document.createElement('a');
// construct the li's
links.setAttribute('href', `#${sectionId}`);
links.innerText = sectionTitle;
list.appendChild(links);
links.classList = 'menu__link';
frag.appendChild(list);
// add event listener for smooth scroll
links.addEventListener('click', function(event) {
event.preventDefault();
window.scrollTo({
top: section.offsetTop - 60,
behavior: 'smooth'
});
});
};
navBar.appendChild(frag);
// select the a tags inside navbar
const allLinks = navBar.querySelectorAll('a');
/* // Add class 'active' to section when near top of viewport */
// Get the Top of the section
function sectionView(section) {
return section.getBoundingClientRect().top;
}
// Remove Active Class of all sections
function removeActiveClass(section) {
return section.classList.remove('your-active-class');
}
// Add Active Class to the required section
function addActiveClass(is_inview, section) {
if (is_inview) {
section.classList.add('your-active-class');
};
}
// combine functions //
const activeSection = () => {
sections.forEach(section => {
const itemView = sectionView(section);
viewport = () => itemView > -200 && itemView <= 400;
removeActiveClass(section);
addActiveClass(viewport(), section);
});
};
// add eventlistener to window
window.addEventListener('scroll', activeSection);
<link href="https://fonts.googleapis.com/css?family=Fira+Sans:900|Merriweather&display=swap" rel="stylesheet">
<!-- HTML Follows BEM naming conventions
IDs are only used for sections to connect menu achors to sections -->
<header class="page__header">
<nav class="navbar__menu">
<!-- Navigation starts as empty UL that will be populated with JS -->
<ul id="navbar__list"></ul>
</nav>
</header>
<main>
<header class="main__hero">
<h1>Landing Page </h1>
</header>
<!-- Each Section has an ID (used for the anchor) and
a data attribute that will populate the li node.
Adding more sections will automatically populate nav.
The first section is set to active class by default -->
<section id="section1" data-nav="Section 1" class="your-active-class">
<div class="landing__container">
<h2>Section 1</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi fermentum metus faucibus lectus pharetra dapibus. Suspendisse potenti. Aenean aliquam elementum mi, ac euismod augue. Donec eget lacinia ex. Phasellus imperdiet porta orci eget mollis.
Sed convallis sollicitudin mauris ac tincidunt. Donec bibendum, nulla eget bibendum consectetur, sem nisi aliquam leo, ut pulvinar quam nunc eu augue. Pellentesque maximus imperdiet elit a pharetra. Duis lectus mi, aliquam in mi quis, aliquam
porttitor lacus. Morbi a tincidunt felis. Sed leo nunc, pharetra et elementum non, faucibus vitae elit. Integer nec libero venenatis libero ultricies molestie semper in tellus. Sed congue et odio sed euismod.</p>
<p>Aliquam a convallis justo. Vivamus venenatis, erat eget pulvinar gravida, ipsum lacus aliquet velit, vel luctus diam ipsum a diam. Cras eu tincidunt arcu, vitae rhoncus purus. Vestibulum fermentum consectetur porttitor. Suspendisse imperdiet porttitor
tortor, eget elementum tortor mollis non.</p>
</div>
</section>
<section id="section2" data-nav="Section 2">
<div class="landing__container">
<h2>Section 2</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi fermentum metus faucibus lectus pharetra dapibus. Suspendisse potenti. Aenean aliquam elementum mi, ac euismod augue. Donec eget lacinia ex. Phasellus imperdiet porta orci eget mollis.
Sed convallis sollicitudin mauris ac tincidunt. Donec bibendum, nulla eget bibendum consectetur, sem nisi aliquam leo, ut pulvinar quam nunc eu augue. Pellentesque maximus imperdiet elit a pharetra. Duis lectus mi, aliquam in mi quis, aliquam
porttitor lacus. Morbi a tincidunt felis. Sed leo nunc, pharetra et elementum non, faucibus vitae elit. Integer nec libero venenatis libero ultricies molestie semper in tellus. Sed congue et odio sed euismod.</p>
<p>Aliquam a convallis justo. Vivamus venenatis, erat eget pulvinar gravida, ipsum lacus aliquet velit, vel luctus diam ipsum a diam. Cras eu tincidunt arcu, vitae rhoncus purus. Vestibulum fermentum consectetur porttitor. Suspendisse imperdiet porttitor
tortor, eget elementum tortor mollis non.</p>
</div>
</section>
<section id="section3" data-nav="Section 3">
<div class="landing__container">
<h2>Section 3</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi fermentum metus faucibus lectus pharetra dapibus. Suspendisse potenti. Aenean aliquam elementum mi, ac euismod augue. Donec eget lacinia ex. Phasellus imperdiet porta orci eget mollis.
Sed convallis sollicitudin mauris ac tincidunt. Donec bibendum, nulla eget bibendum consectetur, sem nisi aliquam leo, ut pulvinar quam nunc eu augue. Pellentesque maximus imperdiet elit a pharetra. Duis lectus mi, aliquam in mi quis, aliquam
porttitor lacus. Morbi a tincidunt felis. Sed leo nunc, pharetra et elementum non, faucibus vitae elit. Integer nec libero venenatis libero ultricies molestie semper in tellus. Sed congue et odio sed euismod.</p>
<p>Aliquam a convallis justo. Vivamus venenatis, erat eget pulvinar gravida, ipsum lacus aliquet velit, vel luctus diam ipsum a diam. Cras eu tincidunt arcu, vitae rhoncus purus. Vestibulum fermentum consectetur porttitor. Suspendisse imperdiet porttitor
tortor, eget elementum tortor mollis non.</p>
</div>
</section>
<!-- Add new sections -->
<section id="section4" data-nav="Section 4">
<div class="landing__container">
<h2>Section 4</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi fermentum metus faucibus lectus pharetra dapibus. Suspendisse potenti. Aenean aliquam elementum mi, ac euismod augue. Donec eget lacinia ex. Phasellus imperdiet porta orci eget mollis.
Sed convallis sollicitudin mauris ac tincidunt. Donec bibendum, nulla eget bibendum consectetur, sem nisi aliquam leo, ut pulvinar quam nunc eu augue. Pellentesque maximus imperdiet elit a pharetra. Duis lectus mi, aliquam in mi quis, aliquam
porttitor lacus. Morbi a tincidunt felis. Sed leo nunc, pharetra et elementum non, faucibus vitae elit. Integer nec libero venenatis libero ultricies molestie semper in tellus. Sed congue et odio sed euismod.</p>
<p>Aliquam a convallis justo. Vivamus venenatis, erat eget pulvinar gravida, ipsum lacus aliquet velit, vel luctus diam ipsum a diam. Cras eu tincidunt arcu, vitae rhoncus purus. Vestibulum fermentum consectetur porttitor. Suspendisse imperdiet porttitor
tortor, eget elementum tortor mollis non.</p>
</div>
</section>
<section id="section5" data-nav="Section 5">
<div class="landing__container">
<h2>Section 5</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi fermentum metus faucibus lectus pharetra dapibus. Suspendisse potenti. Aenean aliquam elementum mi, ac euismod augue. Donec eget lacinia ex. Phasellus imperdiet porta orci eget mollis.
Sed convallis sollicitudin mauris ac tincidunt. Donec bibendum, nulla eget bibendum consectetur, sem nisi aliquam leo, ut pulvinar quam nunc eu augue. Pellentesque maximus imperdiet elit a pharetra. Duis lectus mi, aliquam in mi quis, aliquam
porttitor lacus. Morbi a tincidunt felis. Sed leo nunc, pharetra et elementum non, faucibus vitae elit. Integer nec libero venenatis libero ultricies molestie semper in tellus. Sed congue et odio sed euismod.</p>
<p>Aliquam a convallis justo. Vivamus venenatis, erat eget pulvinar gravida, ipsum lacus aliquet velit, vel luctus diam ipsum a diam. Cras eu tincidunt arcu, vitae rhoncus purus. Vestibulum fermentum consectetur porttitor. Suspendisse imperdiet porttitor
tortor, eget elementum tortor mollis non.</p>
</div>
</section>
</main>
<footer class="page__footer">
<p>© Udacity</p>
</footer>
null
在这里,您可以在functionsaddActiveClass/removeActiveClass
函数中添加/删除类,例如:
//Add class
navBar.querySelector('li a[href*="#'+section.getAttribute('id')+'"]').classList.add('your-active-class');
//Remove Class
navBar.querySelector('li a[href*="#'+section.getAttribute('id')+'"]').classList.remove('your-active-class');
注意:我在菜单中添加了一些CSS,只是为了测试的目的让它变得清晰。
工作样本:
null
// select all sections
const sections = document.querySelectorAll('section');
// select the navbar id
const navBar = document.getElementById('navbar__list');
// create a document fragment to hold all the lists
const frag = document.createDocumentFragment();
/**
* End Global Variables
* Start Helper Functions
*
*/
/**
* End Helper Functions
* Begin Main Functions
*
*/
// build the nav
for (let section of sections) {
const sectionId = section.getAttribute('id');
const sectionTitle = section.getAttribute('data-nav');
const list = document.createElement('li');
const links = document.createElement('a');
// construct the li's
links.setAttribute('href', `#${sectionId}`);
links.innerText = sectionTitle;
list.appendChild(links);
links.classList = 'menu__link';
frag.appendChild(list);
// add event listener for smooth scroll
links.addEventListener('click', function(event) {
event.preventDefault();
window.scrollTo({
top: section.offsetTop - 60,
behavior: 'smooth'
});
});
};
navBar.appendChild(frag);
// select the a tags inside navbar
const allLinks = navBar.querySelectorAll('a');
/* // Add class 'active' to section when near top of viewport */
// Get the Top of the section
function sectionView(section) {
return section.getBoundingClientRect().top;
}
// Remove Active Class of all sections
function removeActiveClass(section) {
navBar.querySelector('li a[href*="#'+section.getAttribute('id')+'"]').classList.remove('your-active-class');
return section.classList.remove('your-active-class');
}
// Add Active Class to the required section
function addActiveClass(is_inview, section) {
if (is_inview) {
section.classList.add('your-active-class');
navBar.querySelector('li a[href*="#'+section.getAttribute('id')+'"]').classList.add('your-active-class');
};
}
// combine functions //
const activeSection = () => {
sections.forEach(section => {
const itemView = sectionView(section);
viewport = () => itemView > -200 && itemView <= 400;
removeActiveClass(section);
addActiveClass(viewport(), section);
});
};
// add eventlistener to window
window.addEventListener('scroll', activeSection);
li a.your-active-class{
color: green;
}
.navbar__menu{
position: fixed;
background: white;
float: right;
width: 100%;
left: 500px;
}
<link href="https://fonts.googleapis.com/css?family=Fira+Sans:900|Merriweather&display=swap" rel="stylesheet">
<!-- HTML Follows BEM naming conventions
IDs are only used for sections to connect menu achors to sections -->
<header class="page__header">
<nav class="navbar__menu">
<!-- Navigation starts as empty UL that will be populated with JS -->
<ul id="navbar__list"></ul>
</nav>
</header>
<main>
<header class="main__hero">
<h1>Landing Page </h1>
</header>
<!-- Each Section has an ID (used for the anchor) and
a data attribute that will populate the li node.
Adding more sections will automatically populate nav.
The first section is set to active class by default -->
<section id="section1" data-nav="Section 1" class="your-active-class">
<div class="landing__container">
<h2>Section 1</h2>
<p>Lorem ipsum dolor sit amet</p>
<p>Aliquam a convallis justo. Vivamus venenatis, erat eget pulvinar gravida, ipsum lacus aliquet velit, vel luctus diam ipsum a diam. Cras eu tincidunt arcu, vitae rhoncus purus. Vestibulum fermentum consectetur porttitor. Suspendisse imperdiet porttitor
tortor, eget elementum tortor mollis non.</p>
</div>
</section>
<section id="section2" data-nav="Section 2">
<div class="landing__container">
<h2>Section 2</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi fermentum metus faucibus lectus pharetra dapibus. Suspendisse potenti. Aenean aliquam elementum mi, ac euismod augue. Donec eget lacinia ex. Phasellus imperdiet porta orci eget mollis.
Sed convallis sollicitudin mauris ac tincidunt. Donec bibendum, nulla eget bibendum consectetur, sem nisi aliquam leo, ut pulvinar quam nunc eu augue. Pellentesque maximus imperdiet elit a pharetra. Duis lectus mi, aliquam in mi quis, aliquam
porttitor lacus. Morbi a tincidunt felis. Sed leo nunc, pharetra et elementum non, faucibus vitae elit. Integer nec libero venenatis libero ultricies molestie semper in tellus. Sed congue et odio sed euismod.</p>
<p>Aliquam a convallis justo. Vivamus venenatis, erat eget pulvinar gravida, ipsum lacus aliquet velit, vel luctus diam ipsum a diam. Cras eu tincidunt arcu, vitae rhoncus purus. Vestibulum fermentum consectetur porttitor. Suspendisse imperdiet porttitor
tortor, eget elementum tortor mollis non.</p>
</div>
</section>
<section id="section3" data-nav="Section 3">
<div class="landing__container">
<h2>Section 3</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi fermentum metus faucibus lectus pharetra dapibus. Suspendisse potenti. Aenean aliquam elementum mi, ac euismod augue. Donec eget lacinia ex. Phasellus imperdiet porta orci eget mollis.
Sed convallis sollicitudin mauris ac tincidunt. Donec bibendum, nulla eget bibendum consectetur, sem nisi aliquam leo, ut pulvinar quam nunc eu augue. Pellentesque maximus imperdiet elit a pharetra. Duis lectus mi, aliquam in mi quis, aliquam
porttitor lacus. Morbi a tincidunt felis. Sed leo nunc, pharetra et elementum non, faucibus vitae elit. Integer nec libero venenatis libero ultricies molestie semper in tellus. Sed congue et odio sed euismod.</p>
<p>Aliquam a convallis justo. Vivamus venenatis, erat eget pulvinar gravida, ipsum lacus aliquet velit, vel luctus diam ipsum a diam. Cras eu tincidunt arcu, vitae rhoncus purus. Vestibulum fermentum consectetur porttitor. Suspendisse imperdiet porttitor
tortor, eget elementum tortor mollis non.</p>
</div>
</section>
<!-- Add new sections -->
<section id="section4" data-nav="Section 4">
<div class="landing__container">
<h2>Section 4</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi fermentum metus faucibus lectus pharetra dapibus. Suspendisse potenti. Aenean aliquam elementum mi, ac euismod augue. Donec eget lacinia ex. Phasellus imperdiet porta orci eget mollis.
Sed convallis sollicitudin mauris ac tincidunt. Donec bibendum, nulla eget bibendum consectetur, sem nisi aliquam leo, ut pulvinar quam nunc eu augue. Pellentesque maximus imperdiet elit a pharetra. Duis lectus mi, aliquam in mi quis, aliquam
porttitor lacus. Morbi a tincidunt felis. Sed leo nunc, pharetra et elementum non, faucibus vitae elit. Integer nec libero venenatis libero ultricies molestie semper in tellus. Sed congue et odio sed euismod.</p>
<p>Aliquam a convallis justo. Vivamus venenatis, erat eget pulvinar gravida, ipsum lacus aliquet velit, vel luctus diam ipsum a diam. Cras eu tincidunt arcu, vitae rhoncus purus. Vestibulum fermentum consectetur porttitor. Suspendisse imperdiet porttitor
tortor, eget elementum tortor mollis non.</p>
</div>
</section>
<section id="section5" data-nav="Section 5">
<div class="landing__container">
<h2>Section 5</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi fermentum metus faucibus lectus pharetra dapibus. Suspendisse potenti. Aenean aliquam elementum mi, ac euismod augue. Donec eget lacinia ex. Phasellus imperdiet porta orci eget mollis.
Sed convallis sollicitudin mauris ac tincidunt. Donec bibendum, nulla eget bibendum consectetur, sem nisi aliquam leo, ut pulvinar quam nunc eu augue. Pellentesque maximus imperdiet elit a pharetra. Duis lectus mi, aliquam in mi quis, aliquam
porttitor lacus. Morbi a tincidunt felis. Sed leo nunc, pharetra et elementum non, faucibus vitae elit. Integer nec libero venenatis libero ultricies molestie semper in tellus. Sed congue et odio sed euismod.</p>
<p>Aliquam a convallis justo. Vivamus venenatis, erat eget pulvinar gravida, ipsum lacus aliquet velit, vel luctus diam ipsum a diam. Cras eu tincidunt arcu, vitae rhoncus purus. Vestibulum fermentum consectetur porttitor. Suspendisse imperdiet porttitor
tortor, eget elementum tortor mollis non.</p>
</div>
</section>
</main>
<footer class="page__footer">
<p>© Udacity</p>
</footer>
我有三个分区作为导航表。我想在单击时将活动类添加到div中。下面是我的代码,但它不能正常工作。请帮我把它修好。 HTML CSS JS
我的目标是从用户输入的“AM”-“PM”字符串格式打印包含24小时十进制格式的进入和退出时间的列表,如以下字符串数组:{6AM#8AM,11AM#1PM,7AM#8PM,7AM#8AM,10AM#12PM,12PM#4PM,1PM#4PM,8AM#9AM} 我在for循环中声明了各个列表,并在循环中为它们赋值,但从代码中得到了以下运行时异常:java。lang.IndexOutOfBoundsEx
< code>list_of_lists=[[1,2,3],[4,5,6]] < br > < code > list _ to _ add =[" A "," B "," C"] 我希望结果是list_of_lists会变成: 谢谢!
我有一个简单的ListView程序,其中试图附加滚动条。上下滚动时列表没有移动,在这里,列表应该相应地移动。似乎,我无法正确设置contentItem。寻找一些提示。 请在下面找到我的示例核心,于是我在listView中添加了一个垂直滚动条。rolesListModel是我的模特。 主要的qml
我从消防队拿到了我的文件。目前我在列表中有以下内容: 然后,在下面的方法中,我转换这个列表以放入一个具有所需格式的地图。
问题内容: 这是一个非常简单的程序: 这是我期望的输出: 但是我得到了这个: 真的有我不来的东西! 问题答案: 你必须做 不 现在可以正常工作: 当您执行类似操作时,您会在一个列表中三次获得相同的列表。 相同的 意思是,当您更改其中之一时,您也会更改所有它们(因为只有一个列表被引用了3次)。 您需要创建三个独立的列表来解决此问题。您可以通过列表理解来做到这一点。您在此处构造一个由独立的空列表组成的