给对我如何解决这个问题感兴趣的人的快速说明。
首先,介绍一下我刚刚了解到的有关 Chrome 的一些细节。缩放插件弹出窗口需要打开它的OptionsChrome 的窗口Settings并放大或缩小。然后,即使当Options页面关闭后,相应的插件将零售缩放。要恢复它,只需恢复缩放Options页。酷啊,哈哈!遗憾的是,许多插件的设计并不能正确处理它。正如我在最初的问题中提到的,大多数都会显示看起来很奇怪的滚动条或只是扭曲内容。
以下是我在插件中克服这个问题的方法:
首先,您需要确定当前的缩放比例弹出窗口。 (以下仅在Chrome上测试,摘自这个帖子 https://stackoverflow.com/a/16091319/843732):
function getPageZoom()
{
//RETURN: 1.0 for 100%, and so on
var zoom = 1;
try
{
var svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
svg.setAttribute('xmlns', 'http://www.w3.org/2000/svg');
svg.setAttribute('version', '1.1');
document.body.appendChild(svg);
zoom = svg.currentScale;
document.body.removeChild(svg);
}
catch(e)
{
console.error("Zoom method failed: " + e.message);
}
return zoom;
}
然后创建一个可滚动的DIV
放置你的弹出窗口如果滚动的话你会满意的内容:
#mainSection{
margin: 0;
padding: 0;
overflow-y: auto; /* The height will be defined in JS */
}
<div id="mainSection">
</div>
然后设置可滚动DIV
使用页面缩放进行小缩放计算的最大高度。 DOM 加载后立即执行此操作,例如onLoad()
事件,或者在 jQuery 中$(function(){});
:
//Get page zoom
var zoom = getPageZoom();
//Using jQuery
var objMain = $("#mainSection");
//Calculate height & offsets of elements inside `mainSection`
//using jQuery's offset() and outerHeight()
//Make sure to multiply results returned by zoom
var offsetElement1 = $("someElement1").offset().top * zoom;
var heightElement2 = $("someElement2").outerHeight() * zoom;
//Apply the calculations of the height (in real situation you'll obviously do more...)
var height = offsetElement1 + heightElement2;
//And finally apply the calculated height by scaling it back down
var scaledHeight = height / zoom;
//Need to convert the result to an integer
scaledHeight = ~~scaledHeight;
//And set it
objMain.css("max-height", scaledHeight + 'px');
当用户选择更大的缩放时,所有这些都应该仅在您想要的位置显示一个漂亮的垂直滚动条。
最后,您需要确保如果用户开始缩放扩展程序的Options页面,而您的弹出窗口显示,需要关闭。我选择了这个方法:
$(window).resize(function()
{
var zoom = getPageZoom();
//Multiply zoom by 100 (to round it to 2 decimal points) and convert to int
var iZoom = zoom * 100;
iZoom = ~~iZoom;
if(window.izoom &&
iZoom != window.izoom)
{
//Close popup
window.close();
}
window.izoom = iZoom;
});