【问题标题】:Reverse functionality of tabs.选项卡的反向功能。
【发布时间】:2011-07-31 00:39:47
【问题描述】:

我正在尝试反转这些选项卡的方向。目前它们的高度为 150 像素,并缩小到 90 像素。我决定我希望看到它们从 90 像素变为 150 像素。我认为反转该功能会很容易,但是,这似乎行不通。有没有人有什么建议。

提前感谢您的任何帮助/建议。

<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.js"></script>
<script type="text/javascript"><!--
$(document).ready(function() { 
    var navDuration = 500; 
    var navJumpHeight = "90px";

    $('#tabs li').hover(function() {
        $(this).animate({ height : "-="+navJumpHeight }, navDuration);           
    }, function() {
        $(this).animate({ height : "150px" }, navDuration);
    });        
});
// --></script>

<style type="text/css"><!--
/* CSS Reset */
     html,body,div,ul,ol,li,dl,dt,dd,h1,h2,h3,h4,h5,h6,pre,form,p,blockquote,fieldset,input,hr{margin:0;padding:0;line-height:1em;}
h1,h2,h3,h4,h5,h6,pre,code,address,caption,cite,code,em,strong,th{font-size:1em;font-weight:normal;font-style:normal;line-height:1em;}
ul,ol{list-style:none;}
fieldset,img,hr{border:none;}
q:before,q:after{content:'';}
abbr,acronym{border:0;}
caption,th{text-align:left;}
table{border-collapse:collapse;border-spacing:0;}
td {vertical-align:top;}
html{font-size:100.01%;}
body{font-size:1em;}
a img{border: none;}

