Как сделать смену активного пункта меню при скролле ?(jQuery)
сразу к проблеме! Есть меню на одностраничный сайт(код ниже). Это меню изменяет активный пункт только при нажатии на сам пункт. (происходит плавная прокрутка к указанному месту). Но если воспользоваться колесиком мыши, cмена активного пункта не произойдет. Как сделать ,чтобы при прокрутке страницы колесиком изменялся активный пункт меню с помощью js или jquery?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{
margin: 0;
padding: 0;
box-sizing: border-box;
}
body{
background-color: #ffffff;
}
.header{
position: fixed;
top: 0;
width: 64%;
margin-left: 18%;
background-color: #fff;
z-index: 100;
}
.header-conteiner{
width: 100%;
border-bottom: 1px solid #828282;
padding-top: 32px;
padding-bottom: 33px;
}
.main-menu{
width: 100%;
display: flex;
padding: 0px 20px;
justify-content: space-between;
}
.main-menu .menu-link{
text-decoration: none;
font-family:sans-serif;
font-size: 18px;
line-height: 22px;
color:#828282;
}
.main-menu .menu-link.active{
color: #070707;
}
.description{
height: 420px;
text-align: center;
margin-top: 88px;
padding-top: 100px;
width: 100%;
padding-bottom: 104px;
}
.about{
background: #F6F6F6;
height: 900px;
padding-top: 120px;
text-align: center;
}
.title{
font-family: sans-serif;
font-size: 34px;
line-height: 42px;
color: #070707;
}
.skills{
width: 100%;
padding-top: 91px;
height: 600px;
text-align: center;
}
.portfolio{
width: 100%;
background-color: #F6F6F6;
height: 1200px;
padding-top: 113px;
text-align: center;
}
.contacts{
width: 100%;
padding-top: 100px;
height: 800px;
text-align: center;
}
</style>
</head>
<body>
<header class="header">
<div class="header-conteiner">
<div class="main-menu">
<a class="menu-link active" href="javascript://0" onclick="slowScroll('#de')">Home</a>
<a class="menu-link" href="javascript://0" onclick="slowScroll('#ab')">About me</a>
<a class="menu-link" href="javascript://0" onclick="slowScroll('#sk')">Skills</a>
<a class="menu-link" href="javascript://0" onclick="slowScroll('#po')">Portfolio</a>
<a class="menu-link" href="javascript://0" onclick="slowScroll('#co')">Contacts</a>
</div>
</div>
</header>
<section class="description" id="de">
<h2 class="title">Home</h2>
</section>
<section class="about" id="ab">
<h2 class="title">About me</h2>
</section>
<section class="skills" id="sk">
<h2 class="title">Skills</h2>
</section>
<section class="portfolio" id="po">
<h2 class="title">Portfolio</h2>
</section>
<section class="contacts" id="co">
<h2 class="title">Contacts</h2>
</section>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>
<script type="text/javascript">
$(document).on('click' , '.main-menu a', function(){
$(this).addClass('active').siblings().removeClass('active');
})
function slowScroll(id) {
var offset = 88;
$('html, body').animate({
scrollTop: $(id).offset().top - offset
}, 500);
return false;
}
</script>
</body>
</html>
Ответы (1 шт):
Автор решения: kremen.karl
→ Ссылка
Во-первых, не использовать jQuery. Но если прям очень надо, то jQuery Scrollfire.