Note that there are some explanatory texts on larger screens.

plurals
  1. POjQuery script ruined by variable assuming value 0 sometimes
    primarykey
    data
    text
    <p>My problem is that sometimes my script doesn't work. I know the problem but I can't solve it. Below, my script.</p> <pre><code>$(document).ready(function(){ /* Cria UL e popula de LI de acordo com o HTML do controle da Vtex*/ $('.slider-nav').after('&lt;ul class="slider-ul"&gt;&lt;/ul&gt;'); /* clona elementos se tiver menos que 3 */ var tamArray = $('#slider .box-banner').length; if (tamArray &lt; 3) { for (ij=1; ij&lt;=(3-tamArray); ij++){ $('#slider .box-banner:last').clone().appendTo('#slider'); } } $('#slider .box-banner').each(function(){ href = $(this).find('a').attr('href'); title = $(this).find('a').attr('title'); src = $(this).find('img').attr('src'); $('ul.slider-ul').append('&lt;li&gt;&lt;a href="'+href+'" title="'+title+'"&gt;&lt;img src="'+src+'"/&gt;&lt;/a&gt;&lt;/li&gt;'); }); $('.slider-ul').wrap('&lt;div class="slider-ul-wrapper" /&gt;'); /* Calcula offset do left pra colocar um banner no meio */ var tamJanela = $(window).width(); var tamImagem = $('#slider .slider-ul:first img').width(); var leftOffset = tamImagem - (tamJanela-tamImagem)/2; $('.slider-ul').css('left',-leftOffset); /* Clique em #nav-dir */ $('#nav-dir').click(function(){ $('.slider-ul li:last').clone().addClass('clone').insertBefore('.slider-ul li:first'); var soma = -(leftOffset)-tamImagem; $('.slider-ul').css('left',soma); $('.slider-ul').animate({ left: '+='+tamImagem+'' //left: "'+="+tamImagem+"'" }, 1000, 'swing', function(){ $('.slider-ul .clone').remove(); $('.slider-ul li:last').insertBefore('.slider-ul li:first'); $('.slider-ul').removeAttr('style'); $('.slider-ul').css('left',-leftOffset); }); }); /* Clique em #nav-esq */ $('#nav-esq').click(function(){ $('.slider-ul li:first').clone().addClass('clone').insertAfter('.slider-ul li:last'); //var soma = -tamImagem; //$('.slider-ul').css('left',soma); $('.slider-ul').animate({ left: '-='+tamImagem+'' //left: "'+="+tamImagem+"'" }, 1000, 'swing', function(){ $('.slider-ul .clone').remove(); $('.slider-ul li:first').insertAfter('.slider-ul li:last'); $('.slider-ul').removeAttr('style'); $('.slider-ul').css('left',-leftOffset); }); }); }); </code></pre> <p>And the relevant HTML:</p> <pre><code>&lt;div class="slider-ul-wrapper"&gt; &lt;ul class="slider-ul" style="left: 671.5px"&gt; &lt;li&gt; &lt;a href="undefined" title&gt; &lt;img src="...." /&gt; &lt;/a&gt; &lt;/li&gt; . . . &lt;/ul&gt; &lt;/div&gt; </code></pre> <p>The problem happens when <code>tamImagem</code> evaluates the <code>img</code> element width to 0. It then passes the wrong value to <code>leftOffset</code> value and ruins the expressions. Sometimes, after several refreshes, it works. Sometimes it doesn't. What is the problem? Does it return 0 because the element isn't ready? But why wouldn't it be ready?</p> <p>Also, there is some sort of Ajax request running on the background that I don't quite understand. Every image on the page receives a <code>complete</code> class when the requests are done. Should I wait until the <code>img</code> element is <code>complete</code>? How do I do that?</p> <p>Thanks a lot!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload