html+css+jQuery+JavaScript实现tab自动切换功能
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了html+css+jQuery+JavaScript实现tab自动切换功能,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4678字,纯文字阅读大概需要7分钟。
内容图文
tab1.html内容
<! DOCTYPE html > < html > < head > < meta charset ="UTF-8" > < title >tab选项卡</title><style> * { margin: 0; padding: 0;} ul,li { list-style: none;} body { background-color: #323232; font-size: 12px; font-family: "微软雅黑"; margin: 100px;}</style><link rel="stylesheet" href="tab1.css"/><script src="../jquery-2.1.0.js" type="text/javascript"></script><script src="tab1.js" type="text/javascript"></script></head><body><div class="js-tab tab" data-config=‘{ "triggerType": "mouseover", "effect": "fade", "invoke": 2, "auto": 3000 }‘><ul class="tab-nav"><li class="actived"><a href="javascript:void(0);">新闻</a></li><li><a href="#">娱乐</a></li><li><a href="#">电影</a></li><li><a href="#">科技</a></li></ul><div class="content-wrap"><div class="content-item current" style="overflow: hidden;"><img src="a.jpg"/></div><div class="content-item" style="overflow: hidden;"><img src="b.jpg"/></div><div class="content-item" style="overflow: hidden;"><img src="c.jpg"/></div><div class="content-item" style="overflow: hidden;"><img src="d.jpg"/></div></div></div><script> $(function() { // TABvar tab1 =new Tab($(".js-tab").eq(0)); // }); </script></body></html>
tab1.css内容
.tab{width: 300px;} .tab .tab-nav{height: 30px;} .tab .tab-nav li {float: left;margin-right: 5px;background-color: #767676;border-radius: 3px 3px 0 0;} .tab .tab-nav li a{display: block;height: 30px; padding: 0 20px; color: #FFFFFF;line-height: 30px; text-decoration: none;} .tab .tab-nav li.actived{background-color: #FFFFFF;} .tab .tab-nav li.actived a {color: #777;} .tab .content-wrap{background-color: #FFFFFF;padding: 5px;height: 200px;} .tab .content-wrap .content-item {position:absolute; height: 200px; width:290px; display: none;} .tab .content-wrap {display: block;overflow: hidden;} .tab .content-wrap .current{display: block;overflow: hidden;}
tab1.js内容
;(function($){ var Tab = function(tab){ var _this_ = this; //保存单个tab组件this.tab = tab; //默认配置参数this.config = { //用来定义鼠标的触发类型,是click还是mouseover "triggerType": "mouseover", //用来定义内容切换效果,是直接切换还是淡入淡出效果 "effect": "default", //默认展示第几个tab "invoke": 1, //用来定义tab是否自动切换,当指定了时间间隔,就表示自动切换,并且切换时间为指定的时间间隔 "auto": false }; //如果配置参数存在,就扩展掉默认的配置参数if(this.getConfig()){ $.extend(this.config, this.getConfig()); }; //保存tab标签列表、对应的内容列表this.tabItems = this.tab.find("ul.tab-nav li"); this.contentItems = this.tab.find("div.content-wrap div.content-item"); //保存配置参数var config = this.config; if(config.triggerType === "click"){ this.tabItems.bind(config.triggerType, function(){ _this_.invoke($(this)); }); }elseif(config.triggerType === "mouseover" || config.triggerType != "click"){ this.tabItems.mouseover(function(){ _this_.invoke($(this)); }); }; //自动切换功能,当配置了时间,我们就根据时间间隔进行自动切换if(config.auto){ //定义一个全局的定时器this.timer = null; //计数器this.loop = 0; this.autoPlay(); //鼠标放到选中的区域,停止轮播,鼠标移开,开启自动轮播this.tab.hover(function(){ window.clearInterval(_this_.timer) },function(){ _this_.autoPlay(); }) }; //设置默认显示第几个tabif(config.invoke > 1){ this.invoke(this.tabItems.eq(config.invoke - 1)); }; }; Tab.prototype = { //自动间隔时间切换 autoPlay:function(){ var _this_ = this, tabItems = this.tabItems, //临时保存tab列表 tabLength = tabItems.size(),//tab的个数 config = this.config; this.timer = window.setInterval(function(){ _this_.loop++; if(_this_.loop>=tabLength){ _this_.loop = 0; }; tabItems.eq(_this_.loop).trigger(config.triggerType) },config.auto) }, //事件驱动函数 invoke:function(currentTab){ var _this_ = this; /*** * 要执行Tab的选中状态,当前选中的加上activd(标记为白底) * 切换对应的tab内容,要根据配置参数的effect是default还是fade **/var index = currentTab.index(); //tab选中状态 currentTab.addClass("actived").siblings().removeClass("actived"); //切换对应的内容区域var effect = this.config.effect; var conItems = this.contentItems; if(effect === "default" || effect != "fade"){ conItems.eq(index).addClass("current").siblings().removeClass("current"); }elseif(effect === "fade"){ conItems.eq(index).fadeIn().siblings().fadeOut(); } //注意:如果配置了自动切换,记得把当前的loop的值设置成当前的tab的indexif(this.config.auto){ this.loop = index; }; }, //获取配置参数 getConfig:function(){ //拿一下tab elem节点上的data-configvar config = this.tab.attr("data-config"); //console.log(config)//确保有配置参数if(config && config!=""){ return $.parseJSON(config); }else{ returnnull; } } }; window.Tab = Tab; })(jQuery);
代码源自慕课网:http://www.imooc.com/learn/825
期待共同学习进步。。。
谢谢
原文:http://www.cnblogs.com/zhzhang/p/7354177.html
内容总结
以上是互联网集市为您收集整理的html+css+jQuery+JavaScript实现tab自动切换功能全部内容,希望文章能够帮你解决html+css+jQuery+JavaScript实现tab自动切换功能所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。