javascript - jQuery: The 'body' element is activating the scroll event twice -


I have applied the animation for my photo blog. I still have a big problem because the 'body' element twice the animation is active Has been doing.

I think the problem arises from $ ('body'). Because I think that when the body is animating, the scroll event will be reactivated and thus the event will be triggered twice.

The problem of my code is scrolling up the page when I scroll the page upwards the scroll animated trigger will trigger and then the $ ('body') element will make itself animated after animation the animating variable is false. Is set on. But $ ('body') triggers the scroll event because I think when I set the scroll, the scroll event is triggered. So once again the current page is set to $ (window) .scrollTop () then returns currentPos> previousPos true and return returns right, so it will trigger scrolling inadvertently.

Now I want to fix this. how?

  $ (function () {var record = 0; var imgHeight = $ ("Picture") height (); var offset = $ (".picture"). Eq (0) . Offset (); off offset = offset.top; var lastpost = $ (window) .scrollTop (); var animating = falls; var state = 0; $ (window) .scroll (function () {var currentPos = $ ( Window) .scrollTop (); console.log (currentPos); if (currentPos> previousPos & amp;! Animating) {record ++; scrollAnimate (record, imgHyight, offsetHyight); animating = true} else if (CurrentPos & LT; PreviousPos & amp; Animating) {record-- scrollAnimatePrev (record, imgHeight, offsetHeight); animating = true} previousPos = currentPos; console.log (previousPos)}) function scrollAnimate , ImgHeight, offsetHeight) {. $ ('Body') Chetan ({scrollTop: (parseInt (offsetHyight) * (record + 1)) + (parseInt (imgHyight) * record)}, 1000, "easeInOutQuart") .animate {ScrollTop: (parseInt * (record)) + (parseint (IMGHEEET) * (record)), 1000, "comfort outboice", function () {animating = falls; }}} Function scrollAnimatePrev (record, imgHeight, offsetHeight) {$ ('body') Chetan ({scrollTop: ((ParseInt (imgHyight) * record) + (parseInt (offsetHyight) * record) - offsetHeight}, 1000, "EaseInOutQuart") .animate ({scrollTop: (parseInt (imgHyight) * record) + (parseInt (offsetHyight) * record)}, 1000, "easeOutBounce", function () {animate = false;})}})  

I think this callback can do twice.

  $ ('# id, # id2'). ({Width: '200px'}, I had a similar problem.  

100, function () {doSomethingOnceOnly ();})

This is my DoSomethingOnce () was calling twice, and I thought it would be in double selectors $ argument I made it just 2 different selectors and it worked fine. So this way

  $ ('# id'). Animate ({width: '200px'}, 100); $ ('# Id2'). Animate ({width: '200px'}, 100, function () {doSomethingOnceOnly (););  

Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -