【问题标题】:jQuery dynamic underliningjQuery动态下划线
【发布时间】:2011-07-06 04:43:34
【问题描述】:

我的导航菜单显示/隐藏了隐藏的 div。显示/隐藏 div 时,相应的菜单选项带有下划线;表示前一个 div 淡出,当前 div 淡入,下划线紧随其后。当您通过在菜单中重新单击其名称来关闭 div 时,下划线不会隐藏。

因此理想情况下,下划线将在动态 div 切换之后动态添加/删除。

感谢您的时间和耐心!

HTML:

    <div id="nav">
        <a class="home" id="show_brand" title="THE BRAND">THE BRAND</a><br />
        <a class="home" id="show_campaigns" title="CAMPAIGNS">CAMPAIGNS</a><br />
        <a id="collection" title="COLLECTION">COLLECTION</a><br />
        <a class="home" id="show_inquiries" title="INQUIRIES">INQUIRIES</a>
    </div>
    <div class="current" id="brand">
        <div class="close"></div>
        <p>this is content</p>
    </div>
    <div id="campaigns">
        <div class="close"></div>
        <p>this is content</p>
    </div>
    <div id="inquiries">
        <div class="close"></div>
        <p>this is content</p>
    </div>

CSS:

#nav {
    width:165px;
    height:100px;
    margin-top:10px;
    color:#000;
    font-size:18px;
    font-family:FuturaStdLightCondensed;
    line-height:120%;
}
.close {
    width:16px;
    height:16px;
    top:0;
    right:0;
    margin:10px 10px 0px 0px;
    position:absolute;
    z-index:4;
    background:url(../images/close.png) no-repeat 0 0;
}
#brand {
    width:300px;
    height:188px;
    top:50%;
    left:50%;
    margin-top:-94px;
    margin-left:-150px;
    position:absolute;
}
#campaigns {
    width:160px;
    height:68px;
    top:50%;
    left:50%;
    margin-top:-34px;
    margin-left:-80px;
    position:absolute;
}
#campaigns a {
    color:#fff;
}
#inquiries {
    width:300px;
    height:500px;
    top:50%;
    left:50%;
    margin-top:-250px;
    margin-left:-150px;
    position:absolute;
}

Javascript:

$('#brand, #campaigns, #inquiries').hide();
$('.home').click(function() {
    var id = $(this).attr("id").replace("show_","").toLowerCase();
    var $content = $('#' + id + ':not(:visible)');
    console.log(id);
    if ($('.current').length === 0) {
        showContent($content)
    }
    else {
        $('.current').fadeOut(600, function() {
            showContent($content)
        });
    }
    $('.home').css('text-decoration', 'none');
    $(this).css('text-decoration', 'underline');
});
function showContent(content) {
    content.fadeIn(600);
    $('.current').removeClass('current');
    content.addClass('current');
}
$('.close').click(function() {
    $('.current').fadeOut(600);
    $('.home').css('text-decoration', 'none');
});

【问题讨论】:

    标签: jquery class toggle hidden underline


    【解决方案1】:
    $('#about, #campaigns, #inquiries').hide();
    $('.home').click(function() {
        var id = $(this).html().toLowerCase();
        var $content = $('#' + id + ':not(:visible)');
        if ($('.current').length === 0) {
            showContent($content)
        }
        else {
            $('.current').fadeOut(600, function() {
                showContent($content)
            });
        }
        $('.home').css('text-decoration', 'none');
        $(this).css('text-decoration', 'underline');  
    
    });
    function showContent(content) {
        content.fadeIn(600);
        $('.current').removeClass('current');
        content.addClass('current');
    }
    $('.close').click(function() {
        $('.current').fadeOut(600);
        $('.home').css('text-decoration', 'none');
    });
    

    【讨论】:

    • @yoda 单击时为相应的菜单项加下划线,单击其他选项之一时隐藏。单击 .close 类或通过单击其名称关闭 div 时不会消失。
    • 不要指望别人为你做这项工作。提示就在那里,你应该知道现在该做什么了:)
    • @yoda 我听到了,尤达。只是想帮助一个朋友,这只是我没有的知识。我尝试加入 $(this).css('text-decoration', 'underline');到 .close 点击功能,但无济于事。无论如何,我并不是要浪费你的时间或贪婪。整个网站都是关于社区和互相帮助的。
    • @yoda:代码和我更新的问题代码一样,除了console.log(id);在 if 语句之上。这会在从 .close 按钮​​关闭 div 时切换下划线,但在单击原始 .home 链接时不会切换它
    • @Daniel Redwood:你能粘贴一些 CSS(只需要一个),以便我可以在 jsfiddle 上进行测试吗?
    猜你喜欢
    • 1970-01-01
    • 2013-09-11
    • 1970-01-01
    • 2015-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-08
    • 2017-10-02
    相关资源
    最近更新 更多