javascript - How can i scroll menu to current item in menu? -


i have menu contains 11 elements. 3 of displayed. there buttons slider, helps scrolling menu. on site give special class element, when href == url of page(highlighting of current element). need move current element in first left position of menu on window load, menu list must scrolled, when page loading, , current element stays in first left place regardless position in list. that's code:

function slider() {    $('#menu > ul > li').first().css('left', '300px').appendto('#menu > ul').animate({      "left": "-=300px"    }, 200);  }    function slidel() {    $('#menu > ul > li').last().animate({      "left": "+=300px"    }, 200, function() {      $(this).prependto('#menu > ul').css('left', '0px');    });  }    $('.arrow-right').click(function() {    slider();  });  $('.arrow-left').click(function() {    slidel();  });
* {    padding: 0;    margin: 0;    box-sizing: border-box;    font-family: "segoe ui", sans-serif;  }  nav {    position: relative;    max-width: 300px;    margin: 25px auto;  }  nav > ul {    height: 50px;    text-align: center;    max-width: 300px;    overflow: hidden;    border: 2px solid #ccc;    font-size: 0;  }  nav > ul > li {    display: inline-block;    vertical-align: middle;    font-size: 16px;  }  nav > ul > li {    color: #000;    display: block;    text-decoration: none;    line-height: 50px;    padding: 0 15px;    transition: 0.25s;  }  nav > ul > li a:hover {    background: #000;    color: #fff;  }  .arrows > .arrow {    position: absolute;    top: 50%;    border: 2px solid #f00;    z-index: 99;    width: 24px;    height: 24px;    margin-top: -12px;    cursor: pointer;    display: block;  }  .arrow-right {    right: 0;  }  .arrow-left {    left: 0;  }  .active {    color: #029a55;  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>  <nav id="menu">    <ul>      <li><a href="#">menu 1</a>      </li>      <li><a href="#">menu 2</a>      </li>      <li><a href="#">menu 3</a>      </li>      <li><a href="#">menu 4</a>      </li>      <li><a href="#">menu 5</a>      </li>      <li><a href="#">menu 6</a>      </li>      <li><a href="#">menu 7</a>      </li>      <li><a href="#">menu 8</a>      </li>      <li class="active"><a href="#">menu 9</a>      </li>      <li><a href="#">menu 10</a>      </li>      <li><a href="#">menu 11</a>      </li>    </ul>    <div class="arrows">      <span class="arrow arrow-right"></span>      <span class="arrow arrow-left"></span>    </div>  </nav>

please me supplement code solve problem. try this

var curry = document.getelementsbyclassname('active')[0];  $(window).load(function() {    curry.style.left = '0px';  });

but nothing happens.

you need loop of slider until first child has active class adding firstload function, in updated snippet.

firstload();  function firstload() {    while(!$('#menu > ul > li').first().hasclass("active")) {      slider();    }  }    function slider() {    $('#menu > ul > li').first().css('left', '300px').appendto('#menu > ul').animate({      "left": "-=300px"    }, 200);  }    function slidel() {    $('#menu > ul > li').last().animate({      "left": "+=300px"    }, 200, function() {      $(this).prependto('#menu > ul').css('left', '0px');    });  }    $('.arrow-right').click(function() {    slider();  });  $('.arrow-left').click(function() {    slidel();  });
* {    padding: 0;    margin: 0;    box-sizing: border-box;    font-family: "segoe ui", sans-serif;  }  nav {    position: relative;    max-width: 300px;    margin: 25px auto;  }  nav > ul {    height: 50px;    text-align: center;    max-width: 300px;    overflow: hidden;    border: 2px solid #ccc;    font-size: 0;  }  nav > ul > li {    display: inline-block;    vertical-align: middle;    font-size: 16px;  }  nav > ul > li {    color: #000;    display: block;    text-decoration: none;    line-height: 50px;    padding: 0 15px;    transition: 0.25s;  }  nav > ul > li a:hover {    background: #000;    color: #fff;  }  .arrows > .arrow {    position: absolute;    top: 50%;    border: 2px solid #f00;    z-index: 99;    width: 24px;    height: 24px;    margin-top: -12px;    cursor: pointer;    display: block;  }  .arrow-right {    right: 0;  }  .arrow-left {    left: 0;  }  .active {    color: #029a55;  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>  <nav id="menu">    <ul>      <li><a href="#">menu 1</a>      </li>      <li><a href="#">menu 2</a>      </li>      <li><a href="#">menu 3</a>      </li>      <li><a href="#">menu 4</a>      </li>      <li><a href="#">menu 5</a>      </li>      <li><a href="#">menu 6</a>      </li>      <li><a href="#">menu 7</a>      </li>      <li><a href="#">menu 8</a>      </li>      <li class="active"><a href="#">menu 9</a>      </li>      <li><a href="#">menu 10</a>      </li>      <li><a href="#">menu 11</a>      </li>    </ul>    <div class="arrows">      <span class="arrow arrow-right"></span>      <span class="arrow arrow-left"></span>    </div>  </nav>


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 -