body{ font-family:Arial; font-size:10px; font-weight:500; background:#ffffff no-repeat center top; }    
    div.headerblock{ position:absolute; display:table-cell; text-align:center; left:0px; top:0px; width:100%; height:150px; }   
        div.header{ position:relative; width:900px; height:170px; margin-left:auto; margin-right:auto; }
            div.logo        { position:relative; display:table-cell; vertical-align:top; text-align:left; left:0px; top:0px; width:349px; height:170px; float:left; }


    #headerblock #header #tabs { height: 150px; overflow: hidden; padding: 0 10px; left:40; list-style: none; position: relative; filter:alpha(opacity=85); opacity:0.85; }
    #headerblock #header #tabs li, #headerblock #header #tabs li a { width: 100px; position: relative; float: left; }      
    #headerblock #header #tabs li { top: 0 px; margin: 0; background: none; padding: 0; display: block; vertical-align: bottom;}
    #headerblock #header #tabs li a { display: block; color: #ffffff; font-size: 1.7em; text-decoration: none; text-transform: uppercase; height: 150px; line-height: 1.1em}
    #headerblock #header #tabs #tab_about a   { background-color: #ff0000; }
    #headerblock #header #tabs #tab_services a{ background-color: #ffa500; }
    #headerblock #header #tabs #tab_contact a { background-color: #ffff00; }
    #headerblock #header #tabs #tab_learning a{ background-color: #00ff00; }
    #headerblock #header #tabs #tab_clients a { background-color: #0000ff; }
--></style>
</head>
<body>
<div class="headerblock" id="headerblock">
<div class="header" id="header">
    <div class="logo">
        &nbsp;
    </div>
    <ul id="tabs">
        <li id="tab_about"><a href="#">&nbsp;<br>Tab 1</a></li>
        <li id="tab_services"><a href="#">&nbsp;<br>Tab 2</a></li>
        <li id="tab_contact"><a href="#">&nbsp;<br>Tab 3</a></li>
        <li id="tab_learning"><a href="#">&nbsp;<br>Tab 4</a></li>
        <li id="tab_clients"><a href="#">&nbsp;<br>Tab 5</a></li>
    </ul>
</div >
</div>
</body>
</html>

【问题讨论】:

    标签: jquery html navigation jquery-animate


    【解决方案1】:

    仅仅颠倒数字是不够的。

    CSS

    让我们先看看你的 CSS。

    只需将 a 标记的高度更改为 90 像素的适当大小即可。 li 标记高度可以保持不变,因为您希望它保留 a 标记在悬停时应增长的 150 像素的高度。

    #headerblock #header #tabs li a
    {
        display: block;
        color: #ffffff;
        font-size: 1.7em;
        text-decoration: none;
        text-transform: uppercase;
        height: 90px;
        line-height: 1.1em
    }
    

    JavaScript

    现在您只需对 JS 进行一些更改。

    在开始时,您的锚标记是 90 像素高。您希望它增长(+= 在 mousover 动画功能中)60px 以达到 150px 的高度。

    var navJumpHeight = "60px";
    $(this).animate({ height: "+=" + navJumpHeight }, navDuration);
    

    正如您在 CSS 中看到的那样,您的 li 标记的高度保持在 150 像素。您要做的就是更改定义了 background-color 属性的 li a 标签的高度。

    $('#tabs li a').hover(function () {
    

    在鼠标移出时,您希望它再次缩小到 90 像素:

    $(this).animate({ height: "90px" }, navDuration);
    

    为了保持 mouseover 和 mouseout 函数一致并重用 navJumpHeight 变量而不是使用幻数,您可以将前一行更改为如下内容:

    $(this).animate({ height: "-=" + navJumpHeight }, navDuration);
    

    结果

    我的机器上运行了以下代码:

    <html>
    <head>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.js"></script>
        <script type="text/javascript">
        <!--
            $(document).ready(function () {
                var navDuration = 500;
                var navJumpHeight = "60px";
                $('#tabs li a').hover(function () {
                    $(this).animate({ height: "+=" + navJumpHeight }, navDuration);
                }, function () {
                    $(this).animate({ height: "-=" + navJumpHeight }, navDuration);
                });
            });
        // -->
        </script>
        <style type="text/css">
        <!--
            /* CSS Reset */    html,body,div,ul,ol,li,dl,dt,dd,h1,h2,h3,h4,h5,h6,pre,form,p,blockquote,fieldset,input,hr{margin:0;padding:0;line-height:1em;} h1,h2,h3,h4,h5,h6,pre,code,address,caption,cite,code,em,strong,th{font-size:1em;font-weight:normal;font-style:normal;line-height:1em;} ul,ol{list-style:none;}fieldset,img,hr{border:none;}q:before,q:after{content:'';}abbr,acronym{border:0;}caption,th{text-align:left;}table{border-collapse:collapse;border-spacing:0;}td {vertical-align:top;}html{font-size:100.01%;}body{font-size:1em;}a img{border: none;}body{ font-family:Arial; font-size:10px; font-weight:500; background:#ffffff no-repeat center top; }        
            div.headerblock{ position:absolute; display:table-cell; text-align:center; left:0px; top:0px; width:100%; height:150px; }           
            div.header{ position:relative; width:900px; height:170px; margin-left:auto; margin-right:auto; }            
            div.logo        { position:relative; display:table-cell; vertical-align:top; text-align:left; left:0px; top:0px; width:349px; height:170px; float:left; }    
            #headerblock #header #tabs { height: 150px; overflow: hidden; padding: 0 10px; left:40; list-style: none; position: relative; filter:alpha(opacity=85); opacity:0.85; }    
            #headerblock #header #tabs li,
            #headerblock #header #tabs li a { width: 100px; position: relative; float: left; }          
            #headerblock #header #tabs li { top: 0; margin: 0; background: none; padding: 0; display: block; vertical-align: bottom;}    
            #headerblock #header #tabs li a { display: block; color: #ffffff; font-size: 1.7em; text-decoration: none; text-transform: uppercase; height: 90px; line-height: 1.1em}    
            #headerblock #header #tabs #tab_about a   { background-color: #ff0000; }    
            #headerblock #header #tabs #tab_services a{ background-color: #ffa500; }    
            #headerblock #header #tabs #tab_contact a { background-color: #ffff00; }    
            #headerblock #header #tabs #tab_learning a{ background-color: #00ff00; }    
            #headerblock #header #tabs #tab_clients a { background-color: #0000ff; }
            -->
        </style>
    </head>
    <body>
        <div class="headerblock" id="headerblock">
            <div class="header" id="header">
                <div class="logo">&nbsp;</div>
                <ul id="tabs">
                    <li id="tab_about"><a href="#">&nbsp;<br>Tab 1</a></li>
                    <li id="tab_services"><a href="#">&nbsp;<br>Tab 2</a></li>
                    <li id="tab_contact"><a href="#">&nbsp;<br>Tab 3</a></li>
                    <li id="tab_learning"><a href="#">&nbsp;<br>Tab 4</a></li>
                    <li id="tab_clients"><a href="#">&nbsp;<br>Tab 5</a></li>
                </ul>
            </div>
        </div>
    </body>
    </html>
    

    希望这会对您有所帮助。重构空间很大!

    【讨论】:

    • 正确,目前它不能,这就是原因,但是当我反转值和/或运算符时它不起作用。我以这种方式发布它的原因是它是工作代码......我正试图让它反向工作。
    • 好吧,我编辑了我的答案,以便稍微解释一下您可能需要更改哪些部分以及原因。希望这能回答您的问题... ;)
    • 感谢 Diemo 的帮助,非常感谢。
    猜你喜欢
    • 2010-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多