Custom scroll bar visualization with HTML/CSS/JavaScript

后端 未结 2 1263
后悔当初
后悔当初 2020-12-23 23:25

I am creating a highly specialized application where I want to experiment with a custom scroll bar.

Ideally, I\'d disable the built-in scroll-bar and draw my own. Th

2条回答
  •  无人及你
    2020-12-24 00:24

    Here is a potential solution using javascript and css. The idea is not to remove the scrollbar but to simply hide it instead and let it keep doing it's work.

    Concept:

    Here the scrollbar of the actual content is shoved outside the wrapper. This prevents it from being seen (wrapper has overflow:hidden;) but does not prevent it from working.

    |---------WRAPPER-----------|
    ||--------CONTENT-----------|---|
    ||                          |[^]|
    ||                          |[0]|
    ||                          |[0]|
    ||                          |[ ]|
    ||                          |[ ]|
    ||                          |[v]|
    ||--------------------------|---|
    |---------------------------|
    

    Implementation:

    The markup:

    1Hello World

    2Hello World

    ...

    And the script (I've used jquery, but it could easily be rewritten in pure javascript.):

    $(function() {
      // initialize: both need to have the same height and width (width default: 100%)
      // these could go on your stylesheet of course.
      $("div.wrapper").css({ height: "200px", overflow: "hidden" });
      $("div.content").css({ height: "200px", overflow: "auto" }); 
    
      // now extend the content width beyond the wrapper
      $("div.content").width($("div.content").width() + 22); // 22px is the width of IE scrollbars
    
      // prevent highlight dragging from scrolling the wrapper div (thereby displaying the bars)
      $("div.wrapper").scroll(function() {
        this.scrollLeft = 0;
        this.scrollTop = 0;
      });
    
      $("div.content").scroll(function() {
        // update custom scrollbar display using this.scrollTop as a percentage of this.clientHeight
        // alert("Place slider at " + ((100 * this.scrollTop) / this.clientHeight) + "%!");
      });
    });
    

    And here it is working (though I don't have a custom scrollbar display).

提交回复
热议问题