是否可以通过 Easing 滑动切换 div 并设置最小高度?

发布于 2024-12-20 19:00:01 字数 348 浏览 4 评论 0原文

我有一个div,它使用slideToggle 和缓动来折叠和展开。

$('button').click(function () {
    $('div').slideToggle('2000', "easeOutBounce");
});

我希望它在向上滑动时具有最小高度,以便始终有一个小的可见内容。

因此,当它向下滑动时,具有 height:(div height) ,而在 slideUp 上,height:10px;

请注意,div 可以具有任意数量的高度,这就是我不使用动画的原因。

I have a div of which collapse and expands using slideToggle and easing.

$('button').click(function () {
    $('div').slideToggle('2000', "easeOutBounce");
});

I want it when it slide's Up to have a minimum height so that there always a small visible content of it.

So when it's slide down has height:(div height) and on slideUp, height:10px;

Note that the div could have any amount of height so that's why I am not using animation.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

老街孤人 2024-12-27 19:00:01

我能想到的最好的方法如下:

var toggleMinHeight = 30,
    duration = 2000,
    easing = 'swing';
$('.toggles').each(
    function(){
        $(this).attr('data-height',$(this).height());
    }).click(
    function(){
        var curH = $(this).height();
        if ($(this).is(':animated')){
            return false;
        }
        else if (curH == $(this).attr('data-height')) {
            $(this).animate(
                {
                    'height' : toggleMinHeight
                }, duration, easing);
        }
        else if (curH == toggleMinHeight){
            $(this).animate(
                {
                    'height' : $(this).attr('data-height')
                }, duration, easing);
        }
    });

JS Fiddle 演示

然而,这个演示有一些问题:

  • 除了基本 jQuery 库指定的功能之外没有缓动功能(允许访问“摇摆”和“线性”),但是可以通过包含 jQuery UI 来改进这一点。
  • 几乎可以肯定,它可以被制作成一个插件,这样看起来就不那么难看了。
  • 需要一个大的、功能齐全但不漂亮的 if/else if 语句。
  • 需要至少执行一次 each() 来分配 div 元素的“默认”/“自然”高度。
  • 如果 div 不是 position:absolute,它们会缩小到内联元素的基线(因为它们是 display: inline-block< /code>),如果它们是 float: left (或者显然是 float: right),这可能不是问题。

希望它有用,但就目前的状态来说肯定不理想。


编辑以发布上述插件化版本(它保留了与以前相同的所有问题):

(function($) {

    $.fn.slideTo = function(slideToMin, duration, easing) {

        var slideToMin = slideToMin || 30,
            duration = duration || 500,
            easing = easing || 'linear';
        $(this)
            .attr('data-height', $(this).height())
            .click(
                function() {
                    var curH = $(this).height();
                    if ($(this).is(':animated')) {
                        return false;
                    }
                    else if (curH == $(this).attr('data-height')) {
                        $(this).animate({
                            'height': slideToMin 
                        }, duration, easing);
                    }
                    else if (curH == slideToMin) {
                        $(this).animate({
                            'height': $(this).attr('data-height')
                        }, duration, easing);
                    }
                });

        return $(this);
    };
})(jQuery);

$('.toggles').slideTo(50,1500,'swing');

JS Fiddle 演示

  1. SlideToMin:这可以是带引号的字符串,例如 '3em'、'20px',也可以是不带引号的数字,例如 30,代表您要达到的高度想要元素滑动到。如果没有提供单位,则高度默认以像素为单位。
  2. 持续时间:动画持续的毫秒数。
  3. easing:带引号的字符串,定义动画中使用的缓动类型;如果没有 jQuery UI,这要么是“线性”,要么是“摆动”。 jQuery UI 可能还有其他选项,但这尚未经过测试。如果未指定,动画默认为“线性”。 因为在带引号的字符串中使用单位会导致最终的else if 返回 false(显然,我想...叹息),请仅使用该变量的不带引号的数字参数(或编辑插件以正确处理带单位的带引号的字符串...)。

在发布本文之前,我没有意识到一个更大的问题,即插件版本仅允许动画一次迭代。我对此感到困惑,尽管也许对其他人来说这是显而易见的?

好吧,看来是 if 语句中对高度的评估。使用 3em 导致最终的else if : curH ==toggleMinHeight 返回 false。大概是由于该变量中存在单位 (em)。上述指南已被编辑,以反映不应使用这些单位。


