javascript - 自定义滚动条不滚动

标签 javascript html css

我很难复制别人 work在 github.com 上

我想做什么

我正在尝试为我的网站构建(复制)自定义滚动条。

什么不起作用

好的,所以我正确地下载了文件夹,我创建了一个 index.html 并将所有文件链接到它。因此,当我在浏览器(chrome 或 firefox)上运行该 index.html 时,该栏不会上下滑动,它处于静止位置;但内容仍然滚动。 我也不能用光标捕获栏来滚动文本。我在 Surface pro 4 上,128gb 值(value) 800 美元,但以 200 美元的价格得到它,所以我只能使用触摸屏滚动。

我想要什么

我只想要像链接中那样的自定义栏,但它确实运行良好。 如果您有比他们展示的更好的解决方案,请告诉我!

Here is there example!

Here is my example!

HTML

<!doctype html>
<html><head>
    <meta charset="UTF-8">
    <title>SimpleScrollbar</title>
    <link rel="stylesheet" href="simple-scrollbar.css">
    <!--[if IE 9]><script src="https://cdn.jsdelivr.net/classlist/2014.01.31/classList.min.js"></script><![endif]-->
    <script src="simple-scrollbar.min.js"></script>
  </head>
  <body>

  <a href="https://github.com/buzinas/simple-scrollbar"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://camo.githubusercontent.com/365986a132ccd6a44c23a9169022c0b5c890c387/68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f7265645f6161303030302e706e67" alt="Fork me on GitHub" data-canonical-src="https://s3.amazonaws.com/github/ribbons/forkme_right_red_aa0000.png"></a>
    <div style="height: 300px; width: 200px; display: inline-block;" ss-container="" class="ss-container"><div class="ss-wrapper"><div class="ss-content">
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
    </div></div><div class="ss-scroll" style="height: 65.6455%; top: 0%; right: -191px;"></div></div>
    <section style="height: 300px; width: 500px; display: inline-block;" ss-container="" class="ss-container"><div class="ss-wrapper"><div class="ss-content">
      <p>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?</p>
      <p>But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure. To take a trivial example, which of us ever undertakes laborious physical exercise, except to obtain some advantage from it? But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no resultant pleasure?</p>
    </div></div><div class="ss-scroll" style="height: 51.3699%; top: 0%; right: -491px;"></div></section>


</body></html>

CSS

.ss-wrapper {
  overflow: hidden;
  width: 100%;
  height: 100%;
  position: relative;
  z-index: 1;
  float: left;
}

.ss-content {
  height: 100%;
  width: calc(100% + 18px);
  padding: 0 0 0 0;
  position: relative;
  overflow: auto;
  box-sizing: border-box;
}

.ss-content.rtl {
  width: calc(100% + 18px);
  right: auto;
}

.ss-scroll {
  position: relative;
  background: rgba(0, 0, 0, 0.1);
  width: 9px;
  border-radius: 4px;
  top: 0;
  z-index: 2;
  cursor: pointer;
  opacity: 0;
  transition: opacity 0.25s linear;
}

.ss-hidden {
  display: none;
}

.ss-container:hover .ss-scroll,
.ss-container:active .ss-scroll {
  opacity: 1;
}

**.ss-grabbed {
  -o-user-select: none;
  -ms-user-select: none;
  -moz-user-select: none;
  -webkit-user-select: none;
  user-select: none;
}

