jsBin demo
(P.S: Use the Tween onUpdate callback function to move the handler on gallery scroll)
Logic:
// How much available scroll-space** have our elements:
//
// |--$element--|<---------------avail scroll space--------------->|
// |<------------------------total width-------------------------->|
//
// Same logic goes for both elements:
galleryAvailScroll = $gallery[0].scrollWidth - $gallery.outerWidth(true);
handlerAvailScroll = $scrollbar.width() - $handler.width();
// On $gallery scroll, how to move the $handler to the proper left position?
handlerLeft = $gallery.scrollLeft() * handlerAvailScroll / galleryAvailScroll; // left PX
That's it. Get the math on scroll but also on window resize to make the handler act like a real scrollbar would:
Here's the final result with simplified CSS, HTML and JS
var $gallery = $("#thumbs"),
$scrollbar = $("#scrollbar"),
$handler = $("#scrollbar-handle"),
galleryAvailScroll = 0,
handlerAvailScroll = 0,
handlerLeft = 0;
function setPos() {
galleryAvailScroll = $gallery[0].scrollWidth - $gallery.outerWidth(true); // PX
handlerAvailScroll = $scrollbar.width() - $handler.width(); // PX
handlerLeft = $gallery.scrollLeft() * handlerAvailScroll / galleryAvailScroll;
$handler.css({left: handlerLeft});
}
setPos(); // Get the sizes
$(window).on("resize", setPos); // Also on resize
// Scroll
$gallery.on("mousewheel DOMMouseScroll", function (e) {
e.preventDefault();
var eorg = e.originalEvent,
delta = eorg.wheelDelta/120 || -eorg.detail; // Ch || FF
TweenLite.to( $gallery, 0.7, {
scrollLeft : $gallery.scrollLeft() - delta * 35,
ease : Expo.easeOut,
overwrite : 5,
onUpdate : setPos
});
});
body {
padding: 15px 0 0;
margin: 0;
}
#thumbs {
position:relative;
overflow:hidden;
padding:0 10px;
height: 175px;
white-space: nowrap;
}
#thumbs > div {
font-size:16px;
display:inline-block;
width: 150px; height: 150px;
margin: 0 6px; /*6 + 4 LineWidth = 10 */
background:#000;
}
#scrollbar {
margin:0 70px;
height:3px;
position:relative;
background:#ddd;
}
#scrollbar-handle {
position:absolute;
width:12%;
left:0;
height:3px;
background:red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/gsap/latest/TweenMax.min.js"></script>
<div id="thumbs">
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</div>
<div id="scrollbar">
<span id="scrollbar-handle"></span>
</div>
Since your handler is "fixed" at 12% width, if you decide to make it's width content-related, you can get the needed math from How to make custom scrollbar jQuery plugin