tab 选项卡(初用jQuery)

---恢复内容开始---

由于对JQuery不熟悉,折腾了半天......

但不管怎么说,总算是完成了,也是一点进步。

先上完成后的代码。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
     <style>
         .section{
            position:relative;
             font-size: 14px;
         }
         .clearfix::after{
            content: "";
            display:block;
            height: 0;
            clear: both;
         }
         .tab{

             position: relative;
             margin: 0;
             padding: 0;
             text-align: center;

         }
         .singer{
            float: left;
            width: 100px;
            height: 30px;
            line-height: 30px;
            border: 1px solid #ccc;
            margin-left: -1px;  /*重叠边距,美观*/
            background: linear-gradient(#fefefe, #ededed);
            list-style: none;
             cursor: pointer;
         }
         .on{
            border-bottom:0 ;
            background:#fff
         }
         .songs{
             position: absolute;
             width:403px;
             height: 300px;
             margin-top: -1px;  /*重叠边距,美观*/
             margin-left: -1px;
             border: 1px solid #ccc;
             font-size:12px;
             text-indent: 14px;
             opacity: 0;
         }
         .show{
             opacity: 1;
             border-top:#fff;
         }

     </style>
</head>
<body>
    <section>
        <ul class="tab clearfix" id="tab">
            <li class="singer on">周杰伦</li>
            <li class="singer">李健</li>
            <li class="singer">薛之谦</li>
            <li class="singer">林俊杰</li>
        </ul>
        <div class="songs show">
            <p>退后、本草纲目、菊花台、青花瓷</p>
        </div>
        <div class="songs">
            <p>假如爱有天意、我愿人长久、童年、春风十里不如你</p>
        </div>
        <div class="songs">
            <p>丑八怪、绅士、演员、方圆几里</p>
        </div>
        <div class="songs">
            <p>江南、杀手、醉赤壁、一千年以后</p>
        </div>
    </section>
</body>
</html>

上面是HTML/CSS代码,没什么大问题。

接下来切换效果,我们用jQuery实现,这个过程够折腾,一把辛酸泪。

用jQuery的时候一定不要和原生js混用!用jQuery的时候一定不要和原生js混用!用jQuery的时候一定不要和原生js混用!重要的事情说三遍!

不然,就是下面这种效果....

<script>
        //思路混杂,原生js和jQuery混用,丢死人了...
        $(document).ready(function(){
            var singers=$(".singer");
            var songs=$(".songs");
            var index=0;
            var len=singers.length;
            for(var i=0;i<len;i++){
                singers[i].onmouseover=function(){
                singers.eq(index).removeClass("on");
                songs.eq(index).removeClass("show");
                singers.eq(i).addClass("on");
                songs.eq(i).addClass("show");
                index=i;
                }
            }
        })
    </script>

所以运行的时候那画面太美我不敢看,程序员有三宝:一看、二查、三请教。崩溃的我于是跑去找大神了,大神一扫代码,三下五除二给了我几行代码,效果搞定!

下面是大神的代码修改版。

<script src="http://libs.baidu.com/jquery/1.11.3/jquery.min.js"></script>
    <script>
        $(function(){
        // 先选择好所有的 菜单li 和 面板div
        var li = $(‘li.singer‘),
        song = $(‘div.songs‘);

        // mouse指针位菜单li上方时的事件
        li.mouseover(function(){
            // 当前是第几个li元素
            var n = $(this).index();

            // 先让菜单和面板恢复成默认
            li.removeClass("on");
            song.removeClass("show");

            // 再分别加上启用的效果
            li.eq(n).addClass("on");
            song.eq(n).addClass("show");
        });
        });
    </script>

大神就是大神,佩服得五体投地。小白仰望星星眼~

当然,我不想一字不差照抄大神的代码,于是我在在这个基础上继续改善完成了我的代码,再添加一个淡入效果。

于是最终完成版是这样子滴。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
     <style>
         .section{
            position:relative;
             font-size: 14px;
         }
         .clearfix::after{
            content: "";
            display:block;
            height: 0;
            clear: both;
         }
         .tab{

             position: relative;
             margin: 0;
             padding: 0;
             text-align: center;

         }
         .singer{
            float: left;
            width: 100px;
            height: 30px;
            line-height: 30px;
            border: 1px solid #ccc;
            margin-left: -1px;  /*重叠边距,美观*/
            background: linear-gradient(#fefefe, #ededed);
            list-style: none;
             cursor: pointer;
         }
         .on{
            border-bottom:0 ;
            background:#fff
         }
         .songs{
             position: absolute;
             width:403px;
             height: 300px;
             margin-top: -1px;  /*重叠边距,美观*/
             margin-left: -1px;
             border: 1px solid #ccc;
             font-size:12px;
             text-indent: 14px;
             opacity: 0;
         }
         .show{
             opacity: 1;
             border-top:#fff;
         }

     </style>
</head>
<body>
    <section>
        <ul class="tab clearfix" id="tab">
            <li class="singer on">周杰伦</li>
            <li class="singer">李健</li>
            <li class="singer">薛之谦</li>
            <li class="singer">林俊杰</li>
        </ul>
        <div class="songs show">
            <p>退后、本草纲目、菊花台、青花瓷</p>
        </div>
        <div class="songs">
            <p>假如爱有天意、我愿人长久、童年、春风十里不如你</p>
        </div>
        <div class="songs">
            <p>丑八怪、绅士、演员、方圆几里</p>
        </div>
        <div class="songs">
            <p>江南、杀手、醉赤壁、一千年以后</p>
        </div>
    </section>
    <script src="http://libs.baidu.com/jquery/1.11.3/jquery.min.js"></script>
    <script>
       $(function(){
           var singers=$(".singer");
           var songs=$(".songs");
           var p=$("p");

           singers.mouseover(function(){
               var n=$(this).index();
               singers.removeClass("on");
               songs.removeClass("show");
               p.hide();

               singers.eq(n).addClass("on");
               songs.eq(n).addClass("show");
               p.eq(n).fadeIn();

           })
       })

    </script>
</body>
</html>

每天进步一点点。

---恢复内容结束---

时间: 2024-08-28 14:34:26

tab 选项卡(初用jQuery)的相关文章

【技术】同页面可多次使用的jQuery tab选项卡代码

        <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>同页面可多次使用的jQuery tab选项卡代码</title><style type="text/css"> *{ margin:0; pad

tytabs.jquery.min.js实例,渐变的TAB选项卡特效

<!DOCTYPE html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>唐山塑钢门窗</title><script type="text/javascript" src="/ajaxjs/jquery-1.6.2.min.js"><

Jquery tab 选项卡 无刷新切换

转载的 演示地址:http://www.freejs.net/demo/29/index.html 首页>>TAB标签>>jquery实现简单的Tab切换菜单(2013-09-09) jquery实现简单的Tab切换菜单 演示 js代码 JavaScript Code <script type="text/javascript"> $(document).ready(function() { //Default Action $(".tab

Jquery 简单的Tab选项卡特效

<!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-

jquery tab选项卡

<!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-Typ

jQuery Tab选项卡切换代码

jQuery Tab选项卡切换代码是一款简单的jquery tab标签 选项卡切换代码样式,可以修改tab选项卡相关样式. 下载地址:http://www.huiyi8.com/sc/10863.html(转载请注明此处)

jQuery封装tab选项卡组件(自定义自动功能和延迟显示功能)

效果图 tab.html <!DOCTYPE html> <html lang="zh-CN"><!-- 设置简体中文 --> <head> <meta charset="UTF-8"> <title>tab</title> <link rel="stylesheet" href="../css/base.css"> <lin

jQuery实现Tab选项卡切换

需求: 实现Tab选项卡切换,鼠标悬浮在标签上,进行Tab页面的切换 此处使用的是jQuery1.7.js 实现代码如下 <!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/xh

【Little Demo】左右按钮tab选项卡双切换

通过前一篇文章 从简单的Tab标签到Tab图片切换 的说明,相关效果也就可以实现了. 1.左右按钮tab选项卡双切换 很明显,左右两个按钮是 absolute 布局,另外就是内容部分和Tab标签部分. 1) 先实现Tab内容和标签部分的显示: HTML代码: <div class="tab-Infomations"> <div class="arrows"></div> <div class="tab-conten

可以自动切换的tab选项卡实现过程详解

可以自动切换的tab选项卡实现过程详解:关于选项卡大家一定不会陌生,应用非常的频繁,通常选项卡都是需要点击或者划过才能够实现切换.本章节分享一个能够实现自动切换的选项卡功能,并给出它的具体实现过程.代码实例如下: <!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta name="author" content="http://www.so