带jQuery的toggle()一个animate() [英] toggle() an animate() with jquery

查看:135
本文介绍了带jQuery的toggle()一个animate()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个非常简单的问题.我基本上知道我想要什么以及怎么做,我不确定在特定部分使用什么选择器.如果我没有使用toggle()函数,动画效果会很好,但是只能打开菜单,而不能关闭菜单.

this is a super easy question. I know basically what I want and how to do it, I'm jusy not sure what selector to use in a certain part. The animation works fine if I take away the toggle() function, but it only works to open the menu, not to close it.

这是我的jquery代码

Here is my jquery code

$(document).ready(function(){
    $('#menu_button').click(function(){
        $(????).toggle(function(){
            $('.sidebar_menu').animate({left:'0'}, 'slow');
            $('.wrapper').animate({'margin-left':'250px'}, 'slow');
        });

    })
});

这是我的页面代码:

<div class="sidebar_menu">
        <h2>Menu</h2>
        <h3>1. Escolher produto</h3>
        <ul>
            <li>Entradas</li>
            <li>Peixe</li>
            <li>Saladas</li>
        </ul>
        <h3>2. Preencher dados</h3>
        <h3>3. Finalizar pedido</h3>
    </div>

    <div class="wrapper">
            <header>
                <button id="menu_button">menu</button>
            </header>
   </div>

我的CSS:

html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed, 
figure, figcaption, footer, header, hgroup, 
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
    margin: 0;
    padding: 0;
    border: 0;
    font-size: 100%;
    font: inherit;
    vertical-align: baseline;
}
/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure, 
footer, header, hgroup, menu, nav, section {
    display: block;
}
body {
    line-height: 1;
}
blockquote, q {
    quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
    content: '';
    content: none;
}
table {
    border-collapse: collapse;
    border-spacing: 0;
}


/**MAIN STYLES**/
body {
    height: 100%;
    width: 100%;
    background-color:#C00;
}
.sidebar_menu {
    width: 25%;
    height: 100%;
    display:block;
    position:fixed;
    left: 0;
    top: 0;
    background-color: #666;
}
.wrapper {
    width: 75%;
    float:left;
    margin-left: 25%;
    background-color:#C00;
}
.menu_button {
    display:none;
    }

    .slideIn {
        background-color: #090;
    }

@media screen and (max-width: 50em){
    .sidebar_menu {
        width: 250px;
        left: -250px;
    }
    .wrapper {
        width: 100%;
        margin-left: 0;
    }
    #menu_button {
        display:block;
    }
}

推荐答案

这是一个带有正确工作代码的小提琴: http://jsfiddle .net/6zg7K/1/

Here is a fiddle with properly working code: http://jsfiddle.net/6zg7K/1/

您需要在adeneo提供的代码中添加$('.sidebar_menu').animate({'left':(state ? -250: 0)}, 'slow');,因此您的整个功能应为:

You need to add $('.sidebar_menu').animate({'left':(state ? -250: 0)}, 'slow'); to the code provided by adeneo, so your entire function would be:

$('#menu_button').on('click', function(){
    var state = parseInt($('.wrapper').css('margin-left'),10) > 200;
    $('.sidebar_menu').animate({'left':(state ? -250: 0)}, 'slow');
    $('.wrapper').animate({'margin-left': (state ? 0 : 250)}, 'slow');
});

此代码是下面adeneo的修改版本.所有代码功劳都归功于他.

This code is a modified version of adeneo's below. All credit for code goes to him.

这篇关于带jQuery的toggle()一个animate()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