英文:
Fix On Scroll Using Jquery
问题
我有一个固定的页眉,下面是一个具有顶部锚点菜单的英雄图像,我希望当滚动时锚点菜单滚动到页眉标记时,它应该变为固定状态,但当锚点菜单滚回到其原始页面位置时,它应该取消固定状态。我已经有必要的CSS使其工作,但似乎无法弄清楚我需要哪些jQuery代码来使其工作。
这是我到目前为止的代码,但这不起作用,因为我的页眉和锚点菜单是响应式的,根据视口大小进行缩放,因此锚点菜单和页眉之间的间隙保持变化。
$(window).scroll(function () {
var scroll = $(window).scrollTop();
if (scroll >= 687) {
$(".anchor-menu").addClass("anchor-menu-grey");
} else {
$(".anchor-menu").removeClass("anchor-menu-grey");
}
});
英文:
I have a fixed header underneath it a hero image that has on top of it an anchor menu, I want that when the anchor menu reaches on scroll the header tag it should become fixed, but when the anchor menu scrolls back to its original page position it should become unfixed. I have the neccesary css to make it work but I can't seem to figure out the jquery code I would need to make this work.
This is the code I had until now but this won't work because my header and anchor menu are responsive and scale according to viewport size so the gap between the anchor menu and header keeps changing.
$(window).scroll(function () {
var scroll = $(window).scrollTop();
if (scroll >= 687) {
$(".anchor-menu").addClass("anchor-menu-grey");
} else {
$(".anchor-menu").removeClass("anchor-menu-grey");
}
});
答案1
得分: 0
我明白了最后,以下是使其工作的jQuery代码:
<script>
var fixunderHeader = $('.anchor-menu').offset().top; // 获取元素的初始位置
$(window).scroll(function () { // 分配滚动事件监听器
var currentScroll = $(window).scrollTop(); // 获取当前位置
var headerHeight = $('header').outerHeight(); // 获取页眉元素的高度
if (currentScroll >= fixunderHeader) {
$(".anchor-menu").addClass("anchor-menu-grey").css({
top: headerHeight + 'px' // 将top值设置为页眉的高度
});
} else {
$(".anchor-menu").removeClass("anchor-menu-grey").css({
top: '' // 移除'top'属性
});
}
});
</script>
让我们逐步分解代码以更好地理解:
代码的第一行使用jQuery初始化一个变量fixunderHeader。它检索具有class为anchor-menu的元素的顶部偏移位置。
接下来,代码使用$(window).scroll(function() { ... })设置了滚动事件监听器。这意味着每当用户滚动窗口时,将执行函数内部的代码。
在滚动事件函数内部,使用$(window).scrollTop()将当前滚动位置存储在变量currentScroll中。这获取了从页面顶部滚动的距离。
然后,使用$('header').outerHeight()将页眉元素的高度存储在变量headerHeight中。这获取了页眉元素的高度。
接下来,代码检查当前滚动位置(currentScroll)是否大于或等于anchor-menu元素的初始位置(fixunderHeader)。
如果条件为真,代码使用.addClass("anchor-menu-grey")将类anchor-menu-grey添加到anchor-menu元素。它还使用.css({ top: headerHeight + 'px' })将元素的top CSS属性设置为页眉的高度,从而确保anchor-menu元素在页眉下方。
如果条件为假,意味着用户已经滚动回到了初始的anchor-menu元素上方的位置,代码使用.removeClass("anchor-menu-grey")从anchor-menu元素中移除类anchor-menu-grey。它还通过设置.css({ top: '' })将top CSS属性设置为空字符串,从而使anchor-menu元素返回到其原始位置。
总之,此代码根据滚动位置动态添加和删除anchor-menu元素的类。它还调整元素的位置,以确保用户向下滚动时该元素保持在页眉下方。
我基于这个答案构建了这个代码:https://stackoverflow.com/a/15850380/20152881
英文:
I figured it out in the end, following is the jquery to make it work:
<script>
var fixunderHeader = $('.anchor-menu').offset().top; // get initial position of the element
$(window).scroll(function () { // assign scroll event listener
var currentScroll = $(window).scrollTop(); // get current position
var headerHeight = $('header').outerHeight(); // get the height of the header element
if (currentScroll >= fixunderHeader) {
$(".anchor-menu").addClass("anchor-menu-grey").css({
top: headerHeight + 'px' // set the top value to the height of the header
});
} else {
$(".anchor-menu").removeClass("anchor-menu-grey").css({
top: '' // remove the 'top' property
});
}
});
</script>
Let's break down the code step by step for better understanding:
The first line of code initializes a variable fixunderHeader using jQuery. It retrieves the top offset position of the element with class anchor-menu.
Next, the code sets up a scroll event listener using $(window).scroll(function() { ... }). This means that whenever the user scrolls the window, the code inside the function will be executed.
Inside the scroll event function, the current scroll position is stored in a variable currentScroll using $(window).scrollTop(). This retrieves the distance scrolled from the top of the page.
The height of the header element is then stored in a variable headerHeight using $('header').outerHeight(). This gets the height of the header element.
The code then checks if the current scroll position (currentScroll) is greater than or equal to the initial position of the anchor-menu element (fixunderHeader).
If the condition is true, the code adds the class anchor-menu-grey to the anchor-menu element using .addClass("anchor-menu-grey"). It also sets the top CSS property of the element to the height of the header using .css({ top: headerHeight + 'px' }). This ensures that the anchor-menu element stays below the header.
If the condition is false, meaning the user has scrolled back up to a position above the initial anchor-menu element, the code removes the class anchor-menu-grey from the anchor-menu element using .removeClass("anchor-menu-grey"). It also removes the top CSS property by setting it to an empty string using .css({ top: '' }). This allows the anchor-menu element to return to its original position.
In summary, this code dynamically adds and removes a class to the anchor-menu element based on the scroll position. It also adjusts the positioning of the element to ensure it stays below the header when the user scrolls down.
I built upon this answer: https://stackoverflow.com/a/15850380/20152881
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论