jquery - 如何将 "lie"转为 mediaquery?如何伪造窗口的宽度?

标签 jquery html css responsive-design media-queries

我正在开发响应式网站,你们都知道手动缩小浏览器窗口有点不自在(而且,Firefox 不允许我在一个值后这样做)。所以我决定编写一个 jQuery“插件”来缩小带有 - 或 + 按钮的区域。

一旦我写了这个:

$(document).ready(function() {
    var doResizing = function(increaseWith) {
        if ($('#xxxx').length == 0) {
            $('body').css('margin', 0).css('padding', 0);
            $('body > *').wrapAll('<div id="xxxx" /></div>');

            $('#xxxx').css('background-color', 'red')
                .css('overflow', 'scroll')
                .css('padding', 0)
                .css('margin', 0)
                .css('position', 'absolute')
                .width('100%');
        }
        $('#xxxx').height(parseInt($(window).height()) + 'px').width(parseInt($('#xxxx').width())+increaseWith + 'px');
    }

    $(document).keypress(function(e) {
        if (e.which == 45) {
            doResizing (-10);
        }

        if (e.which == 43) {
            doResizing (+10);
        }
    });
});

它可以检查,但即使媒体查询的定义正确,它也不会购买。那么如何在不实际调整窗口大小的情况下告诉媒体查询宽度已更改?

最佳答案

问题是媒体查询仍然以全宽显示页面。您可以稍微调整代码以获取页面并将其嵌入到 iframe 中并调整 iFrame 的大小:

var doResizing = function (increaseWith) {
     if ($('#xxxx').length == 0) {
            $('body').css('margin', 0).css('padding', 0);
            $('body > *').wrapAll('<iframe id="xxxx" src="' + window.Location + '" /></iframe>');
            $('#xxxx').css('background-color', 'red').css('overflow', 'scroll').css('padding', 0).css('margin', 0).css('position', 'absolute').width('100%');
    }
    $('#xxxx').height(parseInt($(window).height()) + 'px').width(parseInt($('#xxxx').width()) + increaseWith + 'px');
}

关于jquery - 如何将 "lie"转为 mediaquery?如何伪造窗口的宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21572732/

相关文章:

javascript - 如何将 anchor 标记的名称分配给 JS 中的变量?

html - HTML 中的发音

html - 如何在 twitter bootstrap 容器内设置 100% 宽度的背景?

html - 带有 br 标签的表格单选按钮

css - 位置 : fixed with vertical space

javascript - 使用 PHP inside string 作为字符串。 (查询)

javascript - Arbor js - 将鼠标移到节点上时将节点 ID 显示为文本

php - 如何使用PHP和jquery获取jsonp?

javascript - 为什么我的 angularjs 多选界面不起作用?

javascript - 在 Chrome Devtools 中监听对 CSS 类所做的更改