参考文献:

The best I could come up with is the following:

var toggleMinHeight = 30,
    duration = 2000,
    easing = 'swing';
$('.toggles').each(
    function(){
        $(this).attr('data-height',$(this).height());
    }).click(
    function(){
        var curH = $(this).height();
        if ($(this).is(':animated')){
            return false;
        }
        else if (curH == $(this).attr('data-height')) {
            $(this).animate(
                {
                    'height' : toggleMinHeight
                }, duration, easing);
        }
        else if (curH == toggleMinHeight){
            $(this).animate(
                {
                    'height' : $(this).attr('data-height')
                }, duration, easing);
        }
    });

JS Fiddle demo.

This demo has some issues, however:

  • No easing functionality beyond that specified by the basic jQuery library (giving access to 'swing' and 'linear'), this could be improved by including jQuery UI, however.
  • It could almost certainly be made into a plug-in, to be somewhat less icky to look at.
  • Requires a large, functional but not pretty, if/else if statement.
  • Requires at least one pass of the each() to assign the 'default'/'natural' height of the div elements.
  • If the divs aren't position: absolute they shrink down to the baseline of the in-line elements (as they're display: inline-block), this may not be a problem if they're float: left (or float: right, obviously).

Hopefully it's of use, but it's certainly not ideal in its current state.


Edited to post the plugin-ised version of the above (it retains all the same issues as before):

(function($) {

    $.fn.slideTo = function(slideToMin, duration, easing) {

        var slideToMin = slideToMin || 30,
            duration = duration || 500,
            easing = easing || 'linear';
        $(this)
            .attr('data-height', $(this).height())
            .click(
                function() {
                    var curH = $(this).height();
                    if ($(this).is(':animated')) {
                        return false;
                    }
                    else if (curH == $(this).attr('data-height')) {
                        $(this).animate({
                            'height': slideToMin 
                        }, duration, easing);
                    }
                    else if (curH == slideToMin) {
                        $(this).animate({
                            'height': $(this).attr('data-height')
                        }, duration, easing);
                    }
                });

        return $(this);
    };
})(jQuery);

$('.toggles').slideTo(50,1500,'swing');

JS Fiddle demo.

  1. slideToMin: this can be either a quoted string, such as '3em', '20px', or an unquoted number, such as 30, and represents the height to which you want the element to slide to. If there are no units supplied then the height is, by default, considered to be in pixels.
  2. duration: the number of milliseconds for which the animation lasts.
  3. easing: a quoted string defining the type of easing to be used in the animation; without jQuery UI this is either 'linear' or 'swing'. With jQuery UI other options may be possible, but this is untested. If unspecified the animation defaults to 'linear.' Because using units in a quoted string causes the final else if to return false (obviously, I suppose...sigh), please use only an unquoted numerical argument for this variable (or edit the plug in to properly deal with quoted strings with units...).

A larger issue that I hadn't realised until I posted this, is that the plugin version only allows one iteration of the animation. Which I'm confused about, though perhaps it's obvious to someone else?

Okay, it appears to be the evaluation of the height in the if statement. Using 3em caused the final else if : curH == toggleMinHeight to return false. Presumably due to the presence of the unit (em) in that variable. The above guidance has been edited to reflect that the units should not be used.


References:

像你 2024-12-27 19:00:01

并不真地。当调用 Slide 方法时,它会获取 style 属性

display: none;

但是,您可以通过使用回调函数动态修复它

$('div').slideToggle('2000', "easeOutBounce", function() {
     $('div')[0].style.height="//whatever//"     //You could also use the min-height property
     $('div')[0].style.display="inline";
);

动画到底有什么问题?

Not really. When the slide method is called, it gets the style property

display: none;

However, you may be able to dynamically fix it by using the callback function

$('div').slideToggle('2000', "easeOutBounce", function() {
     $('div')[0].style.height="//whatever//"     //You could also use the min-height property
     $('div')[0].style.display="inline";
);

What exactly is the problem with animation?

春风十里 2024-12-27 19:00:01

我建议你玩点诡计;

为什么不在滑动 div 上方有一个具有相同宽度和颜色的 div,以便它始终显示并作为滑动部分的一部分出现

I suggest that you play it tricky;

why dont you have a div above the sliding div that has the same width and color so that it is always displayed and appears as part of the sliding part

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文