尽管有浏览器缩放级别,仍保持div大小(相对于屏幕)

时间:2010-09-12 00:40:42

标签: iphone html css zoom css-float

我正在为我的iPhone创建一个书签,它会在屏幕的左上角添加一个滚动菜单。我遇到的问题是,如果我访问一个可以放大的网站,当我放大我的“浮动”div时我会放大页面。有没有办法让我的div宽0.2英寸(用卷尺测量,可以这么说)无论我放大Safari浏览器有多远?

谢谢, 奥利弗

3 个答案:

答案 0 :(得分:2)

这是我最终提出的解决方案(有很多评论。)

//This floating div function will cause a div to float in the upper right corner of the screen at all times. However, it's not smooth, it will jump to the proper location once the scrolling on the iPhone is done. (On my Mac, it's pretty smooth in Safari.) 

function flaotingDiv(){

    //How much the screen has been zoomed.
    var zoomLevel = ((screen.width)/(window.innerWidth));
    //By what factor we must scale the div for it to look the same.
    var inverseZoom = ((window.innerWidth)/(screen.width));
    //The div whose size we want to remain constant.
    var h = document.getElementById("fontSizeDiv");

    //This ensures that the div stays at the top of the screen at all times. For some reason, the top value is affected by the zoom level of the Div. So we need to multiple the top value by the zoom level for it to adjust to the zoom. 
    h.style.top = (((window.pageYOffset) + 5) * zoomLevel).toString() + "px";

    //This ensures that the window stays on the right side of the screen at all times. Once again, we multiply by the zoom level so that the div's padding scales up.
    h.style.paddingLeft = ((((window.pageXOffset) + 5) * zoomLevel).toString()) + "px";

    //Finally, we shrink the div on a scale of inverseZoom.
    h.style.zoom = inverseZoom;

}

//We want the div to readjust every time there is a scroll event:
window.onscroll = flaotingDiv;

答案 1 :(得分:0)

您可以使用不受缩放影响的百分比位置和宽度。

Catch browser's "zoom" event in JavaScript

答案 2 :(得分:0)

Safari iPhone - How to detect zoom level and offset?

您可能还想查看此问题的答案,并提供有关如何实际计算缩放级别的信息(尽管建议不要依赖它来处理重要事项)。如果其他所有内容都失败了,拥有缩放值,您可以编写一个小脚本来根据它调整div的大小。

希望这有帮助。