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

发布于 2024-09-18 09:09:04 字数 163 浏览 5 评论 0原文

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

谢谢, 奥利弗

I'm creating a bookmarklet for my iPhone which will add a scrolling menu to the upper-left corner of the screen. The problem I've run into is that if I visit a site which can be zoomed in, when I zoom in my "floating" div becomes larger as I zoom in the page. Is there a way so that my div will be .2 inches wide (measurable with a measuring tape, so to speak) no matter how far I zoom in my Safari browser?

Thanks,
Oliver

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

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

发布评论

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

评论(3

叹倦 2024-09-25 09:09:04

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

//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;

This is the solution I finally came up with (with lots of comments.)

//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;
在风中等你 2024-09-25 09:09:04

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

在 JavaScript 中捕获浏览器的“zoom”事件

You could use percentage positions and widths, which are not affected by zoom.

Catch browser's "zoom" event in JavaScript

不必了 2024-09-25 09:09:04

Safari iPhone - 如何检测缩放级别和偏移?

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

希望这有帮助。

Safari iPhone - How to detect zoom level and offset?

You might also want to check the answers to this question as well as they provide information on how you can actually calculate the zoom level (although it's being advised against relying on it for important stuff). If everything else fails, having the zoom value, you could write a small script to adjust the size of your div based on it.

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文