Problem downtown event images on demand
Hello, I am new to jquery and would like to, if possible, solve my problem. Sorry the mistakes, I'm brazilian is not very familiar with English.
Come on!
I have an event of low demand for images. I do not know for what reason, when the roll scroll to load images, the images start flashing before charging. This happens in Mozilla and Chrome, IE9 does not.
my code follows:
lazy:
- (function($, window) {
- var $window = $(window);
- $.fn.lazy = function(options) {
- var elements = this;
- var $container;
- var settings = {
- threshold : 0,
- failure_limit : 100,
- event : "scroll", /*pode ser scroll ou click para colocar em um botao*/
- effect : "fadeIn", /*pode ser fadeIn ou Show para efeito de aparecimento da imagem*/
- container : window,
- data_attribute : "src", /* pode ser qualquer nome*/
- skip_invisible : true,
- appear : null,
- load : null
- };
- function update() {
- var counter = 0;
-
- elements.each(function() {
- var $this = $(this);
- if (settings.skip_invisible && !$this.is(":visible")) {
- return;
- }
- if ($.abovethetop(this, settings) ||
- $.leftofbegin(this, settings)) {
- /* Nada. */
- } else if (!$.belowthefold(this, settings) &&
- !$.rightoffold(this, settings)) {
- $this.trigger("appear");
- /* se encontra uma imagem que ira carregar, redefinir o contador */
- counter = 0;
- } else {
- if (++counter > settings.failure_limit) {
- return false;
- }
- }
- });
- }
-
- if(options) {
-
- if (undefined !== options.failurelimit) {
- options.failure_limit = options.failurelimit;
- delete options.failurelimit;
- }
- if (undefined !== options.effectspeed) {
- options.effect_speed = options.effectspeed;
- delete options.effectspeed;
- }
- $.extend(settings, options);
- }
-
- $container = (settings.container === undefined ||
- settings.container === window) ? $window : $(settings.container);
- /* Dispara um evento de rolagem por rolagem. Não um evento de rolagem por imagem. */
- if (0 === settings.event.indexOf("scroll")) {
- $container.bind(settings.event, function(event) {
- return update();
- });
- }
- this.each(function() {
- var self = this;
- var $self = $(self);
- self.loaded = false;
- /* Quando aparecer é acionado imagem-original. */
- $self.one("appear", function() {
- if (!this.loaded) {
- if (settings.appear) {
- var elements_left = elements.length;
- settings.appear.call(self, elements_left, settings);
- }
- $("<img />")
- .bind("load", function() {
- $self
- .hide()
- .attr("src", $self.data(settings.data_attribute))
- [settings.effect](3000);
- self.loaded = true;
- /* Remover imagem do array para não exibir na próxima vez. "não repetir imagens" */
- var temp = $.grep(elements, function(element) {
- return !element.loaded;
- });
- elements = $(temp);
- if (settings.load) {
- var elements_left = elements.length;
- settings.load.call(self, elements_left, settings);
- }
- })
- .attr("src", $self.data(settings.data_attribute));
- }
- });
- /* Quando o evento imagem-original é acionado, mostra a/as imagem */
- if (0 !== settings.event.indexOf("scroll")) {
- $self.bind(settings.event, function(event) {
- if (!self.loaded) {
- $self.trigger("appear");
- }
- });
- }
- });
- /* Verifica se há alguma imagem para aparecer quando a janela é dimensionada */
- $window.bind("resize", function(event) {
- update();
- });
- /* Força verificação se imagem deve aparecer */
- $(document).ready(function() {
- update();
- });
-
- return this;
- };
- $.belowthefold = function(element, settings) {
- var fold;
-
- if (settings.container === undefined || settings.container === window) {
- fold = $window.height() + $window.scrollTop();
- } else {
- fold = $(settings.container).offset().top + $(settings.container).height();
- }
- return fold <= $(element).offset().top - settings.threshold;
- };
-
- $.rightoffold = function(element, settings) {
- var fold;
- if (settings.container === undefined || settings.container === window) {
- fold = $window.width() + $window.scrollLeft();
- } else {
- fold = $(settings.container).offset().left + $(settings.container).width();
- }
- return fold <= $(element).offset().left - settings.threshold;
- };
-
- $.abovethetop = function(element, settings) {
- var fold;
-
- if (settings.container === undefined || settings.container === window) {
- fold = $window.scrollTop();
- } else {
- fold = $(settings.container).offset().top;
- }
- return fold >= $(element).offset().top + settings.threshold + $(element).height();
- };
-
- $.leftofbegin = function(element, settings) {
- var fold;
-
- if (settings.container === undefined || settings.container === window) {
- fold = $window.scrollLeft();
- } else {
- fold = $(settings.container).offset().left;
- }
- return fold >= $(element).offset().left + settings.threshold + $(element).width();
- };
- $.inviewport = function(element, settings) {
- return !$.rightoffold(element, settings) && !$.leftofbegin(element, settings) &&
- !$.belowthefold(element, settings) && !$.abovethetop(element, settings);
- };
- /* */
- $.extend($.expr[':'], {
- "below-the-fold" : function(a) { return $.belowthefold(a, {threshold : 0}); },
- "above-the-top" : function(a) { return !$.belowthefold(a, {threshold : 0}); },
- "right-of-screen": function(a) { return $.rightoffold(a, {threshold : 0}); },
- "left-of-screen" : function(a) { return !$.rightoffold(a, {threshold : 0}); },
- "in-viewport" : function(a) { return $.inviewport(a, {threshold : 0}); },
-
- "above-the-fold" : function(a) { return !$.belowthefold(a, {threshold : 0}); },
- "right-of-fold" : function(a) { return $.rightoffold(a, {threshold : 0}); },
- "left-of-fold" : function(a) { return !$.rightoffold(a, {threshold : 0}); }
- });
- })(jQuery, window);
scrollstop:
- (function(){
-
- var special = jQuery.event.special,
- uid1 = 'D' + (+new Date()),
- uid2 = 'D' + (+new Date() + 1);
-
- special.scrollstart = {
- setup: function() {
-
- var timer,
- handler = function(evt) {
-
- var _self = this,
- _args = arguments;
-
- if (timer) {
- clearTimeout(timer);
- } else {
- evt.type = 'scrollstart';
- jQuery.event.handle.apply(_self, _args);
- }
-
- timer = setTimeout( function(){
- timer = null;
- }, special.scrollstop.latency);
-
- };
-
- jQuery(this).bind('scroll', handler).data(uid1, handler);
-
- },
- teardown: function(){
- jQuery(this).unbind( 'scroll', jQuery(this).data(uid1) );
- }
- };
-
- special.scrollstop = {
- latency: 300,
- setup: function() {
-
- var timer,
- handler = function(evt) {
-
- var _self = this,
- _args = arguments;
-
- if (timer) {
- clearTimeout(timer);
- }
-
- timer = setTimeout( function(){
-
- timer = null;
- evt.type = 'scrollstop';
- jQuery.event.handle.apply(_self, _args);
-
- }, special.scrollstop.latency);
-
- };
-
- jQuery(this).bind('scroll', handler).data(uid2, handler);
-
- },
- teardown: function() {
- jQuery(this).unbind( 'scroll', jQuery(this).data(uid2) );
- }
- };
-
- })();
and the call is made so:
- <script src="<%=urlhttp%>js2/jquery.1.7.0.min.js" type="text/javascript" charset="utf-8"></script>
- <script src="<%=urlhttp%>js2/jquery.scrollstop.js" type="text/javascript" charset="utf-8"></script>
- <script src="<%=urlhttp%>js2/jquery.lazy.js" type="text/javascript" charset="utf-8"></script>
- <script type="text/javascript" charset="utf-8">
- jQuery.noConflict();
- jQuery(document).ready(function($){
- $("img").lazy({
- event: "scrollstop"
- });
- });
- </script>
when I hide the comment, not true flashing images, but does not happen in the fadein slow.