看到Sunnyhall在文章里使用了tab选项卡的效果,很不错,也想弄一个过来,网上扒了一串代码,样式没有怎么改。
首先是DIV部分
- <div id="jv_tabs"><span><a href="#" name="#tab1">标题一</a></span><span><a href="#" name="#tab2">标题二</a></span><span><a href="#" name="#tab3">标题三</a></span><span><a href="#" name="#tab4">标题四</a></span></div><div id="jv_content"><div id="tab1" class="jv_act">内容一</div><div id="tab2" class="jv_act">内容二</div><div id="tab3" class="jv_act">内容三</div><div id="tab4" class="jv_act">内容四</div></div>
然后是CSS
- #jv_tabs {
- overflow: hidden;
- width: 100%;
- margin: 0;
- padding: 0;
- list-style: none;
- }
- #jv_tabs span {
- float: left;
- margin: 0 -15px 0 0;
- }
- #jv_tabs a {
- float: left;
- position: relative;
- padding: 0 40px;
- height: 0;
- line-height: 30px;
- text-transform: uppercase;
- text-decoration: none;
- color: #fff;
- border-right: 30px solid transparent;
- border-bottom: 30px solid #3D3D3D;
- border-bottom-color: #777\9;
- opacity: .3;
- filter: alpha(opacity=30);
- }
- #jv_tabs a:hover,
- #jv_tabs a:focus {
- border-bottom-color: #2ac7e1;
- opacity: 1;
- filter: alpha(opacity=100);
- }
- #jv_tabs a:focus {
- outline: 0;
- }
- #jv_tabs #current {
- z-index: 3;
- border-bottom-color: #3d3d3d;
- opacity: 1;
- filter: alpha(opacity=100);
- }
- /* ------jv_content----- */
- #jv_content {
- background: #fff;
- border-top: 2px solid #3d3d3d;
- padding: 0;
- /*height: 220px;*/
- }
- #jv_content h2,
- #jv_content h3,
- #jv_content p {
- margin: 0 0 15px 0;
- }
最后加一段脚本
- <script>
- function resetjv_tabs(){
- $("#jv_content > div").hide(); //Hide all jv_content
- $("#jv_tabs a").attr("id",""); //Reset id's
- }
- var myUrl = window.location.href; //get URL
- var myUrlTab = myUrl.substring(myUrl.indexOf("#")); // For localhost/jv_tabs.html#tab2, myUrlTab = #tab2
- var myUrlTabName = myUrlTab.substring(0,4); // For the above example, myUrlTabName = #tab
- (function(){
- $("#jv_content > div").hide(); // Initially hide all jv_content
- $("#jv_tabs li:first a").attr("id","current"); // Activate first tab
- $("#jv_content > div:first").fadeIn(); // Show first tab jv_content
- $("#jv_tabs a").on("click",function(e) {
- e.preventDefault();
- if ($(this).attr("id") == "current"){ //detection for current tab
- return
- }
- else{
- resetjv_tabs();
- $(this).attr("id","current"); // Activate this
- $($(this).attr('name')).fadeIn(); // Show jv_content for current tab
- }
- });
- for (i = 1; i <= $("#jv_tabs li").length; i++) {
- if (myUrlTab == myUrlTabName + i) {
- resetjv_tabs();
- $("a[name='"+myUrlTab+"']").attr("id","current"); // Activate url tab
- $(myUrlTab).fadeIn(); // Show url tab jv_content
- }
- }
- })()
- </script>
注意:这个效果需要有jQuery库的支持,1.7.2版以上。
本站文章除注明转载/出处外,均为本站原创或翻译。若要转载但请务必注明出处,尊重他人劳动成果。
转载请注明出处链接 : https://www.inlojv.com/3761.html