Js-1; simple-scrollbar.min.js

    ! function(t, e) {
    "object" == typeof exports ? module.exports = e(window, document) : t.SimpleScrollbar = e(window, document)
}(this, function(t, e) {
    function s(t) {
        Object.prototype.hasOwnProperty.call(t, "data-simple-scrollbar") || Object.defineProperty(t, "data-simple-scrollbar", {
            value: new o(t)
        })
    }

    function i(t, s) {
        function i(t) {
            var e = t.pageY - a;
            a = t.pageY, n(function() {
                s.el.scrollTop += e / s.scrollRatio
            })
        }

        function r() {
            t.classList.remove("ss-grabbed"), e.body.classList.remove("ss-grabbed"), e.removeEventListener("mousemove", i), e.removeEventListener("mouseup", r)
        }
        var a;
        t.addEventListener("mousedown", function(s) {
            return a = s.pageY, t.classList.add("ss-grabbed"), e.body.classList.add("ss-grabbed"), e.addEventListener("mousemove", i), e.addEventListener("mouseup", r), !1
        })
    }

    function r(t) {
        for (this.target = t, this.direction = window.getComputedStyle(this.target).direction, this.bar = '<div class="ss-scroll">', this.wrapper = e.createElement("div"), this.wrapper.setAttribute("class", "ss-wrapper"), this.el = e.createElement("div"), this.el.setAttribute("class", "ss-content"), "rtl" === this.direction && this.el.classList.add("rtl"), this.wrapper.appendChild(this.el); this.target.firstChild;) this.el.appendChild(this.target.firstChild);
        this.target.appendChild(this.wrapper), this.target.insertAdjacentHTML("beforeend", this.bar), this.bar = this.target.lastChild, i(this.bar, this), this.moveBar(), this.el.addEventListener("scroll", this.moveBar.bind(this)), this.el.addEventListener("mouseenter", this.moveBar.bind(this)), this.target.classList.add("ss-container");
        var s = window.getComputedStyle(t);
        "0px" === s.height && "0px" !== s["max-height"] && (t.style.height = s["max-height"])
    }

    function a() {
        for (var t = e.querySelectorAll("*[ss-container]"), i = 0; i < t.length; i++) s(t[i])
    }
    var n = t.requestAnimationFrame || t.setImmediate || function(t) {
        return setTimeout(t, 0)
    };
    r.prototype = {
        moveBar: function(t) {
            var e = this.el.scrollHeight,
                s = this.el.clientHeight,
                i = this;
            this.scrollRatio = s / e;
            var r = "rtl" === i.direction,
                a = r ? i.target.clientWidth - i.bar.clientWidth + 18 : -1 * (i.target.clientWidth - i.bar.clientWidth);
            n(function() {
                i.scrollRatio >= 1 ? i.bar.classList.add("ss-hidden") : (i.bar.classList.remove("ss-hidden"), i.bar.style.cssText = "height:" + Math.max(100 * i.scrollRatio, 10) + "%; top:" + i.el.scrollTop / e * 100 + "%;right:" + a + "px;")
            })
        }
    }, e.addEventListener("DOMContentLoaded", a), r.initEl = s, r.initAll = a;
    var o = r;
    return o
});

js-2; simple-scrollbar.js

