在jQuery 1.9删除.toggle(函数,函数)后使用的替代方法是什么?

时间:2013-01-23 22:56:34

标签: jquery css animation toggle

您好我正在尝试创建一些可以在每次点击时交替缩小和增长的内容,但我在我的网站上使用jQuery 1.9.toggle(function,function)功能已从jQuery 1.9中移除,因此我不确定应该使用哪种功能。

任何帮助都会很棒。

由于

jsfiddle(使用旧代码,jquery 1.8版本):http://jsfiddle.net/TNAC6/

new jsfiddle(jquery 1.9):http://jsfiddle.net/TNAC6/1/

这是我正在尝试切换的代码。基本上我正在切换的是圆圈div。

(function($){
    $.fn.createToggle = function(size) {
        var ele = $(this);
        var oldSize = ele.width();
        console.log("creating new toggle on element: " + ele + " old: " + oldSize + " new: " + size );
        console.log("its content is" + ele.children(".content"));
        var growfn = function() {
            $(this).stop().animate({
                'width': size+'px',
                'height': size+'px',
                'margin-left': '-'+(size/2)+'px',
                'margin-top': '-'+(size/2)+'px'
            }, 500);
            $(this).children(".content").toggle();
        };
        var shrinkfn = function() {
            $(this).stop().animate({
                'width': oldSize+'px',
                'height': oldSize+'px',
                'margin-left': '-'+(oldSize/2)+'px',
                'margin-top': '-'+(oldSize/2)+'px'
            }, 500);
            $(this).children(".content").toggle();
        };
        ele.click(function() {
//insert code to toggle stuff
        });
    };
})(jQuery);

$(".home").createToggle(500);

和css:

.circleBase {
    -webkit-border-radius: 999px;
    -moz-border-radius: 999px;
    border-radius: 999px;
    behavior: url(PIE.htc);
}

.home {
    width: 200px;
    height: 200px;
    background: #FF5032;
    position: absolute;
    top: 300px;
    left: 300px;
    margin-left: -100px;
    margin-top: -100px;
}

.title {
    position: relative;
    padding-top: 10%;
    text-align: center;
    font-weight: bold;
    font-size: 24px;
}

.content {
    text-align: center;
    display: none;
}

和html:

<div class="circleBase home">
    <p class="title">Glen Takahashi<p>
    <p class="content">THIS IS CONTENT<BR> THIS IS CONTENT<BR> THIS IS CONTENT<BR> THIS IS CONTENT<BR> THIS IS CONTENT</p>
</div>

2 个答案:

答案 0 :(得分:6)

使用两种状态,您可以将当前切换状态保持为布尔值(我使用clickState)。如果你想拥有多个状态,你可以继续将状态计数加1,然后检查总计数的模数,以根据状态确定应该触发哪个函数。

我更新了你的代码,因为ele实际上是你想要使用的jQuery对象:

http://jsfiddle.net/TNAC6/2/

答案 1 :(得分:4)

$(document).ready(function() {
    var flag=0;
    $('selector').on('click', function(){
        var menu = $("element_to_toggle");
        if(flag==0){
            menu.text("click one");
            flag=1;
            return;
        }
        else if(flag==1){
            menu.text("click two");
            flag=0;
            return;
        }
    });
});