How to make custom scrollbar jQuery plugin

后端 未结 2 777
终归单人心
终归单人心 2020-12-24 08:16

I was thinking to make custom scrollbar jQuery plugin, there are many plugins available for it but I want to make my own, problem is I am not getting the concept that how sh

相关标签:
2条回答
  • 2020-12-24 09:01

    Make custom scrollbar without jQuery-UI .

    HTML:-

        <div class="parent">
                <div class="scrollbar"></div>
                <div class="scrollable">
                     <p>Lorem ipsum dolor sit amet,
                     consectetur adipiscing elit. Cras non nunc eget sapien molestie mollis. 
                     Proin vestibulum vehicula varius. Duis a nunc ac risus facilisis consectetur.</p> 
               </div>
       </div>
    

    css:-

    .parent{
        position:relative;
           margin:50px;
          overflow:hidden;
        height:200px;
        width:180px;
        background:#ffffd;
    }
    .scrollable{
          overflow-y:scroll;
        position:absolute;
          padding:0 17px 0 0;
        width: 180px;
          height:100%;
    }
    .scrollbar{
    
        position:absolute;
        overflow:auto;
        top:0px;
        right:0px;
        z-index:2;
        background:#444;
        width:7px;
        border-radius:5px;
    }
    

    Javascript:-

      var $scrollable = $('.scrollable');
      var $scrollbar  = $('.scrollbar');
      $scrollable.outerHeight(true);
      var H   = $scrollable.outerHeight(true);
      var sH  = $scrollable[0].scrollHeight;
      var  sbH = H*H/sH;
    
    
    
    $('.scrollbar').height(sbH);
    
    $scrollable.on("scroll", function(){
    
        $scrollbar.css({top: $scrollable.scrollTop()/H*sbH });
    });
    
    0 讨论(0)
  • 2020-12-24 09:17

    The easiest concept would be to use the jQuery UI draggable and attach the .draggable() Method to the .scrollbar

    var $scrollable  = $(".scrollable"),
        $scrollbar   = $(".scrollbar"),
        height       = $scrollable.outerHeight(true),    // visible height
        scrollHeight = $scrollable.prop("scrollHeight"), // total height
        barHeight    = height * height / scrollHeight;   // Scrollbar height!
    
    // Scrollbar drag:
    $scrollbar.height( barHeight ).draggable({
      axis : "y",
      containment : "parent", 
      drag: function(e, ui) {
        $scrollable.scrollTop( scrollHeight / height * ui.position.top  );
      }
    }); 
    
    // Element scroll:
    $scrollable.on("scroll", function() {
      $scrollbar.css({top: $scrollable.scrollTop() / height * barHeight });
    });
    .parent{
      position:relative;
      overflow:hidden;
      height:200px;
      width:180px;
      background:#ffffd;
    }
    .scrollable{
      overflow-y:scroll;
      position:absolute;
      padding:0 17px 0 0;
      width: 180px;
      height:100%;
    }
    .scrollbar{
      cursor:n-resize;
      position:absolute;
      overflow:auto;
      top:0px;
      right:0px;
      z-index:2;
      background:#444;
      width:17px;
      border-radius:8px;
    }
    <script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
    <script src="//code.jquery.com/ui/1.11.0/jquery-ui.min.js"></script>
    
    <div class="parent">
      <div class="scrollbar"></div>
      <div class="scrollable">
        Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras non nunc eget sapien molestie mollis. Proin vestibulum vehicula varius. Duis a nunc ac risus facilisis consectetur.
        Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras non nunc eget sapien molestie mollis. Proin vestibulum vehicula varius. Duis a nunc ac risus facilisis consectetur.
        Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras non nunc eget sapien molestie mollis. Proin vestibulum vehicula varius. Duis a nunc ac risus facilisis consectetur.
        Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras non nunc eget sapien molestie mollis. Proin vestibulum vehicula varius. Duis a nunc ac risus facilisis consectetur.       
        Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras non nunc eget sapien molestie mollis. Proin vestibulum vehicula varius. Duis a nunc ac risus facilisis consectetur.
      </div>
    </div>

    The above is just an example with the needed logic and math involved,
    it misses the "hide-scrollbar", just to keep it simple. I'll leave to you to add all the needed tweaks, addons.

    0 讨论(0)
提交回复
热议问题