Home  >  Article  >  Web Front-end  >  How to implement label scrolling switching in JS

How to implement label scrolling switching in JS

亚连
亚连Original
2018-06-15 16:03:291639browse

What this article brings to you is to use JS to realize the scroll switching effect after clicking on the item label. Friends who are interested can test it and learn it.

The JS effect of this article is to achieve the effect of switching the content below with the scroll when the mouse clicks on the ITEM label. Let's first take a look at the rendering after running.

The following is the original running code of Script House:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>jQuery实现的平滑滚动选项卡</title>
<script type="text/javascript" src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<style>
.tabs{width: 1200px; margin: 0 auto; height: 336px; overflow: hidden;}
.tabs_item{height: 36px;}
.tabs_block{height: 300px;}
.list_item{float: left; width: 200px; height: 36px; line-height: 36px; font-size: 26px; text-align: center;background-color:#FC0;cursor: pointer;}
.list_item.active{color: #FFF;background-color:#F60}
.tabs_block{width: 20000px; position: relative;}
.list_block{display: none; float: left; width: 1200px; height: 300px; font-size: 100px; text-align: center; line-height: 300px;background-color:#F4F4F4}
.list_block.show{display: block;}
</style>
<script>jQuery.easing[&#39;easeOutCubic&#39;] = function (x, t, b, c, d) {
 return c*((t=t/d-1)*t*t + 1) + b;
};
function Tabs(tabs,list,block,item,$){
 var tabs = $(tabs);
 var width = tabs.width();
 tabs.find(list+&#39;:first&#39;).addClass(&#39;active&#39;);
 tabs.find(item+&#39;:first&#39;).addClass(&#39;show&#39;);
 tabs.find(list).click(function(){
 var $this = $(this);
 var i = tabs.find(block).find(&#39;.show&#39;).index();
 var n = $this.index();
 $this.addClass(&#39;active&#39;).siblings(&#39;.active&#39;).removeClass(&#39;active&#39;);
 tabs.find(item).eq(n).addClass(&#39;show&#39;);
 if(n > i){
 tabs.find(block).stop().animate({&#39;left&#39;:&#39;-&#39;+width+&#39;px&#39;},600,&#39;easeOutCubic&#39;,function(){
  tabs.find(block).css(&#39;left&#39;,&#39;0px&#39;);
  tabs.find(item).eq(n).siblings(&#39;.show&#39;).removeClass(&#39;show&#39;);
  });
 }else{
 tabs.find(block).css(&#39;left&#39;,&#39;-&#39;+width+&#39;px&#39;).stop().animate({&#39;left&#39;:0},600,&#39;easeOutCubic&#39;,function(){
  tabs.find(item).eq(n).siblings(&#39;.show&#39;).removeClass(&#39;show&#39;);
  });
 }
 });
}
</script>
</head>
<body>
 <p class="tabs">
 <p class="tabs_item">
  <p class="list_item">list item 1</p>
  <p class="list_item">list item 2</p>
  <p class="list_item">list item 3</p>
  <p class="list_item">list item 4</p>
  <p class="list_item">list item 5</p>
  <p class="list_item">list item 6</p>
 </p>
 <p class="clear"> </p>
 <p class="tabs_block">
  <p class="list_block">list block 1</p>
  <p class="list_block">list block 2</p>
  <p class="list_block">list block 3</p>
  <p class="list_block">list block 4</p>
  <p class="list_block">list block 5</p>
  <p class="list_block">list block 6</p>
 </p>
 </p>
<script type="text/javascript">
 jQuery(document).ready(function(){
  var myTabs = new Tabs(&#39;.tabs&#39;,&#39;.list_item&#39;,&#39;.tabs_block&#39;,&#39;.list_block&#39;,jQuery);
 });
</script>
<p style="text-align:center">更多代码请访问:<a href="//www.jb51.net" target="_blank">脚本之家</a></p>
</body>
</html>

In the code we quoted Baidu’s jquery

<script type="text/javascript" src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>

The above is I compiled it for everyone, I hope it will be helpful to everyone in the future.

Related articles:

How to implement Baidu search through JS technology

How to use cookie-parser middleware in Express

What is the function of getBoundingClientRect in js?

How to implement the shopping cart ball parabola in vue 2.0

Migrate AngularJS1.x application to React (detailed tutorial)

Explain in detail how to use components and their functions in Vue.js?

The above is the detailed content of How to implement label scrolling switching in JS. For more information, please follow other related articles on the PHP Chinese website!

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