slideToggle附加文本交换

时间:2013-02-04 18:40:50

标签: jquery jquery-plugins

有一点时间让+更改为 - 使用以下代码进行slideToggle操作。

jQuery('.mean-nav ul ul').show();
if(meanToggleChildren){
    jQuery('.mean-nav ul ul').hide();
    if(jQuery('.mean-nav ul ul').length){
        jQuery('<span class="mm-open-close">+</span>').insertBefore(jQuery('.mean-nav ul ul'));
        jQuery('.mean-nav ul li').click(function(){
            jQuery(this).find('ul').slideToggle('fast', function(){
                $('.mm-open-close').text().toLowerCase().indexOf('-') != -1 ? $('.mm-open-close').text('+') : $('.mm-open-close').text('-');
            });
        });
    }
}

它正在正确的位置插入+,然而,交换似乎无法正常工作,因为(根据下面的html)它交换了+

这是使用jQuery 1.7.1库,meanMenu Responsive Nav插件和以下HTML:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style type="text/css">
/* hide the link until viewport size is reached */
a.meanmenu-reveal {
display: none;
}

/* when under viewport size, .mean-container is added to body */
.mean-container .mean-bar { 
float: left;
width: 100%;
position: relative;
background: #0c1923;
padding: 4px 0;
min-height: 33px;
z-index:9000;
}
.mean-container a.meanmenu-reveal {
width: 22px;
height: 22px;
padding: 10px 10px 8px 10px;
position: absolute;
top: 0;
right: 0;
cursor: pointer;
color: #fff;
text-decoration: none;
font-size: 16px;
text-indent: -9999em;
line-height: 22px;
font-size: 1px;
display: block;
font-family: Arial, Helvetica, sans-serif;
font-weight: 700;
}
.mean-container a.meanmenu-reveal span {
display: block;
background: #fff;
height: 3px;
margin-top: 3px;
}
.mean-container .mean-nav { 
float: left;
width: 100%;
background: #0c1923;
margin-top: 33px;
}
.mean-container .mean-nav ul {
width: 100%;
}
.mean-container .mean-nav ul li a {
display: block;
float: left;
width: 90%;
padding: .5em 5%;
margin: 0;
text-align: left;
color: #fff;
border-top: 1px solid #383838;
border-top: 1px solid rgba(255,255,255,0.5);
text-decoration: none;
text-transform: uppercase;
}
.mean-container .mean-nav ul li:first-child a { 
border: none; 
}
.mean-container .mean-nav ul li li a {
width: 80%;
padding: .5em 10%;
border-top: 1px solid #f1f1f1;
border-top: 1px solid rgba(255,255,255,0.25);
opacity: 0.75; 
filter: alpha(opacity=75);
text-shadow: none !important;
visibility: visible;
}
.mean-container .mean-nav ul li li li a {
width: 70%;
padding: .5em 15%;
}
.mean-container .mean-nav ul li li:first-child a {
border-top: 1px solid #f1f1f1;
border-top: 1px solid rgba(255,255,255,0.25);
}
.mean-container .mean-nav ul li a:hover { 
background: #252525;
background: rgba(255,255,255,0.1); 
}
.mean-container .mean-push { 
float: left;
width: 100%;
padding: 0;
margin: 0;
clear: both; 
}
.mm-open-close{color:#fff;float:left;}
</style>
</head>
<body>
<header>
<nav>
    <ul>
        <li><a>Test</a></li>
        <li><a>Test</a></li>
        <li><a>Test</a>
            <ul>
                <li><a>Test</a></li>
                <li><a>Test</a></li>
                <li><a>Test</a></li>
                <li><a>Test</a></li>
                <li><a>Test</a></li>
            </ul>
        </li>
        <li><a>Test</a>         <ul>
                <li><a>Test</a></li>
                <li><a>Test</a></li>
                <li><a>Test</a></li>
                <li><a>Test</a></li>
                <li><a>Test</a></li>
            </ul>
        </li>

        <li><a>Test</a></li>
        <li><a>Test</a></li>
    </ul>
</nav>
</header>

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.js"></script>
<script type="text/javascript" src="/mean.menu.js"></script>
<script type="text/javascript">
    jQuery(document).ready(function($){
        $('header nav').meanmenu({
            meanScreenWidth:        800, 
            meanRevealPosition:     "left",
            meanShowChildren:       true,
            meanToggleChildren:     true
        });
    });
</script>
</body>
</html>

当切换部分时,如何正确地交换+?

2 个答案:

答案 0 :(得分:1)

这就是我在插件本身中所做的事情(今天在https://github.com/weare2ndfloor/meanMenu上发布的新版本)。

// hide sub nav
                   if(meanShowChildren) {
                        // allow expandable sub nav(s)
                       if(meanExpandableChildren){
                           jQuery('.mean-nav ul ul').each(function() {
                               if(jQuery(this).children().length){
                                   jQuery(this,'li:first').parent().append('<a class="mean-expand" href="#" style="font-size: '+ meanMenuCloseSize +'">'+ meanExpand +'</a>');                               
                               }
                           });
                           jQuery('.mean-expand').on("click",function(e){
                                e.preventDefault();
                               if (jQuery(this).hasClass("mean-clicked")) {
                                    jQuery(this).text(meanExpand);
                                    console.log("Been clicked");
                                   jQuery(this).prev('ul').slideUp(300, function(){

                                   });
                               } else {
                                    jQuery(this).text(meanContract);
                                    jQuery(this).prev('ul').slideDown(300, function(){
                                    });
                               }   
                               jQuery(this).toggleClass("mean-clicked"); 
                           });     
                       } else {
                           jQuery('.mean-nav ul ul').show();   
                       }
                   } else {
                       jQuery('.mean-nav ul ul').hide();
                   }

显然,我的功能名称不同。与您的主要区别在于只有+是可点击的。

您需要从上面的链接中获取新的CSS和插件。

答案 1 :(得分:0)

找到并修复它:

if(meanShowChildren) {
    if(meanToggleChildren){
        //check to make sure that there are children first off...
        if(jQuery('.mean-nav ul ul').length){
            jQuery('.mean-nav ul ul').prev().prepend('<span class="mm-open-close">+</span>');                               
            jQuery('.mean-nav ul li').click(function(){
                jQuery(this).find('ul').slideToggle('fast', function(){
                    var tObj = jQuery(this).prev().find('.mm-open-close');
                    tObj.text().toLowerCase().indexOf('-') != -1 ? tObj.text('+') : tObj.text('-');
                });
            });                             
        }
    } else {
        jQuery('.mean-nav ul ul').show();   
    }
} else {
    jQuery('.mean-nav ul ul').hide();
}