Skip to content

Commit b8b6edb

Browse files
authored
Merge pull request #2 from againstsoftware/sticky
sticky headers mal en movil
2 parents b2adb37 + 9681e71 commit b8b6edb

2 files changed

Lines changed: 138 additions & 46 deletions

File tree

css/style.css

Lines changed: 94 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,10 @@ body {
106106
top: 0;
107107
left: 0;
108108
right: 0;
109-
z-index: 1030; /* Highest z-index */
109+
z-index: 1030;
110110
background: var(--bs-dark) !important;
111+
transition: all 0.3s ease;
112+
width: 100%;
111113
min-height: 70px;
112114
padding: 1rem 0;
113115
}
@@ -156,14 +158,13 @@ body {
156158
min-height: 95vh; /* This makes it take full viewport height */
157159
background: var(--bs-dark);
158160
color: var(--bs-light);
159-
padding: 0; /* Remove default padding */
161+
padding: 0;
160162
margin-bottom: 3rem;
161163
min-width: 100%;
162164
}
163165

164-
/* If the image is too large, you might want to constrain its height */
165166
.hero-section .about-img img {
166-
max-height: 40vh; /* Adjust this value as needed */
167+
max-height: 40vh;
167168
width: auto;
168169
}
169170

@@ -172,6 +173,9 @@ body {
172173
position: sticky;
173174
top: 70px;
174175
background: var(--bs-dark);
176+
padding: 2rem 0;
177+
margin-bottom: 0;
178+
z-index: 1020;
175179
padding: 2rem;
176180
margin: 0;
177181
z-index: 1020;
@@ -202,6 +206,12 @@ body {
202206
z-index: -1;
203207
}
204208

209+
/*** Sections ***/
210+
section {
211+
position: relative;
212+
min-height: auto;
213+
}
214+
205215
.section-header.is-sticky {
206216
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
207217
}
@@ -210,7 +220,6 @@ body {
210220
section {
211221
position: relative;
212222
margin: 0;
213-
margin-bottom: 0;
214223
}
215224

216225
.section-content {
@@ -454,13 +463,14 @@ section {
454463

455464
.testimonial-carousel .owl-dots {
456465
width: 100%;
457-
height: 80px; /* Fixed height that accommodates both normal and active states */
466+
height: auto;
458467
display: flex;
459468
align-items: center;
460469
justify-content: center;
461470
margin-top: 2rem;
462-
padding: 0;
471+
padding: 1rem;
463472
position: relative; /* Add this */
473+
flex-wrap: wrap;
464474
}
465475

466476
.testimonial-carousel .owl-dots .owl-dot {
@@ -711,6 +721,16 @@ section {
711721

712722
/*** Media Queries ***/
713723
@media (max-width: 991px) {
724+
725+
body {
726+
padding-top: 56px; /* Standard mobile navbar height */
727+
}
728+
729+
.navbar {
730+
min-height: 56px;
731+
padding: 0.5rem 0;
732+
}
733+
714734
.navbar-collapse {
715735
background: var(--bs-dark);
716736
padding: 1rem;
@@ -733,6 +753,25 @@ section {
733753
position: relative;
734754
}
735755

756+
.section-header {
757+
top: 56px;
758+
padding: 0.75rem;
759+
margin-bottom: 0;
760+
}
761+
762+
.section-content {
763+
padding: 1rem 0;
764+
}
765+
766+
.text-body {
767+
padding: 1rem;
768+
margin: 0;
769+
}
770+
771+
section {
772+
margin-bottom: 0;
773+
}
774+
736775
.section-header h2 {
737776
font-size: 1.75rem;
738777
}
@@ -755,6 +794,38 @@ section {
755794
}
756795

757796
@media (max-width: 767px) {
797+
798+
body {
799+
padding-top: 56px; /* Standard mobile navbar height */
800+
}
801+
802+
.navbar {
803+
min-height: 56px;
804+
padding: 0.5rem 0;
805+
}
806+
807+
.navbar-collapse {
808+
background: var(--bs-dark);
809+
padding: 1rem;
810+
margin-top: 0.5rem;
811+
max-height: calc(100vh - 70px);
812+
overflow-y: auto;
813+
}
814+
815+
.navbar .container-fluid .container {
816+
padding: 0 15px;
817+
}
818+
819+
.navbar-brand {
820+
z-index: 1000;
821+
position: relative;
822+
}
823+
824+
.navbar-toggler {
825+
z-index: 1000;
826+
position: relative;
827+
}
828+
758829
.hero-section {
759830
padding: 4rem 0;
760831
}
@@ -764,25 +835,28 @@ section {
764835
}
765836

766837
.section-header {
767-
padding: 0.75rem 0;
838+
top: 56px;
839+
padding: 0.75rem;
840+
margin-bottom: 0;
768841
}
769-
770-
.section-header h2 {
771-
font-size: 1.25rem;
772-
margin: 0;
773-
}
774-
842+
775843
.section-content {
776-
padding: 0.75rem 0;
844+
padding: 1rem 0;
777845
}
778-
779-
/* Remove any unexpected margins */
780-
.container, .container-fluid {
781-
padding-left: 15px;
782-
padding-right: 15px;
846+
847+
.text-body {
848+
padding: 1rem;
783849
margin: 0;
784850
}
785851

852+
section {
853+
margin-bottom: 0;
854+
}
855+
856+
.section-header h2 {
857+
font-size: 1.5rem;
858+
}
859+
786860
.testimonial-carousel .owl-nav {
787861
display: none;
788862
}

js/main.js

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
return false;
3636
});
3737

38-
// Sticky Navbar
38+
39+
// Simple navbar scroll spy
3940
document.addEventListener("DOMContentLoaded", function () {
4041
const dataSpyList = document.querySelectorAll('[data-bs-spy="scroll"]');
4142
dataSpyList.forEach((dataSpyEl) => {
@@ -51,44 +52,61 @@
5152
if (this.hash !== "") {
5253
e.preventDefault();
5354
const hash = this.hash;
54-
55-
document.querySelectorAll(".navbar-nav a.nav-link").forEach((l) => {
56-
l.classList.remove("active");
57-
});
58-
this.classList.add("active");
59-
6055
document.querySelector(hash).scrollIntoView({
6156
behavior: "smooth",
6257
});
6358

6459
// En móvil cerrar tras pulsar un enlace
60+
6561
const navbarToggle = document.querySelector(".navbar-toggler");
6662
if (window.getComputedStyle(navbarToggle).display !== "none") {
6763
document.querySelector(".navbar-collapse").classList.remove("show");
6864
}
65+
6966
}
7067
});
7168
});
69+
});
70+
71+
document.addEventListener("DOMContentLoaded", function () {
72+
const navbar = document.querySelector(".navbar");
73+
const navbarHeight = navbar.offsetHeight;
74+
75+
const observer = new IntersectionObserver(
76+
(entries) => {
77+
entries.forEach((entry) => {
78+
const header = entry.target.querySelector(".section-header");
79+
const nextSection = entry.target.nextElementSibling;
80+
81+
if (!header) return;
7282

73-
window.addEventListener("scroll", () => {
74-
const scrollPosition = window.scrollY;
75-
76-
document.querySelectorAll("section").forEach((section) => {
77-
const sectionTop = section.offsetTop - 150; // 150px de margen
78-
const sectionBottom = sectionTop + section.offsetHeight;
79-
80-
if (scrollPosition >= sectionTop && scrollPosition < sectionBottom) {
81-
const currentId = section.getAttribute("id");
82-
document
83-
.querySelectorAll(".navbar-nav a.nav-link")
84-
.forEach((link) => {
85-
link.classList.remove("active");
86-
if (link.getAttribute("href") === `#${currentId}`) {
87-
link.classList.add("active");
83+
if (entry.isIntersecting) {
84+
header.style.position = "sticky";
85+
header.style.top = `${navbarHeight}px`;
86+
87+
// Only handle the case where headers would overlap
88+
if (nextSection) {
89+
const sectionRect = entry.target.getBoundingClientRect();
90+
const headerRect = header.getBoundingClientRect();
91+
92+
if (sectionRect.bottom <= headerRect.height + navbarHeight) {
93+
header.style.position = "absolute";
94+
header.style.top = `${entry.target.offsetHeight - headerRect.height}px`;
8895
}
89-
});
90-
}
91-
});
96+
}
97+
}
98+
});
99+
},
100+
{
101+
threshold: [0],
102+
rootMargin: `-${navbarHeight}px 0px 0px 0px`
103+
}
104+
);
105+
106+
// Observe each section
107+
document.querySelectorAll("section").forEach((section) => {
108+
observer.observe(section);
109+
92110
});
93111
});
94-
})(jQuery);
112+
})(jQuery);

0 commit comments

Comments
 (0)