Home >Web Front-end >JS Tutorial >jQuery implements simple demonstration of Tab switching effect_jquery
The example in this article is a simple demonstration of jQuery's Tab switching effect. It is completely my own thinking and implementation process, and I share it with you for your reference. The details are as follows:
At first my Html code frame looked like this:
<div class="tabs"> <ul> <li class="acss" data-box="#panel-1">标签1</li> <li class="bcss" data-box="#panel-2">标签2</li> <li class="bcss" data-box="#panel-3">标签3</li> </ul> <div id="panel-1">内容111111</div> <div id="panel-2" style="display:none;">内容222222</div> <div id="panel-3" style="display:none;">内容333333</div> </div>
Later changed to the following one:
<dl class="tabs"> <dt> <a class="acss" href="#panel-1">标签1</a> <a class="bcss" href="#panel-2">标签2</a> <a class="bcss" href="#panel-3">标签3</a> </dt> <dd id="panel-1">内容1</dd> <dd id="panel-2" style="display:none;">内容2</dd> <dd id="panel-3" style="display:none;">内容3</dd> </dl>
The reason why I changed to this is because I think dl dt dd is used less than div ul li in page layout, which can achieve better isolation. If we use js to operate the dl dt dd object, it will have less impact on other elements on the page. In addition, there is no need to customize the data-box attribute in the li tag, which is more in line with page writing standards. And the overall feel of this structure is better than the one above.
The implementation code of the plug-in is as follows:
(function ($) { $.fn.Tabs = function (options) { //默认参数设置 var settings = { beforeCss: "bcss", //激活前样式名 afterCss: "acss", //激活后样式名 model: "mouseover" //切换方式("mouseover"或者"click") }; //不为空,则合并参数 if (options) $.extend(settings, options); //获取a标签集合 var arr_a = $("> dt > a", this); //给a标签分别绑定事件 arr_a.each(function () { $(this).bind(settings.model, function (event) { //去除a标签的锚点跳转 event.preventDefault(); //样式控制 $(this).removeClass().addClass(settings.afterCss) .siblings("a").removeClass().addClass(settings.beforeCss); //隐藏与显示控制 var dd_id = $(this).attr("href"); $(dd_id).show().siblings("dd").hide(); }); }); //遵循链式原则 return this.each(function () { }); }; })(jQuery);
The reason why it is said to be lightweight is because the amount of code is really small and very simple. Comments added, I believe everyone can understand.
The model in settings is used to control the switching method:
At first I wanted to use hover to implement mouse slide-in switching, but I found that hover does not support bind binding. Because hover is the product of jquery encapsulating the mouseover event, it is not an authentic event and therefore cannot be bound.
Here is a DEMO:
<!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> <title></title> <style type="text/css"> * { margin: 0; padding: 0; } .tabs { width: 504px; margin: 50px auto; } .acss,.bcss { text-decoration:none; line-height: 35px; font-size: 14px; padding:8px 15px; } .bcss { background-color: #D4D4D4; border-bottom:1px solid white; } .acss { background-color: orange; border-bottom:1px solid orange; } .tabs dd { width: 500px; height: 300px; border: 1px solid orange; text-align: center; line-height: 300px; } </style> </head> <body> <dl class="tabs" id="tabs1"> <dt> <a class="acss" href="#panel-1">标签1</a> <a class="bcss" href="#panel-2">标签2</a> <a class="bcss" href="#panel-3">标签3</a> </dt> <dd id="panel-1"><h1>鼠标滑入切换</h1></dd> <dd id="panel-2" style="display:none;">内容2</dd> <dd id="panel-3" style="display:none;">内容3</dd> </dl> <dl class="tabs" id="tabs2"> <dt> <a class="acss" href="#panel-4">标签1</a><!--默认第一个激活--> <a class="bcss" href="#panel-5">标签2</a> <a class="bcss" href="#panel-6">标签3</a> </dt> <dd id="panel-4"><h1>鼠标点击切换</h1></dd><!--默认第一个显示--> <dd id="panel-5" style="display:none;">内容2</dd> <dd id="panel-6" style="display:none;">内容3</dd> </dl> <script src="../js/jquery-1.4.1.min.js" type="text/javascript"></script> <script src="../js/jquery.similar.Tabs.js" type="text/javascript"></script> <script type="text/javascript"> $("#tabs1").Tabs(); //默认鼠标滑入切换 $("#tabs2").Tabs({model:"click"}); //设置为点击切换 </script> </body> </html>
The rendering is as follows:
I hope this article will be helpful to everyone learning jquery programming.