【问题标题】:What is alternative to use after jQuery 1.9 removed .toggle(function,function)?在 jQuery 1.9 删除 .toggle(function,function) 后有什么替代方法?
【发布时间】:2013-01-07 14:18:09
【问题描述】:

您好,我正在尝试创建在每次点击时会交替缩小和增长的东西,但我在我的网站上使用jQuery 1.9.toggle(function,function) 函数已从 jQuery 1.9 中删除,所以我不确定应该改用什么。

任何帮助都会很棒。

谢谢

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

新的 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>

【问题讨论】:

  • 可以在这里找到一些替代方法:forum.jquery.com/topic/… 但是,如果您发布代码,我们很可能会帮助您解决直接情况。您很可能一开始就不需要 .toggle 方法。
  • jQuery UI 库中还有一个 .toggle() 方法api.jqueryui.com/toggle
  • 好的,我已经添加了我的代码,如果这样对你来说更容易

标签: jquery css animation toggle


【解决方案1】:

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

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

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

【讨论】:

  • 这正是我所需要的。谢谢!
【解决方案2】:
$(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;
        }
    });
});

【讨论】:

    猜你喜欢
    • 2012-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-05
    • 2013-01-01
    • 1970-01-01
    • 2020-11-20
    相关资源
    最近更新 更多