আপনি পৃষ্ঠাটি নীচে স্ক্রোল করার সাথে সাথে সক্রিয় মেনু আইটেম পরিবর্তন হয়। এটি কিভাবে হয়?
উত্তর:
এটি ধারকটির (সাধারণত উইন্ডো) স্ক্রোল ইভেন্টের সাথে আবদ্ধ হয়ে সম্পন্ন হয়েছে ।
দ্রুত উদাহরণ:
// Cache selectors
var topMenu = $("#top-menu"),
topMenuHeight = topMenu.outerHeight()+15,
// All list items
menuItems = topMenu.find("a"),
// Anchors corresponding to menu items
scrollItems = menuItems.map(function(){
var item = $($(this).attr("href"));
if (item.length) { return item; }
});
// Bind to scroll
$(window).scroll(function(){
// Get container scroll position
var fromTop = $(this).scrollTop()+topMenuHeight;
// Get id of current scroll item
var cur = scrollItems.map(function(){
if ($(this).offset().top < fromTop)
return this;
});
// Get the id of the current element
cur = cur[cur.length-1];
var id = cur && cur.length ? cur[0].id : "";
// Set/remove active class
menuItems
.parent().removeClass("active")
.end().filter("[href='#"+id+"']").parent().addClass("active");
});
স্ক্রোল অ্যানিমেশন সহ jsFiddle এ কর্মের উপরের দেখুন See
শুধু আমার কোড এবং স্নিপার এবং ডেমো লিঙ্ক পরীক্ষা করুন:
// Basice Code keep it
$(document).ready(function () {
$(document).on("scroll", onScroll);
//smoothscroll
$('a[href^="#"]').on('click', function (e) {
e.preventDefault();
$(document).off("scroll");
$('a').each(function () {
$(this).removeClass('active');
})
$(this).addClass('active');
var target = this.hash,
menu = target;
$target = $(target);
$('html, body').stop().animate({
'scrollTop': $target.offset().top+2
}, 500, 'swing', function () {
window.location.hash = target;
$(document).on("scroll", onScroll);
});
});
});
// Use Your Class or ID For Selection
function onScroll(event){
var scrollPos = $(document).scrollTop();
$('#menu-center a').each(function () {
var currLink = $(this);
var refElement = $(currLink.attr("href"));
if (refElement.position().top <= scrollPos && refElement.position().top + refElement.height() > scrollPos) {
$('#menu-center ul li a').removeClass("active");
currLink.addClass("active");
}
else{
currLink.removeClass("active");
}
});
}
কেবলমাত্র @ মারকাস একওয়াল এর উত্তরের পরিপূরক হিসাবে। এটি করার ফলে কেবল অ্যাঙ্কর লিঙ্ক পাবেন। আপনার যদি অ্যাঙ্কর লিংক এবং নিয়মিত একটি মিশ্রণ থাকে তবে আপনার সমস্যা হবে না।
jQuery(document).ready(function(jQuery) {
var topMenu = jQuery("#top-menu"),
offset = 40,
topMenuHeight = topMenu.outerHeight()+offset,
// All list items
menuItems = topMenu.find('a[href*="#"]'),
// Anchors corresponding to menu items
scrollItems = menuItems.map(function(){
var href = jQuery(this).attr("href"),
id = href.substring(href.indexOf('#')),
item = jQuery(id);
//console.log(item)
if (item.length) { return item; }
});
// so we can get a fancy scroll animation
menuItems.click(function(e){
var href = jQuery(this).attr("href"),
id = href.substring(href.indexOf('#'));
offsetTop = href === "#" ? 0 : jQuery(id).offset().top-topMenuHeight+1;
jQuery('html, body').stop().animate({
scrollTop: offsetTop
}, 300);
e.preventDefault();
});
// Bind to scroll
jQuery(window).scroll(function(){
// Get container scroll position
var fromTop = jQuery(this).scrollTop()+topMenuHeight;
// Get id of current scroll item
var cur = scrollItems.map(function(){
if (jQuery(this).offset().top < fromTop)
return this;
});
// Get the id of the current element
cur = cur[cur.length-1];
var id = cur && cur.length ? cur[0].id : "";
menuItems.parent().removeClass("active");
if(id){
menuItems.parent().end().filter("[href*='#"+id+"']").parent().addClass("active");
}
})
})
মূলত আমি প্রতিস্থাপন করেছি
menuItems = topMenu.find("a"),
দ্বারা
menuItems = topMenu.find('a[href*="#"]'),
অ্যাঙ্কারের সাথে সমস্ত লিঙ্ক কোথাও মেলে, এবং এটির সাথে এটি কাজ করার জন্য প্রয়োজনীয় সমস্তগুলি পরিবর্তন করে
এটি jsfizz এ কর্মে দেখুন
আপনি যদি JQuery 3 এ কাজ করার জন্য গৃহীত উত্তর চান তবে কোডটি এইভাবে পরিবর্তন করুন:
var scrollItems = menuItems.map(function () {
var id = $(this).attr("href");
try {
var item = $(id);
if (item.length) {
return item;
}
} catch {}
});
জাভাস্ক্রিপ্টটিকে বিধ্বস্ত হওয়া থেকে রোধ করার জন্য আমি চেষ্টা করেও যুক্ত করেছি যদি id আইডিতে কোনও উপাদান না থাকে। আরও উন্নত করতে নির্দ্বিধায় মনে করুন;)
menuItems = topMenu.find("a"),tomenuItems = topMenu.find("a").slice(0,4),4