mercredi 3 mai 2017

Prevent scroll to fire on every scroll

I'm trying to use a flag to prevent scroll to fire function each time scroll is triggered. $(window).unbind("scroll");since I'm using .scroll() with other functions. So why isn't it preventing the function from firing each time?

JS:

$(window).scroll(function() {
        $("iframe").each( function() {
            $this = $(this);
            _src = $this.attr('src');
            _ID = $this.attr('id');
            _yPos = $(window).scrollTop();
            _thisHT = $this.height();
            _thisTop = $this.offset().top;
            _thisBottom = _thisTop + _thisHT;
            _Play = true;

            if (_Play) {
                if( _yPos > _thisTop*0.5 && _yPos < _thisBottom) {       
                    $this.attr('src', _src + '&autoplay=1');
                    _Play=false;

                //$(window).unbind("scroll");
                } else {
                    $this.attr('src', _src);

                }
            }
        }); 
    });

Aucun commentaire:

Enregistrer un commentaire