Improve click-scroll functionality by adding checks for section existence before accessing offsets, enhancing robustness against missing elements. Update scroll event handling for better performance and reliability.
This commit is contained in:
@@ -5,7 +5,9 @@ var sectionArray = [1, 2, 3, 4, 5];
|
|||||||
|
|
||||||
$.each(sectionArray, function (index, value) {
|
$.each(sectionArray, function (index, value) {
|
||||||
$(document).scroll(function () {
|
$(document).scroll(function () {
|
||||||
var offsetSection = $("#" + "section_" + value).offset().top - 90;
|
var section = $("#" + "section_" + value);
|
||||||
|
if (section.length) { // Check if section exists
|
||||||
|
var offsetSection = section.offset().top - 90;
|
||||||
var docScroll = $(document).scrollTop();
|
var docScroll = $(document).scrollTop();
|
||||||
var docScroll1 = docScroll + 1;
|
var docScroll1 = docScroll + 1;
|
||||||
|
|
||||||
@@ -15,12 +17,15 @@ $.each(sectionArray, function (index, value) {
|
|||||||
$(".navbar-nav .nav-item .nav-link").eq(index).addClass("active");
|
$(".navbar-nav .nav-item .nav-link").eq(index).addClass("active");
|
||||||
$(".navbar-nav .nav-item .nav-link").eq(index).removeClass("inactive");
|
$(".navbar-nav .nav-item .nav-link").eq(index).removeClass("inactive");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
$(".click-scroll")
|
$(".click-scroll")
|
||||||
.eq(index)
|
.eq(index)
|
||||||
.click(function (e) {
|
.click(function (e) {
|
||||||
var offsetClick = $("#" + "section_" + value).offset().top - 90;
|
var section = $("#" + "section_" + value);
|
||||||
|
if (section.length) { // Check if section exists
|
||||||
|
var offsetClick = section.offset().top - 90;
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
$("html, body").animate(
|
$("html, body").animate(
|
||||||
{
|
{
|
||||||
@@ -28,6 +33,7 @@ $.each(sectionArray, function (index, value) {
|
|||||||
},
|
},
|
||||||
300
|
300
|
||||||
);
|
);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user