简体   繁体   English

如何使用JavaScript和CSS Grid根据屏幕/视口大小使RadWindow响应

[英]How to make a RadWindow responsive based on screen / viewport size using JavaScript and CSS Grid

I've been looking for a solution to make a Telerik RadWindow work responsively. 我一直在寻找使Telerik RadWindow响应式工作的解决方案。 There are two things that need to be responsive, the contents in the RadWindow, and the RadWindow itself. 需要响应的两件事是RadWindow中的内容和RadWindow本身。

Problem: 问题:

Per Telerik: " RadWindow does not support responsive size change and it does not change size automatically according to the viewport, because the behavior in such a scenario cannot be strictly defined " Per Telerik:RadWindow不支持响应大小更改,它不会根据视口自动更改大小,因为不能严格定义这种情况下的行为

Solution: (The contents inside the RadWindow need to be responsive, as does the RadWindow itself) 解决方案: (RadWindow内部的内容需要响应,RadWindow本身也需要响应)

RadWindow Contents: I used CSS Grid to make the contents of the RadWindow responsive in this example. RadWindow内容:在此示例中,我使用CSS Grid使RadWindow的内容具有响应性。

        .sectionSearch {
            display: grid;
            grid-gap: 3px;
            grid-template-columns: repeat(auto-fit, 200px);
            align-items: end;
            max-width: 809px;
        }

RadWindow Responsiveness: RadWindow的响应度:

The viewport can be set to a percentage of the screen, per this article. 视口可以设置为屏幕的百分比,每文章。 You can then use JavaScript media queries to adjust the percentages of the height and width of the viewport, depending on the size of the screen. 然后,您可以使用JavaScript媒体查询来调整视口的高度和宽度的百分比,具体取决于屏幕的大小。

Using a combination and modification of a few posts, including this one, the following sample solution worked for my needs of making a RadWindow responsive. 通过结合使用和修改一些帖子(包括这篇文章),以下示例解决方案可以满足我对RadWindow作出响应的需求。

          var $ = $telerik.$;
        var radwindow;

        function pageLoad() {
            radwindow = $find("<%= radwindow.ClientID%>");
        }

        $(window).resize(function () {
            if (radwindow.isVisible()) {
                setWindowsize();
            }
        });

        function setWindowsize() {

            //var viewportWidth = $(window).width();
            //var viewportHeight = $(window).height();

            /* ==================================================== */

            // media query event handler
            if (matchMedia) {
                var mqls = [ // array of media queries
                    window.matchMedia("(min-width: 0px) and (max-width: 399px)"),
                    window.matchMedia("(min-width: 400px) and (max-width: 600px)"),
                    window.matchMedia("(min-width: 600px) and (max-width: 800px)"),
                    window.matchMedia("(min-width: 800px) and (max-width: 1000px)"),
                    window.matchMedia("(min-width: 1000px) and (max-width: 4000px)")
                ]

                for (i = 0; i < mqls.length; i++) { // loop though media queries
                    mqls[i].addListener(WidthChange); // listen for queries
                    WidthChange(mqls[i]); // call handler func at runtime
                }
            }

            // media query change
            function WidthChange(mql) {

                if (mqls[0].matches) {
                    var viewportWidth = $(window).width();
                    var viewportHeight = $(window).height();
                    radwindow.setSize(Math.ceil(viewportWidth * 90 / 100), Math.ceil(viewportHeight * 90 / 100));
                } else if (mqls[1].matches) {
                    var viewportWidth = $(window).width();
                    var viewportHeight = $(window).height();
                    radwindow.setSize(Math.ceil(viewportWidth * 90 / 100), Math.ceil(viewportHeight * 60 / 100));
                } else if (mqls[2].matches) {
                    var viewportWidth = $(window).width();
                    var viewportHeight = $(window).height();
                    radwindow.setSize(Math.ceil(viewportWidth * 90 / 100), Math.ceil(viewportHeight * 40 / 100));
                } else if (mqls[3].matches) {
                    var viewportWidth = $(window).width();
                    var viewportHeight = $(window).height();
                    radwindow.setSize(Math.ceil(viewportWidth * 90 / 100), Math.ceil(viewportHeight * 40 / 100));
                } else if (mqls[4].matches) {
                    var viewportWidth = $(window).width();
                    var viewportHeight = $(window).height();
                    radwindow.setSize(Math.ceil(viewportWidth * 70 / 100), Math.ceil(viewportHeight * 45 / 100));
                }
            }
                radwindow.center();
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM