Home >Web Front-end >JS Tutorial >jquery writes Tab scroll navigation switching special effects_jquery

jquery writes Tab scroll navigation switching special effects_jquery

WBOY
WBOYOriginal
2016-05-16 15:06:182215browse

The example in this article is to share with you the tab switching effects written in jquery for your reference. The specific content is as follows

Effect description: Click the tab navigation, and the page will slide to the corresponding section below. And when the page is scrolled with the mouse, the tab above can also automatically switch to the section at the current location.

Code description: Write two actions separately in js, one is to click the tab and slide down to the corresponding section position; the other is to determine the scroll height of the current page when the mouse scrolls down and switch tabs.
js:

$(document).ready(function(){  
  $('.switch-tab>li').click(function(){
    var s=$('.switch-tab>li').index(this);
    $('body,html').animate({scrollTop:$('.tab-content>.tab-panel:eq('+s+')').offset().top-50},200);
  });

  var DT=$('.switch-tab').offset().top;
  $(window).scroll(function(){
    var wt=$(window).scrollTop(),l=$('.tab-content>.tab-panel'),s=l.length-1;
    if(wt<DT||wt>=l.last().offset().top+l.last().height()+50){
      $('.switch-tab').removeClass('fixed');
      $('.switch-tab>li:first').addClass('active').siblings().removeClass('active');
    }else{
      $('.switch-tab').addClass('fixed');
      for(var i=0;i<s;i++){
        if(wt>=parseInt(l.eq(i).offset().top-50)&&wt<parseInt(l.eq(i+1).offset().top-50)){
          s=i;
          break;
        }
      }
      $('.switch-tab>li:eq('+s+')').addClass('active').siblings().removeClass('active');
    }
  });
});

html:

<div class="switch-nav">
  <ul class="switch-tab">
    <li><a href="javascript:; ">拉托红酒</a></li>
    <li><a href="javascript:; ">法国酒庄风情</a></li>
    <li><a href="javascript:; ">红酒包装</a></li>
    <li><a href="javascript:; ">个性定制</a></li>
  </ul>
</div>

<div class="tab-content">
  <div class="tab-panel" id="cpxq">
    1111
  </div>
  <div class="tab-panel" id="cpxq">
    222
  </div>
  <div class="tab-panel" id="cpxq">
    333
  </div>
  <div class="tab-panel" id="cpxq">
    444
  </div>
</div>

The above is the entire content of this article, I hope it will be helpful to everyone’s study.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn