[英]Jquery hide Div with anchor text when bottom page section is reached
我有自動滾動功能,有一個靜態箭頭,可讓用戶滾動到頁面的下一部分。 當用戶到達“聯系人”部分(最后一頁)時,我希望箭頭隱藏起來,因為沒有其他頁面可以向下滾動。
當前,導航箭頭在最后一頁上消失了,但在“關於”和“簡介”部分也消失了。
$(function() {
$('a.page-scroll').bind('click', function(event) {
var $anchor = $(this);
$('html, body').stop().animate({
scrollTop: $($anchor.attr('href')).offset().top
}, 1500, 'easeInOutExpo');
event.preventDefault();
});
});
function nextSection()
{
var scrollPos = $(document).scrollTop();
$('#section-navigator a').each(function () {
var currLink = $(this);
var refElement = $(currLink.attr("href"));
if (refElement.position().top > scrollPos) {
var $anchor = $(this);
$('html, body').stop().animate({
scrollTop: $($anchor.attr('href')).offset().top
}, 1500, 'easeInOutExpo');
event.preventDefault();
location.hash = "";
location.hash = currLink.attr("href");
if ($($anchor.attr('href')).attr('id') == "contact") {
$("div.page-scroll").hide();
}
return false;
}
});
}
<div class="page-scroll">
<img class="arrow-down page-scroll-btn" src="img/arrow_dark.png" onclick="nextSection()" />
</div>
謝謝!
從外觀#contact
,您將鏈接用作下一個選擇器的ID,因此您應該在#contact
中使用#contact
。
另外,您將if括號)
放在錯誤的位置
if ($anchor.attr('href') == "#contact") {
}
如果要將其與目標div ID進行比較,則需要執行以下操作:
if ($($anchor.attr('href')).attr('id') == "contact") {
$("div.page-scroll").hide();
}
但這似乎需要額外的處理才能獲得相同的結果
更新
考慮到您的所有修改-由於它們沒有創建MCVE,因此它們中的任何一個都沒有真正的幫助-而且我們似乎正在遠離原始問題。 我將執行以下操作:
當您在html中手動綁定時,擺脫jQuery頂部的jquery onclick綁定函數,將您的下一部分函數更改為:
function nextSection() {
var currentPos = $(document).scrollTop();
$('#section-navigator a').each(function() {
var currLinkHash = $(this).attr("href");
var refElement = $(currLinkHash);
if (refElement.offset().top > scrollPos) { // change this to offset
$('html, body').stop().animate({
scrollTop: refElement.offset().top // just use refElement
}, 1500, 'easeInOutExpo');
location.hash = "";
location.hash = currLinkHash;
if (refElement.attr('id') == "contact") { // hide the scroller if the id is contact
$("div.page-scroll").hide();
}
return false;
}
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.