简体   繁体   English

如何将div注入到按窗口大小调整的网页中?

[英]How can I inject a div into a webpage that moves on window resize?

Essentially, I'm creating a custom click & drag selection box. 本质上,我正在创建一个自定义的单击并拖动选择框。 The problem is that the div is position absolutely, so it will scroll with the page, but it will not move with the page when the window is being resized. 问题是div的位置是绝对的,因此它会随着页面滚动,但是在调整窗口大小时它不会随页面移动。 My attempted solution was to listen to the window resize, and move the div according to the change. 我尝试的解决方案是听窗口调整大小,然后根据更改移动div。 The problem is that it will SEEM to work, but it will not move entirely accurately, so it will slowly move out of place if the window is resized slowly, or quickly move out of place if the window is resized quickly. 问题在于它将使SEEM正常工作,但不能完全准确地移动,因此,如果缓慢调整窗口大小,它将缓慢移出适当位置;如果迅速调整窗口大小,它将迅速移出适当位置。 It seems that the resize listener does not capture every resize event. 调整大小侦听器似乎无法捕获每个调整大小事件。 I've narrowed the code down to the concept I'm using. 我已将代码缩小到我正在使用的概念。

Try injecting this script into a page (I'm using the Chrome console and I haven't made any attempt for cross-compatibility because this will be used in a Chrome extension). 尝试将此脚本注入到页面中(我使用的是Chrome控制台,但尚未尝试进行交叉兼容性,因为它将在Chrome扩展程序中使用)。 It will attempt to resize only when the scrollbar is not active, to replicate the behavior of the page content. 仅当滚动条不处于活动状态时,它才会尝试调整大小,以复制页面内容的行为。 The client and scoll variables are interchangeable for recording the change in dimensions, but they are both there for testing purposes. client和scoll变量可以互换,以记录尺寸的变化,但是它们都用于测试目的。 I would love to see a solution which solves this problem using styling attributes. 我希望看到使用样式属性解决此问题的解决方案。 Thanks for your help! 谢谢你的帮助!

var div = document.createElement("div");
div.style.position = "absolute";
div.style.backgroundColor = "#000";
div.style.width = div.style.height = div.style.left = div.style.top = "200px";
document.body.appendChild(div);

// get the highest z index of the document
function highestZIndex() {
    var elems = document.getElementsByTagName("*");
    var zIndex = 0;
    var elem, value;
    for (var i = 0; i < elems.length; i++) {
        value = parseInt(document.defaultView.getComputedStyle(elems[i], null).zIndex, 10);
        if (value > zIndex) {
            zIndex = value;
            elem = elems[i];
        }
    }
    return {
        elem: elem,
        zIndex: zIndex
    };
}

// set the div on top if it is not already
var highestZ = highestZIndex();
if (highestZ.elem != div) div.style.zIndex = highestZ.zIndex + 1;

// last width & height of client & scroll to calculate the change in dimensions
var clientWidth = document.body.clientWidth;
var clientHeight = document.body.clientHeight;
var scrollWidth = document.body.scrollWidth;
var scrollHeight = document.body.scrollHeight;

// move the div when the window is being resized
function resizeListener() {
    var _clientWidth = document.body.clientWidth;
    var _clientHeight = document.body.clientHeight;
    var _scrollWidth = document.body.scrollWidth;
    var _scrollHeight = document.body.scrollHeight;
    // horizontal scrollbar is not enabled
    if (_scrollWidth <= _clientWidth) {
        div.style.left = parseInt(div.style.left.replace(/px/, ''), 10) + (_scrollWidth - scrollWidth) / 2 + 'px';
    }
    // vertical scrollbar is not enabled
    if (_scrollHeight <= _clientHeight) {
        div.style.top = parseInt(div.style.top.replace(/px/, ''), 10) + (_scrollHeight - scrollHeight) / 2 + 'px';
    }
    clientWidth = _clientWidth;
    clientHeight = _clientHeight;
    scrollWidth = _scrollWidth;
    scrollHeight = _scrollHeight;
}
window.addEventListener("resize", resizeListener);

PS: Please, no jQuery solutions. PS:请,没有jQuery解决方案。

Since the resize listener isn't quite dependable with outside events, I've developed a simple "hack" to get the wanted results. 由于调整大小的侦听器对外部事件不太可靠,因此我开发了一个简单的“ hack”来获得所需的结果。 The window overflow is forced to scroll and the body width & height are set to +1 so that the scrollbar is active, in which the div will then stay in place. 强制窗口溢出滚动,并且将主体的宽度和高度设置为+1,以使滚动条处于活动状态,然后div会留在原处。 Once the resize is complete, the overflow and body dimensions are restored. 调整大小完成后,溢流和阀体尺寸将恢复。 This may not be a desired solution for others who want the div to move on a manual window resize, but I am invoking the resize from JavaScript so it works perfectly for me. 对于希望div手动调整窗口大小的其他人来说,这可能不是理想的解决方案,但是我正在从JavaScript调用调整大小,因此它非常适合我。

The script in practice: 实践中的脚本:

 var overflow, overflowX, overflowY, bodyWidth, bodyHeight;

 function startResize() {
     // store the original overflow values
     overflow = document.body.style.overflow;
     overflowX = document.body.style.overflowX;
     overflowY = document.body.style.overflowY;
     bodyWidth = document.body.style.width;
     bodyHeight = document.body.style.height;
     // force the scrollbar
     document.body.style.overflow = "scroll";
     // activate the scrollbar
     document.body.style.width = document.client.width + 1 + "px";
     document.body.style.height = document.client.height + 1 + "px";
 }

 function stopResize() {
     // restore the original overflow values; x & y are included because enabling the global overflow will update x and y
     document.body.style.overflow = overflow;
     document.body.style.overflowX = overflowX;
     document.body.style.overflowY = overflowY;
     // restore the original body width & height
     document.body.style.width = bodyWidth;
     document.body.style.height = bodyHeight;
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM