如何让 jquery 与 matchmedia 一起工作?

时间:2020-12-22 20:17:28

标签: html jquery matchmedia

我尝试使此代码在小屏幕上运行。但我无法解决前 55.5em 的问题

    $(document).on('click', '#myBtn', function () {

        if ($('div.content2').hasClass("clicked")) {
            $('div.content2').animate({
                'top': "12.5em",
                'height': "42%"
            });
            $('div.content2').removeClass("clicked");
        } else {
            $('div.content2').animate({
                'height': "42%",
                'top': '32.75em'
            });
            $('div.content2').addClass("clicked");
        }


    });
    var smallScreen = window.matchMedia("(max-width:1024px)");

    $(document).on('click', '#myBtn', function (e) {
        if (smallScreen.matches) {
            if ($('div.content2').hasClass("clicked")) {
                $('div.content2').animate({
                    'top': "32.5em",
                    'height': "30%"
                }, 1000);
                $('div.content2').removeClass("clicked");
            } else {
                $('div.content2').animate({
                    'height': "auto",
                    'top': '55.5em'
                }, 1000);
                $('div.content2').addClass("clicked");
            }

        }
    });

0 个答案:

没有答案
相关问题