How to make custom scrollbar jQuery plugin

后端 未结 2 793
终归单人心
终归单人心 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: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;
    }
    
    
    
    
    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.

    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.

提交回复
热议问题