我目前有一个滚动锚点动画,它还向被点击的锚点添加了一个“活动”类。我试图将下面的函数也纳入到工作中,所以说有人点击“锚点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');
});
这几乎可以工作,但我有几个问题。主要使用scrollFn,什么是解除绑定? 使用此方法,将在点击时添加活动类,然后在窗口滚动时将其删除,然后在动画完成后返回到锚点,从而形成更多闪烁的外观。我该如何调整它,以便在动画完成后仅添加活动类,或者在滚动动画发生时将其保持开启状态? – jaasum 2010-05-30 18:05:34
@Jaasum:我在帖子中添加了解释。 如果它不适合你,你需要给我更多的信息,因为它在我的FF3.6 – Luc 2010-05-30 18:47:00