MatchMedia.js 是否包含在 Modernizr JS 中?

发布于 2024-12-22 14:13:24 字数 1408 浏览 2 评论 0原文

我目前正在使用

<script src="https://raw.github.com/paulirish/matchMedia.js/master/matchMedia.js"></script>

<!--    <script>

</script>-->

    <script>
        if (matchMedia('only screen and (min-width : 1025px) and (max-width : 2048px)').matches) {
  // smartphone/iphone... maybe run some small-screen related dom scripting?



    $( document ).ready( function() {
                var $body = $('body'); //Cache this for performance

                var setBodyScale = function() {
                    var scaleFactor = 0.5,
                        scaleSource = $body.width(),
                        maxScale = 150,
                        minScale = 10;

                    var fontSize = scaleSource * scaleFactor; //Multiply the width of the body by the scaling factor:

                    if (fontSize > maxScale) fontSize = maxScale;
                    if (fontSize < minScale) fontSize = minScale; //Enforce the minimum and maximums

                    $('body').css('font-size', fontSize + '%');
                }

                $(window).resize(function(){
                    setBodyScale();
                });

                //Fire it when the page first loads:
                setBodyScale();
            });
            }
            </script>

Now,如果我将 matchmedia.js 替换为 Modernizr.JS 那么我上面的代码可以工作吗?

I'm currently using

<script src="https://raw.github.com/paulirish/matchMedia.js/master/matchMedia.js"></script>

<!--    <script>

</script>-->

    <script>
        if (matchMedia('only screen and (min-width : 1025px) and (max-width : 2048px)').matches) {
  // smartphone/iphone... maybe run some small-screen related dom scripting?



    $( document ).ready( function() {
                var $body = $('body'); //Cache this for performance

                var setBodyScale = function() {
                    var scaleFactor = 0.5,
                        scaleSource = $body.width(),
                        maxScale = 150,
                        minScale = 10;

                    var fontSize = scaleSource * scaleFactor; //Multiply the width of the body by the scaling factor:

                    if (fontSize > maxScale) fontSize = maxScale;
                    if (fontSize < minScale) fontSize = minScale; //Enforce the minimum and maximums

                    $('body').css('font-size', fontSize + '%');
                }

                $(window).resize(function(){
                    setBodyScale();
                });

                //Fire it when the page first loads:
                setBodyScale();
            });
            }
            </script>

Now if I replace matchmedia.js to Modernizr.JS then will my above code work ?

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

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

发布评论

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

评论(1

情痴 2024-12-29 14:13:24

Modernizr 使用类似的称为 mq 的东西。这是文档:http://www.modernizr.com/docs/#mq

基本上,您可以将线路更改为:

if (Modernizr.mq('only screen and (min-width : 1025px) and (max-width : 2048px)')) {

Modernizr uses something similar called mq. Here's the documentation: http://www.modernizr.com/docs/#mq.

Basically, you'd change your line to:

if (Modernizr.mq('only screen and (min-width : 1025px) and (max-width : 2048px)')) {
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文