• Doo...">
    All geek questions in one place

    Input and output of text in text

    I am having problems with a slide in the Text slider:

    HTML:

    <div id="textSlider"> <ul> <li class="active">Doors | Windows | Showers</li> <li>Superb Fenestration | Superior Service</li> </ul> </div> <div class="clear"></div> 

    JS:

     $('#textSlider ul li').each(function(){ setTimeout(function(){ $('.active').animate({'left': '0px'},500).delay(500).animate({'left': '-300px'},500); $(this).removeClass('active'); }, 2000); }); 

    CSS

     #textSlider{ position: relative; left: 0; } ul{ list-style: none; } ul li{ position: relative; left: -300px; } .clear{ clear: both; } 

    Fiddle

    What I need:

    I want the first line to turn on and then go out. After the first line, the second should do the same.

    Then the function should repeat the whole process.

    How to do it?

    +4
    jquery
    David van staden Jun 10 '13 at 19:35
    source share
    3 answers

    You can change your JavaScript to:

     setInterval(function(){ $('.active').animate({'left': '0px'},500) .delay(500) .animate({'left': '0px'},500) .animate({'left':'-300px'},500) .removeClass('active') .siblings('li') .addClass('active'); }, 2000); 

    JSFiddle Demo

    This will run endlessly every 2 seconds ...

    Keep in mind that it will iterate over only 2 elements (as shown in your example), if you need to iterate over the number of elements n , you can try this:

     setInterval(function(){ var $current = $('.active').animate({'left': '0px'},500) .delay(500) .animate({'left': '0px'},500) .animate({'left':'-300px'},500) .removeClass('active'); if($current.next('li').length > 0) { $current.next('li').addClass('active'); } else { $current.siblings('li:eq(0)').addClass('active'); } }, 2000); 

    Basically caching the current active element in the $current variable, using if to check if there is still <li> after it, in which case not, returning to the first <li> to add the active class ...

    JSFiddle Demo 2

    Finally . If you want the first loop to be different from the others, you can do something like this:

     setTimeout(function(){ loopList(); setInterval(loopList,4000); }, 1); function loopList(){ var $current = $('.active').animate({'left': '0px'},500) .delay(500).animate({'left': '0px'},500) .animate({'left':'-300px'},500) .removeClass('active'); if($current.next('li').length > 0) { $current.next('li').addClass('active'); } else { $current.siblings('li:eq(0)').addClass('active'); } } 

    In this case, you first call the first iteration from setTimeout for the first time (this is the reason for 0 in this example), and then call setInterval , which will loop every 4 seconds.

    JSFiddle Demo 3

    +5
    DarkAjax Jun 10 '13 at 19:46
    source share

    Fiddle

    I moved your logic to a separate function. The animate () method accepts a callback that changes the β€œactive” li, and then calls itself again. This gives you an endless loop.

      function slide() { var activeElement = $('li.active'); $('.active').animate({ 'left': '0px' }, 500).delay(500).animate({ 'left': '-300px' }, 500, function() { var nextActiveElement = activeElement.next('li'); activeElement.removeClass('active'); if (nextActiveElement.length < 1) { nextActiveElement = $('li').first(); } nextActiveElement.addClass('active'); slide(); }); } slide(); 
    +2
    officert Jun 10 '13 at 19:58
    source share

    You can use jquery callback to archive this:

     function showLi(){ $('#textSlider ul li').each(function(index, value){ setTimeout(function(){ $(value).animate({'left': '0px'},500).delay(500).animate({'left': '0px'},500).animate({'left':'-300px'},500); }, index*2000); }); } showLi(); setInterval(showLi, $("#textSlider ul li").length*2000); 

    See http://api.jquery.com/jQuery.each/

    The index is used to delay the timeout.

    Check the forked script:

    http://jsfiddle.net/3Dwkz/4/

    +1
    thertweck Jun 10 '13 at 19:41
    source share

    Source: https://habr.com/ru/post/1485504/

    More articles:

    • https://translate.googleusercontent.com/translate_c?depth=1&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1485499/intel-c-compiler-icc-seems-to-ingnore-sseavx-seetings&usg=ALkJrhjoTjvo7L52rUDNVDtet_xqCLatAw
    • GCC / LD cannot find link library - c ++
    • GCC library link not found? - c
    • How to determine input type - javascript
    • Codeigniter searches libraries in the "main" folder - php
    • Can I automatically generate a Venn chart from a hold graph - graph
    • MS SQL - replacing identifier values ​​in a query with values ​​from another table - sql-server
    • Question on Div Position in Firefox - html
    • How to properly access the value of an array in a callback function? - javascript
    • Live update Screen brightness with SeekBar - java

    All Articles

    Geek Questions | 2019