【发布时间】:2020-04-15 15:38:44
【问题描述】:
我目前有一个滚动锚动画,它还为单击的锚添加了一个“活动”类。我也在尝试将以下功能融入作品中,因此假设有人单击“锚 1”,“锚 1”将获得一个活动类,并且窗口将滚动到该位置。但是,在那之后说用户手动开始向下滚动页面,我希望删除活动类。我现在遇到的问题是,当单击锚点的滚动动画发生时,将发生以下功能。仅当从单击的锚点滚动窗口时,如何才能禁用此功能?
$(window).scroll(function() {
$('a[href^=#]').removeClass('active');
});
这是我正在使用的滚动锚脚本:
/*******
*** Anchor Slider by Cedric Dugas ***
*** Http://www.position-absolute.com ***
Never have an anchor jumping your content, slide it.
Don't forget to put an id to your anchor !
You can use and modify this script for any project you want, but please leave this comment as credit.
*****/
jQuery.fn.anchorAnimate = function(settings) {
settings = jQuery.extend({
speed : 500
}, settings);
return this.each(function(){
var caller = this
$(caller).click(function (event) {
event.preventDefault()
var locationHref = window.location.href
var elementClick = $(caller).attr("href")
var destination = $(elementClick).offset().top;
$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination}, settings.speed, 'easeOutCubic', function() {
window.location.hash = elementClick
});
return false;
})
})
}
最后,我的 jQuery:
// Scrolling Anchors
$('[href^=#]').anchorAnimate();
// Active Class For Clicked Anchors
var anchorscroll = $('a[href^=#]')
anchorscroll.click(function(){
var anchorlocation = $(this).attr("href");
anchorscroll.removeClass('active');
$(this).addClass('active');
$('a[href='+anchorlocation+']').addClass('active');
});
【问题讨论】:
标签: jquery scroll jquery-events