Ich habe eine Animation für meinen Foto-Blog implementiert. Ich habe immer noch ein großes Problem, weil das Element 'body' die Animation zweimal aktiviert.jQuery: Das 'body'-Element aktiviert das Scroll-Ereignis zweimal
Ich denke, das Problem stammt aus dem $ ('Körper'). Weil ich denke, dass wenn der Körper animiert wird, das Scroll-Ereignis erneut aktiviert wird und somit das Ereignis zweimal auslöst.
Das Problem meines Codes ist Scrollen der Seite nach oben. Wenn ich die Seite nach oben scrolle. Der scrollAnimatePrev wird ausgelöst und dann wird $ ('body') Element sich selbst animieren. Nach der Animation wird die animierende Variable auf "false" gesetzt. Aber das Element $ ('body') löst das Bildlaufereignis aus, weil ich denke, wenn ich scrollTop setze, wird das Bildlaufereignis ausgelöst. Also wird currentPos wieder auf $ (window) .scrollTop() gesetzt, dann gibt currentPos> previousPos true zurück und animating gibt true zurück, so dass scrollAnimate ausgelöst wird.
Jetzt möchte ich das beheben. Wie?
$(function() {
var record = 0;
var imgHeight = $(".images").height();
var offset = $(".images").eq(0).offset();
var offsetHeight = offset.top;
var previousPos = $(window).scrollTop();
var animating = false;
var state = 0;
$(window).scroll(function() {
var currentPos = $(window).scrollTop();
console.log(currentPos);
if(currentPos > previousPos && !animating) {
record++;
scrollAnimate(record, imgHeight, offsetHeight);
animating = true;
} else if (currentPos < previousPos && !animating) {
record--
scrollAnimatePrev(record, imgHeight, offsetHeight);
animating = true;
}
previousPos = currentPos;
console.log(previousPos)
})
function scrollAnimate(record, imgHeight, offsetHeight) {
$('body').animate(
{scrollTop: (parseInt(offsetHeight) * (record+1)) + (parseInt(imgHeight) * record)},
1000,
"easeInOutQuart"
)
.animate(
{scrollTop: (parseInt(offsetHeight) * (record)) + (parseInt(imgHeight) * (record))},
1000,
"easeOutBounce",
function() {
animating = false;
}
)
}
function scrollAnimatePrev(record, imgHeight, offsetHeight) {
$('body').animate(
{scrollTop: ((parseInt(imgHeight) * record) + (parseInt(offsetHeight) * record)) - offsetHeight},
1000,
"easeInOutQuart"
)
.animate(
{scrollTop: ((parseInt(imgHeight) * record) + (parseInt(offsetHeight) * record))},
1000,
"easeOutBounce",
function() {
animating = false;
}
)
}
})
Ja, wie haben Sie das Problem zu beheben .. ich eine hässliche Hack haben, ist es ein setTimeout .. Ich möchte diese generisch sein und auf Timing verlassen nicht ... –
Ich glaube, ich stoppte den belebtes Verkettungs (); Ich bearbeite meine Antwort – alex
IC, okay .. Danke Alex –