;(function(root, factory) {
  if (typeof exports === 'object') {
    module.exports = factory(window, document)
  } else {
    root.SimpleScrollbar = factory(window, document)
  }
})(this, function(w, d) {
  var raf = w.requestAnimationFrame || w.setImmediate || function(c) { return setTimeout(c, 0); };

  function initEl(el) {
    if (Object.prototype.hasOwnProperty.call(el, 'data-simple-scrollbar')) return;
    Object.defineProperty(el, 'data-simple-scrollbar', { value: new SimpleScrollbar(el) });
  }

  // Mouse drag handler
  function dragDealer(el, context) {
    var lastPageY;

    el.addEventListener('mousedown', function(e) {
      lastPageY = e.pageY;
      el.classList.add('ss-grabbed');
      d.body.classList.add('ss-grabbed');

      d.addEventListener('mousemove', drag);
      d.addEventListener('mouseup', stop);

      return false;
    });

    function drag(e) {
      var delta = e.pageY - lastPageY;
      lastPageY = e.pageY;

      raf(function() {
        context.el.scrollTop += delta / context.scrollRatio;
      });
    }

    function stop() {
      el.classList.remove('ss-grabbed');
      d.body.classList.remove('ss-grabbed');
      d.removeEventListener('mousemove', drag);
      d.removeEventListener('mouseup', stop);
    }
  }

  // Constructor
  function ss(el) {
    this.target = el;

    this.direction = w.getComputedStyle(this.target).direction;

    this.bar = '<div class="ss-scroll">';

    this.wrapper = d.createElement('div');
    this.wrapper.setAttribute('class', 'ss-wrapper');

    this.el = d.createElement('div');
    this.el.setAttribute('class', 'ss-content');

    if (this.direction === 'rtl') {
      this.el.classList.add('rtl');
    }

    this.wrapper.appendChild(this.el);

    while (this.target.firstChild) {
      this.el.appendChild(this.target.firstChild);
    }
    this.target.appendChild(this.wrapper);

    this.target.insertAdjacentHTML('beforeend', this.bar);
    this.bar = this.target.lastChild;

    dragDealer(this.bar, this);
    this.moveBar();

    w.addEventListener('resize', this.moveBar.bind(this));
    this.el.addEventListener('scroll', this.moveBar.bind(this));
    this.el.addEventListener('mouseenter', this.moveBar.bind(this));

    this.target.classList.add('ss-container');

    var css = w.getComputedStyle(el);
    if (css['height'] === '0px' && css['max-height'] !== '0px') {
        el.style.height = css['max-height'];
    }
  }

  ss.prototype = {
    moveBar: function(e) {
      var totalHeight = this.el.scrollHeight,
          ownHeight = this.el.clientHeight,
          _this = this;

      this.scrollRatio = ownHeight / totalHeight;

      var isRtl = _this.direction === 'rtl';
      var right = isRtl ?
        (_this.target.clientWidth - _this.bar.clientWidth + 18) :
        (_this.target.clientWidth - _this.bar.clientWidth) * -1;

      raf(function() {
        // Hide scrollbar if no scrolling is possible
        if(_this.scrollRatio >= 1) {
          _this.bar.classList.add('ss-hidden')
        } else {
          _this.bar.classList.remove('ss-hidden')
          _this.bar.style.cssText = 'height:' + Math.max(_this.scrollRatio * 100, 10) + '%; top:' + (_this.el.scrollTop / totalHeight ) * 100 + '%;right:' + right + 'px;';
        }
      });
    }
  }

  function initAll() {
    var nodes = d.querySelectorAll('*[ss-container]');

    for (var i = 0; i < nodes.length; i++) {
      initEl(nodes[i]);
    }
  }

  d.addEventListener('DOMContentLoaded', initAll);
  ss.initEl = initEl;
  ss.initAll = initAll;

  var SimpleScrollbar = ss;
  return SimpleScrollbar;
});

边注

如果有人有更好的解决方案请告诉我。 谢谢!

最佳答案

您所做的只是复制相关站点的整个 HTML 输出。

如果你想使用给定的库,你必须按照他们在 README 部分中所说的去做:

https://github.com/buzinas/simple-scrollbar#manual-binding

要以这种方式使用它,您需要将脚本添加到您的代码库中。这就是关于两个 javascript 文件的问题出现的地方: 有两个给定的 javascript 文件,一个以 .min.js 结尾,另一个以 .min.js. 结尾。 min 指的是 minified,因此它不会占用太多空间。

请不要在 Instagram 上分享视频。非常不专业。

关于javascript - 自定义滚动条不滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50894348/

相关文章:

html - 在横向模式下获取 html 页面

javascript - 为axios调用绑定(bind)数据到vue

javascript - 禁用组上的复选框

javascript - 具有特定数字的输入类型范围

html - 为什么我的 div 也不能按照我想要的方式对齐,因为当我尝试定位它们时它们一直重叠

jquery - pace.js 从未达到 100%

javascript - 停留在一个简单的对象文字范围/引用问题上

javascript - getDefaultProps 和 getInitialState 之间有什么区别?

javascript - 使用indexOf()验证电子邮件

javascript - 两个输入元素和两个按钮上的 JQuery 类选择器