【问题标题】:jQuery toggle with slideToggle?jQuery切换与slideToggle?
【发布时间】:2013-02-03 02:18:35
【问题描述】:

我有一个 div,当单击该 div 时,会导致我的标题图像缩小并调整标题大小。 html如下:

<div id="topmenu" class="navbar navbar-fixed-top">
    <div class="navbar-inner">
        <div class="container">
            <a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
                <i class="icon-bar"></i>
                <i class="icon-bar"></i>
                <i class="icon-bar"></i>
            </a>

                            <a class="brand logo" href="http://www.test.com/testsite/">
                                <div id="imgsize">
                    <img src="http://www.test.com/testsite/Logo-1024x335.png" alt="Logo" />
                </div>
                            </a>

            <div id="searchicons">
                <form class="navbar-search pull-right" method="get" id="searchform" action="http://www.test.com/testsite/">
                    <input type="text" class="search-query" placeholder="Search" name="s" id="s" value="">
                </form>

                <a href="http://www.test.com/testsite/feed/" title="Subscribe to our RSS Feed" class="topmenu-social pull-right"><i class="icon-rss icon-large"></i></a>

                                    <a href="http://twitter.com/#" title="Follow us on Twitter" class="topmenu-social pull-right"><i class="icon-twitter icon-large"></i></a>

                                    <a href="http://facebook.com/#" title="Find us on Facebook" class="topmenu-social pull-right"><i class="icon-facebook icon-large"></i></a>
                                </div>
        </div>
            <nav id="nav-main" class="nav-collapse" role="navigation">
                <ul id="menu-top" class="nav"><li class="menu-about"><a href="http://www.test.com/testsite/about/">About</a></li></ul>                  
            </nav>
        <div id="toggler" class="shrink visible-desktop"><a href="#"><span id="toggler_span"><i class="icon-chevron-up"></i><br>Hide</span></a></div>
    </div>
</div>

这是我的 jQuery:

<script>
$(document).ready(function() {
$('#toggler').toggle(function () {
    $('#imgsize').css('max-width','230px');
    $('#toggler_span').replaceWith('<div id="toggler_span">Show<br><i class="icon-chevron-down"></i></span>');
}, function () {
    $('#imgsize').css('max-width','1024px');
    $('#toggler_span').replaceWith('<div id="toggler_span"><i class="icon-chevron-up"></i><br>Hide</span>');
});
});
</script>

好消息是代码有效。但我想给它加点小东西,让它为图像/标题的大小调整设置动画。

标题的高度由图像的高度确定,该高度根据图像的宽度设置为自动,这是通过 jquery 更改以创建图像大小的调整。

我知道 slideToggle 但是当我将它应用到我的代码时它不会做任何事情。

我可以使用其他方法吗?

【问题讨论】:

    标签: jquery image resize toggle slidetoggle


    【解决方案1】:

    代替css 方法试试animate

    $(document).ready(function() { 
        $('#toggler').toggle(function () { 
            $('#imgsize').animate({'max-width': '230px'}, 1000);
    
            $('#toggler_span').replaceWith('<div id="toggler_span">Show<br><i class="icon-chevron-down"></i></span>'); }, function () { $('#imgsize').animate({'max-width': '1024px'}, 1000);
    
            $('#toggler_span').replaceWith('<div id="toggler_span"><i class="icon-chevron-up"></i><br>Hide</span>'); 
        }); 
    });
    

    【讨论】:

    • 它可以工作,除了在第一次单击切换 div 时,图像在“增长”到 230px 大小之前完全消失。从那时起,切换将正确地“增长”和“缩小”动画而不会消失。消失的行为如何解决?
    • 当页面首次加载时,您是否在 CSS 中为 #imgsize 设置了 max-width
    • 是的,我愿意。这有什么不同吗?
    • 不 - 它应该在那里。在你的 CSS 中,你也可以在 div #imgsize 中添加overflow: visible !important 吗?
    • 我添加了它,但同样的事情仍然发生。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多