javascript - 修复使用 margin-auto 时滚动条跳转的问题

标签 javascript html css

所以我正在尝试用 javascript(非 jQuery)修复滚动条跳转问题。我找到了确定滚动条是否可见的代码。我似乎无法弄清楚如何以某种方式阻止它跳跃。我不能使用 overflow-y: scroll;,因为它会破坏设计。

我不想使用任何其他人的库。

我正在考虑在左侧插入一个与我的滚动条宽度相同的不可见 div,以便它居中中间 div,但它没有用,因为它搞砸了 margin-left: automargin-right:auto

这个问题快把我逼疯了!

html:

<div class="header">header</div>
<div class="container">
  <div id="info"></div>
  herro<br>
  herro<br>
  herro<br>
  herro<br>
  herro<br>
  herro<br>
  herro<br>
  herro<br>
  herro<br>
  herro<br>
  herro<br>
  herro<br>
  herro<br>
  herro<br>
  herro<br>
</div>

CSS:

html, body {
  margin: 0;
}

.header {
  width: 100%;
  background:lightblue;
}

.container {
  max-width: 1200px;
  margin-left: auto;
  margin-right: auto;
  padding-left: 50px;
  padding-right: 50px;
  background: red;
}

js:

(function (window, document) {
    window.addEventListener('resize', function resized() {
        var scrollHeight = document.body.scrollHeight;
        var clientHeight = document.documentElement.clientHeight;
        var hasVerticalScrollbar = scrollHeight > clientHeight;
        if (hasVerticalScrollbar) {
            document.getElementById('info').innerHTML = "scrollbar";
        } else {
            document.getElementById('info').innerHTML = "!scrollbar";
        }
    });

    var iframe = document.createElement('iframe');
    iframe.id = "hacky-scrollbar-resize-listener";
    iframe.style.cssText = 'height: 0; background-color: transparent; margin: 0; padding: 0; overflow: hidden; border-width: 0; position: absolute; width: 100%;';

    iframe.onload = function () {
        iframe.contentWindow.addEventListener('resize', function () {
            try {
                var evt = document.createEvent('UIEvents');
                evt.initUIEvent('resize', true, false, window, 0);
                window.dispatchEvent(evt);
            } catch (e) {}
        });
    };

    document.body.appendChild(iframe);
})(window, document);

最佳答案

我想通了。制作了一个包装 div,中间的 div 位于其中。然后,当滚动条出现/消失时,我调整包装器的 margin-left 属性。

http://codepen.io/basickarl/pen/rxGRXR

HTML:

<div class="header">header</div>
<div id="wrapper">
  <div class="container">
    <div id="info"></div>
    herro<br>
    herro<br>
    herro<br>
    herro<br>
    herro<br>
    herro<br>
    herro<br>
    herro<br>
    herro<br>
    herro<br>
    herro<br>
    herro<br>
    herro<br>
    herro<br>
    herro<br>
  </div>
</div>

CSS:

html, body {
  margin: 0;
}

.header {
  width: 100%;
  background:lightblue;
}

.container {
  max-width: 1200px;
  margin-left: auto;
  margin-right: auto;
  padding-left: 50px;
  padding-right: 50px;
  background: red;
}

js:

(function (window, document) {
    window.addEventListener('resize', function resized() {
        var scrollHeight = document.body.scrollHeight;
        var clientHeight = document.documentElement.clientHeight;
        var hasVerticalScrollbar = scrollHeight > clientHeight;
        if (hasVerticalScrollbar) {
            document.getElementById('info').innerHTML = "scrollbar";
            document.getElementById('wrapper').style.marginLeft = '15px';
        } else {
            document.getElementById('info').innerHTML = "!scrollbar";
            document.getElementById('wrapper').style.marginLeft = '0px';
        }
    });

    var iframe = document.createElement('iframe');
    iframe.id = "hacky-scrollbar-resize-listener";
    iframe.style.cssText = 'height: 0; background-color: transparent; margin: 0; padding: 0; overflow: hidden; border-width: 0; position: absolute; width: 100%;';

    iframe.onload = function () {
        iframe.contentWindow.addEventListener('resize', function () {
            try {
                var evt = document.createEvent('UIEvents');
                evt.initUIEvent('resize', true, false, window, 0);
                window.dispatchEvent(evt);
            } catch (e) {}
        });
    };

    document.body.appendChild(iframe);
})(window, document);

关于javascript - 修复使用 margin-auto 时滚动条跳转的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34842607/

相关文章:

javascript - 如何从 Facebook Messenger 对话下载所有照片? JavaScript Vanilla

javascript - 如何创建一个方法来/2数组

javascript - 如果没有 React-hooks 并使用类组件而不是功能组件,这个 React 代码会是什么样子?(打开附加窗口的按钮)

html - 如何以符合标准的方式链接 header ?

CSS - 重叠的 div

javascript - 为什么我不能从 Reactjs 中的另一个文件导入常量?

javascript - $_GET 使用旧值

javascript - 表单时隐藏元素(菜单选择选项为空)

css - 为什么第 nth-of-type 没有被 sublime text 识别?

javascript - 打开新的基础元素时出现 "Uncaught TypeError: a.indexOf is not a function"错误