2012-03-05 2 views
15

Я думал создать пользовательский плагин jQuery для прокрутки, есть много плагинов, доступных для него, но я хочу создать свою собственную проблему. Я не понимаю, как мне перемещать контент перемещая другой элемент div (полосу прокрутки), а также как перемещать содержимое с помощью скроллера мыши?Как сделать пользовательский прокрутки jQuery плагин

Пожалуйста, помогите мне разобраться в этом.

Благодаря

+0

Я не пробовал любой доступный плагин, я просто Вашингтон nt, чтобы сделать свой собственный так, прежде чем начинать, идея должна быть ясна, что то, что является концепцией/логикой, если это ясно, тогда я могу начать ее строить. –

ответ

32

Самый простой концепции будет использовать JQuery UI перетаскивать и прикрепить .draggable() метод к .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:#ddd; 
 
} 
 
.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>

выше является лишь примером с необходимости логики и математики,
он пропускает «скрытую полосу прокрутки», просто чтобы это было просто. Я оставлю вам, чтобы добавить все необходимые настройки, дополнения.

+0

это g8, на самом деле g8, и скоро вы увидите плагин, нужно применить в нем еще много вещей, но я постараюсь сделать это. Большое вам спасибо :) –

+0

@Dheeraj не может дождаться, чтобы увидеть ваш скрипт! Удачи! –

+0

Hi roXon, как я могу сделать эту работу, прокручивая колесиком мыши? – jeewan

0

Сделать обычную прокрутку без 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:#ddd; 
} 
.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 }); 
}); 
Смежные вопросы