javascript - scroll to next and previous tab from tabbed box -


i've build tab container multiple tabs , corresponding content, visible if corresponding tab clicked. works great far, have tried add 2 buttons navigate next , previous tab , don't know how achieve this.

you'll find below i've done far. think it's no js code, maybe can give me hint's improve it.

thanks in advance

      $(document).ready(function(){            // hide contents accept first div          $('.tabcontent div:not(:first)').toggle();            // hide previous button          $('.previous').hide();            $('.tabs li').click(function () {                if($(this).is(':last-child')){                $('.next').hide();              }else{                $('.next').show();              }                if($(this).is(':first-child')){                $('.previous').hide();              }else{                $('.previous').show();              }                var position = $(this).position();              var corresponding = $(this).data("id");                // scroll clicked tab little gap left show previous tabs              scroll = $('.tabs').scrollleft();              $('.tabs').animate({'scrollleft': scroll+position.left-30},200);                // hide content divs              $('.tabcontent div').hide();                // show content of corresponding tab              $('div.' + corresponding).toggle();                // remove active class not active tabs              $('.tabs li').removeclass('active');                // add active class clicked tab              $(this).addclass('active');          });            });
      body{          background-color: gray;        }          *{          box-sizing: border-box;        }          .contentwrapper{          width: 350px;          margin: 0 auto;          position: relative;        }          .tabswrapper{          width: 100%;          height: 24px;          overflow: hidden;          position: relative;        }          .tabs{          margin: 0;          padding: 0;          position: absolute;          top: 0;          bottom: -25px;          left: 0;          right: 0;          white-space: nowrap;          overflow: auto;        }          .tabs li{          display: inline-block;          background-color: #ccc;          padding: 3px 8px;          cursor: pointer;        }          .tabs li.active{          background-color: white;        }          .next, .previous{          position: absolute;          padding: 2px 4px;          top: 0;          background-color: white;        }          .next{          right: -25px;        }          .previous{          left: -25px;        }          .tabcontent{          width: 100%;          background-color: white;          padding: 15px;        }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <div class="contentwrapper">        <div class="tabswrapper">          <ul class="tabs">            <li data-id="contentone" class="active">css</li>            <li data-id="contenttwo">html, html, html</li>            <li data-id="contentthree">js , jquery</li>            <li data-id="contentfour">one more tab</li>            <li data-id="contentfive">another tab</li>            <li data-id="contentsix">the last tab</li>          </ul>        </div>        <a class="next">></a>        <a class="previous"><</a>        <div class="tabcontent">          <div class="contentone">            <p>this css tab content</p>            <a href="">next</a>          </div>          <div class="contenttwo">            <p>this html tab content<br><br>1</p>            <a href="">next</a>          </div>          <div class="contentthree">            <p>this js tab content</p>            <a href="">next</a>          </div>          <div class="contentfour">            <p>this sample content</p>            <a href="">next</a>          </div>          <div class="contentfive">            <p>this more sample content</p>            <a href="">next</a>          </div>          <div class="contentsix">            <p>this more more sample content</p>            <a href="">next</a>          </div>        </div>      </div>

you can use trigger('click') programmatically click tab want. way code written tab change automatically executed. example see following code:

$('div a').click(function(e){     e.preventdefault();     $('li.active').next('li').trigger('click'); }); 

see jsfiddle here


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -