jQuery custom content scroller
Highly customizable custom scrollbar jQuery plugin. Features include vertical and/or horizontal scrollbar(s), adjustable scrolling momentum, mouse-wheel (via jQuery mousewheel plugin), keyboard and touch support, ready-to-use themes and customization via CSS, RTL direction support, option parameters for full control of scrollbar functionality, methods for triggering actions like scroll-to, update, destroy etc., user-defined callbacks and more.
Current version 3.1.5 (Changelog)
Upgrading from version 2
How to use it
Get started by downloading the archive which contains the plugin files (and a large amount of HTML demos and examples). Extract and upload jquery.mCustomScrollbar.concat.min.js, jquery.mCustomScrollbar.css and mCSB_buttons.png to your web server (alternatively you can load plugin files from a CDN).
HTML
Include jquery.mCustomScrollbar.css in the head tag your HTML document (more info)
<link rel="stylesheet" href="/path/to/jquery.mCustomScrollbar.css" />
Include jQuery library (if your project doesn’t use it already) and jquery.mCustomScrollbar.concat.min.js in the head tag or at the very bottom of your document, just before the closing body tag
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script src="/path/to/jquery.mCustomScrollbar.concat.min.js"></script>
CSS
The element(s) you want to add scrollbar(s) should have the typical CSS properties of an overflowed block which are a height (or max-height) value, an overflow value of auto (or hidden) and content long enough to require scrolling. For horizontal scrollbar, the element should have a width (or max-width) value set.
If you prefer to set your element’s height/width via javascript, you can use the setHeight
/setWidth
option parameters.
Initialization
Initialize via javascript
After files inclusion, call mCustomScrollbar function on the element selector you want to add the scrollbar(s)
<script> (function($){ $(window).on("load",function(){ $(".content").mCustomScrollbar(); }); })(jQuery); </script>
Initialize via HTML
Add the class mCustomScrollbar
to any element you want to add custom scrollbar(s) with default options. Optionally, set its axis via the HTML data attribute data-mcs-axis
(e.g. "x"
for horizontal and "y"
for vertical) and its theme via data-mcs-theme
. For example:
<div class="mCustomScrollbar" data-mcs-theme="dark"> <!-- your content --> </div>
Basic configuration & option parameters
axis
By default, the script applies a vertical scrollbar. To add a horizontal or 2-axis scrollbars, invoke mCustomScrollbar function with the axis option set to "x"
or "yx"
respectively
$(".content").mCustomScrollbar({ axis:"x" // horizontal scrollbar });
$(".content").mCustomScrollbar({ axis:"yx" // vertical and horizontal scrollbar });
theme
To quickly change the appearance of the scrollbar, set the theme option parameter to any of the ready-to-use themes available in jquery.mCustomScrollbar.css, for example:
$(".content").mCustomScrollbar({ theme:"dark" });
Configuration
You can configure your scrollbar(s) using the following option parameters on mCustomScrollbar function
Usage $(selector).mCustomScrollbar({ option: value });
setWidth: false
- Set the width of your content (overwrites CSS width), value in pixels (integer) or percentage (string).
setHeight: false
- Set the height of your content (overwrites CSS height), value in pixels (integer) or percentage (string).
setTop: 0
- Set the initial css top property of content, accepts string values (css top position).
Example:setTop: "-100px"
.
setLeft: 0
- Set the initial css left property of content, accepts string values (css left position).
Example:setLeft: "-100px"
.
axis: "string"
- Define content’s scrolling axis (the type of scrollbars added to the element: vertical and/of horizontal).
Available values:"y"
,"x"
,"yx"
.axis: "y"
– vertical scrollbar (default)axis: "x"
– horizontal scrollbaraxis: "yx"
– vertical and horizontal scrollbars
scrollbarPosition: "string"
- Set the position of scrollbar in relation to content.
Available values:"inside"
,"outside"
.
SettingscrollbarPosition: "inside"
(default) makes scrollbar appear inside the element. SettingscrollbarPosition: "outside"
makes scrollbar appear outside the element. Note that setting the value to"outside"
requires your element (or parent elements) to have CSSposition: relative
(otherwise the scrollbar will be positioned in relation to document’s root element).
scrollInertia: integer
- Set the amount of scrolling momentum as animation duration in milliseconds.
Higher value equals greater scrolling momentum which translates to smoother/more progressive animation. Set to0
to disable.
autoDraggerLength: boolean
- Enable or disable auto-adjusting scrollbar dragger length in relation to scrolling amount (same bahavior with browser’s native scrollbar).
SetautoDraggerLength: false
when you want your scrollbar to (always) have a fixed size.
autoHideScrollbar: boolean
- Enable or disable auto-hiding the scrollbar when inactive.
SettingautoHideScrollbar: true
will hide the scrollbar(s) when scrolling is idle and/or cursor is out of the scrolling area.
Please note that some special themes like “minimal” overwrite this option.
autoExpandScrollbar: boolean
- Enable or disable auto-expanding the scrollbar when cursor is over or dragging the scrollbar.
alwaysShowScrollbar: integer
- Always keep scrollbar(s) visible, even when there’s nothing to scroll.
alwaysShowScrollbar: 0
– disable (default)alwaysShowScrollbar: 1
– keep dragger rail visiblealwaysShowScrollbar: 2
– keep all scrollbar components (dragger, rail, buttons etc.) visible
snapAmount: integer
- Make scrolling snap to a multiple of a fixed number of pixels. Useful in cases like scrolling tabular data, image thumbnails or slides and you need to prevent scrolling from stopping half-way your elements. Note that your elements must be of equal width or height in order for this to work properly.
To set different values for vertical and horizontal scrolling, use an array:[y,x]
snapOffset: integer
- Set an offset (in pixels) for the snapAmount option. Useful when for example you need to offset the snap amount of table rows by the table header.
mouseWheel:{ enable: boolean }
- Enable or disable content scrolling via mouse-wheel.
mouseWheel:{ scrollAmount: integer }
- Set the mouse-wheel scrolling amount (in pixels). The default value
"auto"
adjusts scrolling amount according to scrollable content length.
mouseWheel:{ axis: "string" }
- Define the mouse-wheel scrolling axis when both vertical and horizontal scrollbars are present.
Setaxis: "y"
(default) for vertical oraxis: "x"
for horizontal scrolling.
mouseWheel:{ preventDefault: boolean }
- Prevent the default behaviour which automatically scrolls the parent element when end or beginning of scrolling is reached (same bahavior with browser’s native scrollbar).
mouseWheel:{ deltaFactor: integer }
- Set the number of pixels one wheel notch scrolls. The default value “auto” uses the OS/browser value.
mouseWheel:{ normalizeDelta: boolean }
- Enable or disable mouse-wheel (delta) acceleration. Setting
normalizeDelta: true
translates mouse-wheel delta value to -1 or 1.
mouseWheel:{ invert: boolean }
- Invert mouse-wheel scrolling direction. Set to
true
to scroll down or right when mouse-wheel is turned upwards.
mouseWheel:{ disableOver: [array] }
- Set the tags that disable mouse-wheel when cursor is over them.
Default value:["select","option","keygen","datalist","textarea"]
scrollButtons:{ enable: boolean }
- Enable or disable scrollbar buttons.
scrollButtons:{ scrollAmount: integer }
- Set the buttons scrolling amount (in pixels). The default value
"auto"
adjusts scrolling amount according to scrollable content length.
scrollButtons:{ scrollType: "string" }
- Define the buttons scrolling type/behavior.
scrollType: "stepless"
– continuously scroll content while pressing the button (default)scrollType: "stepped"
– each button click scrolls content by a certain amount (defined in scrollAmount option above)
scrollButtons:{ tabindex: integer }
- Set a tabindex value for the buttons.
keyboard:{ enable: boolean }
- Enable or disable content scrolling via the keyboard.
The plugin supports the directional arrows (top, left, right and down), page-up (PgUp), page-down (PgDn), Home and End keys.
keyboard:{ scrollAmount: integer }
- Set the keyboard arrows scrolling amount (in pixels). The default value
"auto"
adjusts scrolling amount according to scrollable content length.
keyboard:{ scrollType: "string" }
- Define the keyboard arrows scrolling type/behavior.
scrollType: "stepless"
– continuously scroll content while pressing the arrow key (default)scrollType: "stepped"
– each key release scrolls content by a certain amount (defined in scrollAmount option above)
contentTouchScroll: integer
- Enable or disable content touch-swipe scrolling for touch-enabled devices.
To completely disable, setcontentTouchScroll: false
.
Integer values define the axis-specific minimum amount required for scrolling momentum (default:25
).
documentTouchScroll: boolean
- Enable or disable document touch-swipe scrolling for touch-enabled devices.
advanced:{ autoExpandHorizontalScroll: boolean }
- Auto-expand content horizontally (for
"x"
or"yx"
axis).
If set totrue
, content will expand horizontally to accommodate any floated/inline-block elements.
Setting its value to2
(integer) forces the non scrollHeight/scrollWidth method. A value of3
forces the scrollHeight/scrollWidth method.
advanced:{ autoScrollOnFocus: "string" }
- Set the list of elements/selectors that will auto-scroll content to their position when focused.
For example, when pressing TAB key to focus input fields, if the field is out of the viewable area the content will scroll to its top/left position (same bahavior with browser’s native scrollbar).
To completely disable this functionality, setautoScrollOnFocus: false
.
Default:"input,textarea,select,button,datalist,keygen,a[tabindex],area,object,[contenteditable='true']"
advanced:{ updateOnContentResize: boolean }
- Update scrollbar(s) automatically on content, element or viewport resize.
The value should betrue
(default) for fluid layouts/elements, adding/removing content dynamically, hiding/showing elements etc.
advanced:{ updateOnImageLoad: boolean }
- Update scrollbar(s) automatically each time an image inside the element is fully loaded.
Default value isauto
which triggers the function only on"x"
and"yx"
axis (if needed).
The value should betrue
when your content contains images and you need the function to trigger on any axis.
advanced:{ updateOnSelectorChange: "string" }
- Update scrollbar(s) automatically when the amount and size of specific selectors changes.
Useful when you need to update the scrollbar(s) automatically, each time a type of element is added, removed or changes its size.
For example, settingupdateOnSelectorChange: "ul li"
will update scrollbars each time list-items inside the element are changed.
Setting the value totrue
, will update scrollbars each time any element is changed.
To disable (default) set tofalse
.
advanced:{ extraDraggableSelectors: "string" }
- Add extra selector(s) that’ll release scrollbar dragging upon mouseup, pointerup, touchend etc.
Example:extraDraggableSelectors: ".myClass, #myID"
advanced:{ releaseDraggableSelectors: "string" }
- Add extra selector(s) that’ll allow scrollbar dragging upon mousemove/up, pointermove/up, touchend etc.
Example:releaseDraggableSelectors: ".myClass, #myID"
advanced:{ autoUpdateTimeout: integer }
- Set the auto-update timeout in milliseconds.
Default timeout:60
theme: "string"
- Set the scrollbar theme.
View all ready-to-use themes
All themes are contained in plugin’s CSS file (jquery.mCustomScrollbar.css).
Default theme:"light"
callbacks:{ onCreate: function(){} }
- A function to call when plugin markup is created.
Example:
callbacks:{ onCreate:function(){ console.log("Plugin markup generated"); } }
callbacks:{ onInit: function(){} }
- A function to call when scrollbars have initialized (demo).
Example:
callbacks:{ onInit:function(){ console.log("Scrollbars initialized"); } }
callbacks:{ onScrollStart: function(){} }
- A function to call when scrolling starts (demo).
Example:
callbacks:{ onScrollStart:function(){ console.log("Scrolling started..."); } }
callbacks:{ onScroll: function(){} }
- A function to call when scrolling is completed (demo).
Example:
callbacks:{ onScroll:function(){ console.log("Content scrolled..."); } }
callbacks:{ whileScrolling: function(){} }
- A function to call while scrolling is active (demo).
Example:
callbacks:{ whileScrolling:function(){ console.log("Scrolling..."); } }
callbacks:{ onTotalScroll: function(){} }
- A function to call when scrolling is completed and content is scrolled all the way to the end (bottom/right) (demo).
Example:
callbacks:{ onTotalScroll:function(){ console.log("Scrolled to end of content."); } }
callbacks:{ onTotalScrollBack: function(){} }
- A function to call when scrolling is completed and content is scrolled back to the beginning (top/left) (demo).
Example:
callbacks:{ onTotalScrollBack:function(){ console.log("Scrolled back to the beginning of content."); } }
callbacks:{ onTotalScrollOffset: integer }
- Set an offset for the onTotalScroll option.
For example, settingonTotalScrollOffset: 100
will trigger the onTotalScroll callback 100 pixels before the end of scrolling is reached.
callbacks:{ onTotalScrollBackOffset: integer }
- Set an offset for the onTotalScrollBack option.
For example, settingonTotalScrollBackOffset: 100
will trigger the onTotalScrollBack callback 100 pixels before the beginning of scrolling is reached.
callbacks:{ alwaysTriggerOffsets: boolean }
- Set the behavior of calling onTotalScroll and onTotalScrollBack offsets.
By default, callback offsets will trigger repeatedly while content is scrolling within the offsets.
SetalwaysTriggerOffsets: false
when you need to trigger onTotalScroll and onTotalScrollBack callbacks once, each time scroll end or beginning is reached.
callbacks:{ onOverflowY: function(){} }
- A function to call when content becomes long enough and vertical scrollbar is added.
Example:
callbacks:{ onOverflowY:function(){ console.log("Vertical scrolling required"); } }
callbacks:{ onOverflowX: function(){} }
- A function to call when content becomes wide enough and horizontal scrollbar is added.
Example:
callbacks:{ onOverflowX:function(){ console.log("Horizontal scrolling required"); } }
callbacks:{ onOverflowYNone: function(){} }
- A function to call when content becomes short enough and vertical scrollbar is removed.
Example:
callbacks:{ onOverflowYNone:function(){ console.log("Vertical scrolling is not required"); } }
callbacks:{ onOverflowXNone: function(){} }
- A function to call when content becomes narrow enough and horizontal scrollbar is removed.
Example:
callbacks:{ onOverflowXNone:function(){ console.log("Horizontal scrolling is not required"); } }
callbacks:{ onBeforeUpdate: function(){} }
- A function to call right before scrollbar(s) are updated.
Example:
callbacks:{ onBeforeUpdate:function(){ console.log("Scrollbars will update"); } }
callbacks:{ onUpdate: function(){} }
- A function to call when scrollbar(s) are updated.
Example:
callbacks:{ onUpdate:function(){ console.log("Scrollbars updated"); } }
callbacks:{ onImageLoad: function(){} }
- A function to call each time an image inside the element is fully loaded and scrollbar(s) are updated.
Example:
callbacks:{ onImageLoad:function(){ console.log("Image loaded"); } }
callbacks:{ onSelectorChange: function(){} }
- A function to call each time a type of element is added, removed or changes its size and scrollbar(s) are updated.
Example:
callbacks:{ onSelectorChange:function(){ console.log("Scrollbars updated"); } }
live: "string"
- Enable or disable applying scrollbar(s) on all elements matching the current selector, now and in the future.
Setlive: true
when you need to add scrollbar(s) on elements that do not yet exist in the page. These could be elements added by other scripts or plugins after some action by the user takes place (e.g. lightbox markup may not exist untill the user clicks a link).
If you need at any time to disable or enable the live option, setlive: "off"
and"on"
respectively.
You can also tell the script to disable live option after the first invocation by settinglive: "once"
.
liveSelector: "string"
- Set the matching set of elements (instead of the current selector) to add scrollbar(s), now and in the future.
Plugin methods
Ways to execute various plugin actions programmatically from within your script(s).
update
Usage $(selector).mCustomScrollbar("update");
Call the update method to manually update existing scrollbars to accommodate new content or resized element(s). This method is by default called automatically by the script (via updateOnContentResize
option) when the element itself, its content or scrollbar size changes.
scrollTo
Usage $(selector).mCustomScrollbar("scrollTo",position,options);
Call the scrollTo method to programmatically scroll the content to the position parameter (demo).
position parameter
Position parameter can be:
"string"
- e.g. element selector:
"#element-id"
- e.g. special pre-defined position:
"bottom"
- e.g. number of pixels less/more:
"-=100"
/"+=100"
- e.g. element selector:
integer
- e.g. number of pixels:
100
- e.g. number of pixels:
[array]
- e.g. different y/x position:
[100,50]
- e.g. different y/x position:
object/function
- e.g. jQuery object:
$("#element-id")
- e.g. js object:
document.getelementbyid("element-id")
- e.g. function:
function(){ return 100; }
- e.g. jQuery object:
Pre-defined position strings:
"bottom"
– scroll to bottom"top"
– scroll to top"right"
– scroll to right"left"
– scroll to left"first"
– scroll to the position of the first element within content"last"
– scroll to the position of the last element within content
Method options
scrollInertia: integer
- Scroll-to duration, value in milliseconds.
Example:
$(selector).mCustomScrollbar("scrollTo","bottom",{ scrollInertia:3000 });
scrollEasing: "string"
- Scroll-to animation easing, values:
"linear"
,"easeOut"
,"easeInOut"
.
Example:
$(selector).mCustomScrollbar("scrollTo","bottom",{ scrollEasing:"easeOut" });
moveDragger: boolean
- Scroll scrollbar dragger (instead of content).
Example:
$(selector).mCustomScrollbar("scrollTo",80,{ moveDragger:true });
timeout: integer
- Set a timeout for the method (the default timeout is 60 ms in order to work with automatic scrollbar update), value in milliseconds.
Example:
$(selector).mCustomScrollbar("scrollTo","top",{ timeout:1000 });
callbacks: boolean
- Trigger user defined callbacks after scroll-to completes.
Example:
$(selector).mCustomScrollbar("scrollTo","left",{ callbacks:false });
stop
Usage $(selector).mCustomScrollbar("stop");
Stops any running scrolling animations (usefull when you wish to interupt a previously scrollTo method call).
disable
Usage $(selector).mCustomScrollbar("disable");
Calling disable method will temporarily disable the scrollbar (demo). Disabled scrollbars can be re-enable by calling the update method.
To disable the scrollbar and reset its content position, set the method’s reset parameter to true
$(selector).mCustomScrollbar("disable",true);
destroy
Usage $(selector).mCustomScrollbar("destroy");
Calling destroy method will completely remove the custom scrollbar and return the element to its original state (demo).
Scrollbar styling & themes
You can design and visually customize your scrollbars with pure CSS, using jquery.mCustomScrollbar.css which contains the default/basic styling and all scrollbar themes.
The easiest/quickest way is to select a ready-to-use scrollbar theme. For example:
$(selector).mCustomScrollbar({ theme:"dark" });
You can modify the default styling or any theme either directly in jquery.mCustomScrollbar.css or by overwriting the CSS rules in another stylesheet.
Creating a new scrollbar theme
Create a name for your theme (e.g. “my-theme”) and set it as the value of the theme option
$(selector).mCustomScrollbar({ theme:"my-theme" });
Your element will get the class “mCS-my-theme” (your theme-name with “mCS” prefix), so you can create your CSS using the .mCS-my-theme
in your rules. For instance:
.mCS-my-theme.mCSB_scrollTools .mCSB_dragger .mCSB_dragger_bar{ background-color: red; } .mCS-my-theme.mCSB_scrollTools .mCSB_draggerRail{ background-color: white; } /* and so on... */
In the same manner you can clone any existing theme (e.g. “dark”), change its selector (e.g. .mCS-dark
) to your own theme name (e.g. .mCS-my-theme
) and modify its CSS rules.
Scrollbar markup
The plugin applies specific id (unique) and/or classes to every scrollbar element/component, meaning that you can target and modify any scrollbar in more than one ways.
For example, every element with a scrollbar gets a unique class in the form of _mCS_1
, _mCS_2
etc. Every scrollbar container element gets a unique id in the form of mCSB_1_scrollbar_vertical
, mCSB_2_scrollbar_vertical
etc. Every scrollbar dragger gets a unique id in the form of mCSB_1_dragger_vertical
, mCSB_2_dragger_vertical
etc. in addition to the class mCSB_dragger
. All these mean that you can do stuff like:
._mCS_1 .mCSB_dragger .mCSB_dragger_bar{ background-color: red; } ._mCS_2 .mCSB_dragger .mCSB_dragger_bar{ background-color: green; } #mCSB_3_dragger_vertical .mCSB_dragger_bar{ background-color: blue; } #mCSB_1_scrollbar_vertical .mCSB_dragger{ height: 100px; } #mCSB_1_scrollbar_horizontal .mCSB_dragger{ width: 100px; } .mCSB_1_scrollbar .mCSB_dragger .mCSB_draggerRail{ width: 4px; }
User-defined callbacks
You can trigger your own js function(s) by calling them inside mCustomScrollbar callbacks option parameter
$(".content").mCustomScrollbar({ callbacks:{ onScroll:function(){ myCustomFn(this); } } }); function myCustomFn(el){ console.log(el.mcs.top); }
In the example above, each time a scroll event ends and content has stopped scrolling, the content’s top position will be logged in browser’s console. There are available callbacks for each step of the scrolling event:
onScrollStart
– triggers the moment a scroll event startswhileScrolling
– triggers while scroll event is runningonScroll
– triggers when a scroll event completesonTotalScroll
– triggers when content has scrolled all the way to bottom or rightonTotalScrollBack
– triggers when content has scrolled all the way back to top or left
You can set an offset value (pixels) for both onTotalScroll
and onTotalScrollBack
by setting onTotalScrollOffset
and onTotalScrollBackOffset
respectively (view example).
By default, onTotalScroll
and onTotalScrollBack
callbacks are triggered repeatedly. To prevent multiple calls when content is within their offset, set alwaysTriggerOffsets
option to false
(view example).
Additional callbacks:
onInit
onOverflowY
onOverflowX
onOverflowYNone
onOverflowXNone
onUpdate
onImageLoad
onSelectorChange
Returning values
The script returns a number of values and objects related to scrollbar that you can use in your own functions
this
– the original element containing the scrollbar(s)this.mcs.content
– the original content wrapper as jquery objectthis.mcs.top
– content’s top position (pixels)this.mcs.left
– content’s left position (pixels)this.mcs.draggerTop
– scrollbar dragger’s top position (pixels)this.mcs.draggerLeft
– scrollbar dragger’s left position (pixels)this.mcs.topPct
– content vertical scrolling percentagethis.mcs.leftPct
– content horizontal scrolling percentagethis.mcs.direction
– content’s scrolling direction (y or x)
Plugin-specific jQuery expressions
$("#myID:mcsInView")
- Select element(s) in your content that are within scrollable viewport.
As condition:$("#myID").is(":mcsInView");
$(".content:mcsOverflow")
- Select overflowed element(s) with visible scrollbar.
As condition:$(".content").is(":mcsOverflow");
$("#myID:mcsInSight")
$("#myID:mcsInSight(exact)")
- Select element(s) in your content that are in view of the scrollable viewport. Using the
exact
parameter will include elements that have any part of them (even 1 pixel) in view of the scrollable viewport.
As condition:$("#myID").is(":mcsInSight");
,$("#myID").is(":mcsInSight(exact)");
Plugin dependencies & requirements
- jQuery version 1.6.0 or higher
- Mouse-wheel support
License
This work is released under the MIT License.
You are free to use, study, improve and modify it wherever and however you like.
https://opensource.org/licenses/MIT
Donating helps greatly in developing and updating free software and running this blog 🙂
First thank you for your great plugin !
I use 2 horizontal scrollbars for a div (1 scroll bottom and 1 top) with the technic explained there : http://stackoverflow.com/questions/3934271/horizontal-scrollbar-on-top-and-bottom-of-table
In my case I have in “whileScrolling” event of both scrollbar:
position = this.mcs.leftPct; sScroll.mCustomScrollbar("scrollTo", position + "%", {callbacks:false});
Notice that “sScroll” is the selector of the other scrollbar.
So when a scrollbar is scrolling, we force scrolling the other. I use pourcentage because this.mcs.draggerLeft doesn’t work.
It works (a bit) but scrollbar don’t follow in the right way.
Have you a better solution for having 2 horizontal scrollbar (top & bottom) following each other ?
Hello,
You could use
this.mcs.left
and probably set scrollTo method timeout to zero (default is 60 milliseconds). I’d also suggest setting method’s scrollEasing option to"easeOut"
to make it identical to the standard scrolling animation:position = this.mcs.left; sScroll.mCustomScrollbar("scrollTo", position, { callbacks:false, timeout:0, scrollEasing:"easeOut" });
Wow thank you so much ! It works great !
My last problem is to change all scrollbars when a div is hover.
I tried to set background of scrollbar when the div capture “mouseenter” event and back color to normal when “mouseleave”.
It works but then the hover of the scrollbar doesn’t work anymore.
Code :
sDiv.on("mouseenter", function(){ $(".mCS-theme.mCSB_scrollTools .mCSB_dragger .mCSB_dragger_bar").css("background-color", "#1997c6"); $(".mCS-theme.mCSB_scrollTools .mCSB_dragger .mCSB_dragger_bar").css("background-color", "rgba(25, 151, 198, 0.85)"); }).on("mouseleave", function(){ $(".mCS-theme.mCSB_scrollTools .mCSB_dragger .mCSB_dragger_bar").css("background-color", "#4F4F4F"); $(".mCS-theme.mCSB_scrollTools .mCSB_dragger .mCSB_dragger_bar").css("background-color", "#rgba(79, 79, 79, 0.5)"); });
My other solution was to force a “mouseover” on my scrollbars but it didn’t succeed.
So what is the best way to force “mouseover” on a scrollbar ?
Thank you again for your last answer (very helpful !)
I think hover doesn’t work because the inline CSS, which is the one added by jQuery, overwrites all others. I’d suggest toggling a class instead of applying the background-color style directly, so everything works as expected. For example:
JS:
var draggerBar=$(".mCS-theme.mCSB_scrollTools .mCSB_dragger .mCSB_dragger_bar"); sDiv.on("mouseenter", function(){ draggerBar.addClass("sDiv-hover"); }).on("mouseleave", function(){ draggerBar.removeClass("sDiv-hover"); });
CSS:
.mCS-theme.mCSB_scrollTools .mCSB_dragger .mCSB_dragger_bar.sDiv-hover{ background-color: #1997c6; }
Does this make sense for what you want to do?
Excellent !
You’re right I was not using the right way to change the background-color.
Thank you se much for your plugin and your time.
An update on double scrollbars:
I’ve just released plugin version 3.1.2 so you can re-download it as it fixes some potential issues with horizontal scrollbar resizing.
I’ve also created an example of double horizontal scrollbars here:
http://manos.malihu.gr/repository/custom-scrollbar/demo/examples/double-horizontal-scrollbar-example.html
In this example, the code is simplified without extra markup/dummy elements and no scrollTo method which works much better. Everything is done within
mCustomScrollbar
function call and the demo code is:$(".wrapper").mCustomScrollbar({ axis:"x", callbacks:{ onInit:function(){ var $this=$(this), cloned=$this.find(".mCSB_scrollTools").clone(true).addClass("mCSB_scrollTools-clone"); $this.find(".mCSB_scrollTools").parent().append(cloned); $this.data("scrollbar",{ top:$this.find(".mCSB_scrollTools-clone .mCSB_dragger"), bottom:$this.find(".mCSB_scrollTools:not(.mCSB_scrollTools-clone) .mCSB_dragger") }); }, onUpdate:function(){ $(this).data("scrollbar").top.css("width",$(this).data("scrollbar").bottom.width()); }, whileScrolling:function(){ $(this).data("scrollbar").top.css("left",this.mcs.draggerLeft); } } });
Hi,malihu,can you help me?
It doesn’t work When I call a scrollTo method and set the position parameter to be an element selector,actually it scrolls to the bottom.
And when set to be a specific number of pixels(i.e. integer values),it scrolls to an unexpected position,depend on the height of my content.
It should normally work, so I can’t really help unless I see your page/code (can you send me a link?). Maybe some element requires a CSS position value(?)
Please complete the security check to access malihu.gr
I get a wrong page every time the comment contains code,why?
Let me try to post some idle code,I’m sorry for your any inconenience..
<body>why?</body>
I tried on another computer,and this time it works well in the first case,but scrolls to the bottom in the other case.My code is as follow:
HTML <body> <div class="content" style="width: 100px; height: 200px; overflow: auto;"> <div> <div class="scale"> 20px </div> <div class="scale"> 40px </div> //A long list of "scale"... <div id="scrollHere" class="scale scrollHere">//Notice this 300px </div> //A long list of "scale"... </div> </div> <button style="width:30px;height:20px;"></button> </body>
Thanks godness! CSS and JS below
.scale { height: 19px; border-bottom: 1px solid; } $(document).ready(function () { $(".content").mCustomScrollbar(); $("button").click(function () { $(".content").mCustomScrollbar("scrollTo", ".scrollHere");//works well $(".content").mCustomScrollbar("scrollTo", 400);//scroll to the bottom }); });
The list is long enough ,much longer than 400px.
OK I think the issue is because of the button element getting the focus (while clicked) and the scrollbar scrolling automatically to the focused element (the button itself!).
You need to use the
autoScrollOnFocus
option parameter and remove thebutton
selector from its value:$(".content").mCustomScrollbar({ advanced:{ autoScrollOnFocus: "input,textarea,select,datalist,keygen,a[tabindex],area,object,[contenteditable='true']" } });
Thank you! It’s very kind of you.
I want to make scrollbar element always exist even if content is not long enough to use scrollbar. Is it possible?
See alwaysShowScrollbar: int
thank you leomn!
Dear Malihu, I have a trouble. But I’m not good at english,,so you’d better check this youtube video..
the contents was changed yet ‘mCS_no_scrollbar’ didn’t be removed and your scrollbar didn’t appear.
how can I fix this problem?
plz check this video
https://youtu.be/kg_bvVKW9kU
Actually, the code $(xxx).mCustomScrollbar(xxx); executed everytime modal window opened
Hello,
How do you change the content of the modal? With javascript? Can you send me your code?
I’m using AngularJS for controlling MVC model..
In Controller, ‘$scope.tmodal_intro’is binding with the element’s value.
in the method $scope.setTmodal(), $scope.tmodal_intro = $scope.tutors[index].intro;
$scope.tmodal_intro is updated like above and AngularJS automatically apply this change to DOM.
AngularJS always scanning the controller, then it detect the change and update related DOM element.
My code is too complicated, so I will make some simple example with angularJS and bootstrap for you.
https://github.com/Rocketboy2015/malihu
this is the sample code
You need to either set/update the content inside the
.mCSB_container
which is inside the.content
element or apply the scrollbar on.modal-body
instead.I’ve modified the files to apply the scrollbar on
.modal-body
(tested and fully working). I sent you the modified files (index.html, script.js and style.css) by email.Thank you so much! It works!
The X Scroll is not working with mac trackpad or magic mouse.
With a PC gamer mouse its working fine.
Any suggestions? i didnt find any options to help me with this problem
Thank you
Do you mean on elements with both vertical and horizontal scrollbars?
Hi
I have an issue where resizing is not quite right. If I am running two monitors and monitor 1 is a smaller resolution than monitor 2 and I drag my browser window from monitor 1 to monitor 2 then there is a gap at the bottom. I am assuming the code has cached some available sizes from monitor 1 rather than re-reading them when needed.
Any thoughts on this?
Thanks
Can’t really say why this happens as I’ve never experience it before… The script does not cache any sizes as it’s checking them in real-time every few milliseconds.
When you say “gap at the bottom” what exactly happens? Is there an empty scrollable gap? Does this happen only when dragging between monitors or by resizing the browser as well?
Using your scrollbar in my site and thought it was running great, however, I have found some more of my code further down the header section that’s not being run at all. Seems like anywhere down from where I include the jquery.mCustomScrollbar.js, or the minified version, in my header block then it doesn’t run.
I’m not getting any errors or anything reported in the browser and I can’t think why it is failing. Moving the including of your code right down to just before the end of the body tag would fix things but then I got lots of other errors where I’m calling it in $(document).ready(function() { })
I’m running jQuery 1.11.2 and the latest version of your scrollbar. I know you will ask for code or errors, but I’m fitting it into an existing site which is rather complicated and it seems to work standalone on the demo pages.
Any ideas how I can debug this, or any thoughts on what would be causing it? As far as I can see it just loads the scrollbar code and then stops, ignoring everything else following. Do I really need to add it right at the bottom of the page for reasons other than performance? Would love to sort this out because otherwise it’s fantastic and is just the job.
Thanks in advance.
No you can add it anywhere you want as any other plugin script. This might be related to something else(?) but I cannot help unless I see your page/code. Can you send me a link?
Do you run other libraries in addition to jQuery (e.g. prototype.js)?
Apologies, but it seems to have fixed itself now.
Can I ask you another question which I am puzzling over? I’ve a jquery sortable list inside your scrollbar. When I drag an item to the top of the list it doesn’t automatically scroll upwards. Could this be programmed do you know, or is there a setting somewhere? Detecting when you’ve hit the top and then scrolling the scrollbar is proving tricky.
Plugin archive contains an example with jquery-ui sortable which does what you describe. You can see it online here:
http://manos.malihu.gr/repository/custom-scrollbar/demo/examples/jquery_ui_sortable_example.html
I think you can see the code and adapt it to your markup 😉
Thank you, you’re a star.
Hi,
I love your custom scrollbar, however I am trying this one thing which I can’t seem to get working. I have these “” tabs of a huge table which is scrolled through using your scrollbar. Now when I am using tab to traverse, the horizontal scroll seem to move faster because of more width of each tab (than height) and the bottom one moves slower, so the content gets hidden. I was hoping if I could know about an option where it is possible to have two different snapAmounts (x-axis and y-axis). Or any other work around.
Please note, I have triggered a fixed table headers and left bar using the callback scroll function.
There’s no option for different y/x snapAmount but if you could send me a link in order to understand better what you’re trying to do, I might be able to help with a workaround.
https://www.diigo.com/item/image/5a2ja/r4c0
As you can see, I am navigating the green center tabs using arrows. The scrollbar moves faster when moved to bottom hence the tab goes underneath while the snapamount for horizontal movements are perfect.
The table column on the right is to fixed using the callback function onscroll.
Hey thanks for contacting me. I solved it by assigning
$(selector).mCustomScrollbar(“scrollTo”,position,options);
to each of the keys I had binded arrows to.
Cool! I’ll push an update to 3.x branch (plugin development version) that’ll feature different snapAmount values for each axis (e.g.
snapAmount:[50,100]
for 50px vertical and 100px horizontal). I’ll push the update by tomorrow so you can check it out if you want.Thanks. I will check.
The scroll bar seems to work fine in most cases but I have a div that contains images with CSS property set to inline-block, and no-whitespace in order to get a horiz scroll bar of images. It works fine as long as I don’t enable the plugin but as soon as I do, all those images sizes get adjusted (really huge). It only happens when I set the no-whitespace property and I can’t think of an y other way to get the effect I want without that being set (a div with a horiz scroll bar filled with images)
Can you post the CSS or send me a link/jsFiddle?
Normally, the following example will work:
HTML
<div class="mcs-horizontal-example"> <img src="..." class="item" /> <img src="..." class="item" /> <img src="..." class="item" /> <!-- and so on... --> </div>
CSS
.mcs-horizontal-example{ overflow-x: auto; white-space: nowrap; } .mcs-horizontal-example .item{ display: inline-block; }
JS
$(".mcs-horizontal-example").mCustomScrollbar({ axis:"x" //,advanced:{autoExpandHorizontalScroll:true} //uncomment/enable for dynamic content });
You fixed my issue, thank you so much not only for that but for making this wonderful plugin.
Hey, first of all I just want to say you’ve built an amazing plugin!
Now, my question is there any way to have the buttonUp / buttonDown arrows to have mobile click functionality? As in, lets say the user doesn’t want to click the scroll bar or click and drag and instead wants to use the teeny tiny up and arrow keys to navigate. From what I’ve messed around with I can’t seem to find a way to have it listen correctly. Thanks in advance!
Figured it out, realized it needed to be a ‘stepless’ type instead of a ‘stepped’. Not too certain on as to why though. Didn’t spend too much time researching why the button struggles to click if set as a ‘stepped’ but works like a charm as ‘stepless’.
I have enabled the plugin on my website. I chose for the horizontal scrollbar and everything seems to work perfectly. When i checked my website in chrome and safari there is an issue though. When i move the scrollbar it seems to flicker. Any idea what causes this problem?
Thanks for your reply.
I can’t really say without viewing your website… Can you send me a link?
Is there any way to include any “sticky” library in mCustomScrollbar?
I need heading on each of my paragraphs to be fixed to the top of the page while i am scrolling.
I tried with Sticky-Kit (http://leafo.net/sticky-kit/) but it seams that the library did not catch the event when something is out of view.
I’m not sure Sticky-Kit or other similar scripts will work with the custom scrollbar, because they usually use browser’s native/default scrollbar events. CSS fixed position should work with the custom scrollbar though.
I presume that i need to make that in “whileScrolling” callback.
Can you please share with us how is the best way to approach this?
Thanks
Sure.
If you want to do it programmatically you can use
whileScrolling
callback as you said 🙂It all depends on the functionality you need but a simple example would be:
Assuming you have the following element in your scrollable content:
<div id="sticky">sticky content</div>
You give it the following CSS:
#sticky{ position: absolute; top: 0; }
Then in your
mCustomScrollbar
you can do:$(selector).mCustomScrollbar({ callbacks:{ whileScrolling:function(){ $("#sticky").css("top",-this.mcs.top); } } });
The above will make it stick to zero position. The calculation is done easily using the
mcs
object (see more info in “Returning values” section of User-defined callbacks).To give it an offset, simply add a margin-top value in the CSS, e.g.
margin-top: 30px;
You can of course create more advanced sticky elements by doing various calculations in
whileScrolling
callback (e.g. usingmcs
object) but it depends on how you want the sticky element to function.Hi, i’m not sure, maybe i’ve just missed this in APi, but how can i make plugin to scorll at point where i clicked on scrollbine line; i mean when you clicking on OS default scroll it’s goint to same position of page where you clicked on scroll-bar
Thanks 🙂
Normally when clicking on browser’s default scrollbar it scrolls by a fixed amount each time (not to the same position you clicked). It appears that it scrolls to the same position because it keeps scrolling while mouse button is being pressed (until it reaches the cursor position). The difference with the custom scrollbar is that it scrolls by click (not repeatedly by pressing the mouse). I’ll probably change the event at some future version to match the default browser’s scrollbar.
Hello, firstly I’d like to congrats you, this plugin is very useful.
I’m trying use it with horizontal scroll, but it doesn’t work 🙁
Look my code:
<div class="row div-scroll-horizontal"> <!---- In that class I put overflow-x: auto ---> <ul id="thumblist" class="list-inline img-thumbs" > <li class="image-list"><a href="#" class="bullet-image"><img src="../images/image1.png" data-imageName='image1.png' class="img-lista" title="RG Frente"></a></li> <li class="image-list"><a href="#" class="bullet-image"><img src="../images/image2.png" data-imageName='image2.png' class="img-lista" title="RG Verso"></a></li> <li class="image-list"><a href="#" class="bullet-image"><img src="../images/image1.png" data-imageName='image1.png' class="img-lista" title="RG Frente"></a></li> <li class="image-list"><a href="#" class="bullet-image"><img src="../images/image2.png" data-imageName='image2.png' class="img-lista" title="RG Verso"></a></li> <li class="image-list"><a href="#" class="bullet-image"><img src="../images/image1.png" data-imageName='image1.png' class="img-lista" title="RG Frente"></a></li> <li class="image-list"><a href="#" class="bullet-image"><img src="../images/img1.jpg" data-imageName='img1.jpg' class="img-lista" title="RG Verso"></a></li> <li class="image-list"><a href="#" class="bullet-image"><img src="../images/Desert.jpg" data-imageName='Desert.jpg' class="img-lista" title="RG Frente"></a></li> <li class="image-list"><a href="#" class="bullet-image"><img src="../images/img1.jpg" data-imageName='img1.jpg' class="img-lista" title="RG Verso"></a></li> <li class="image-list"><a href="#" class="bullet-image"><img src="../images/Desert.jpg" data-imageName='Desert.jpg' class="img-lista" title="RG Frente"></a></li> <li class="image-list"><a href="#" class="bullet-image"><img src="../images/image2.png" data-imageName='image2.png' class="img-lista" title="RG Verso"></a></li> </ul> </div>
I apreciate any help 🙂
Are you using latest plugin version (3.1.x)? Can you post your CSS?
If your list-items are floated left, you should probably need to enable autoExpandHorizontalScroll option (info)
advanced:{ autoExpandHorizontalScroll: true }
First of all, thanks for writing such a great plugin!
My use case might be a little different than what you’re used to. 🙂 Basically, I would like to decouple the scrollbar from scrollable content and instead to use it as a “zoom” feature for an image cropper. Is this decoupling possible? I’ve looked through the API and either I completely missed what I’m looking for or it just never clicked.
I’ve been able to wire the arrow buttons to the cropper API using some javascript when I initiate the scrollbar, but I would like to have a fixed number of clicks for the dragger bar to move across the dragger rail. Here’s what I’ve got so far:
$(‘#zoomSlider’).mCustomScrollbar({
theme: “rounded-dark”,
axis: “x”,
autoDraggerLength: false,
//advanced: { autoExpandHorizontalScroll: true },
//setWidth: “269”,
alwaysShowScrollbar: 2,
scrollbarPosition: “outside”,
scrollButtons: {
enable: true,
scrollAmount: 1
}
});
What changes can I make to get approximately 6 clicks in either direction from 0% to 100%? (I’ve managed to get everything working correctly except for this….)
Thanks!
Maybe you could use the snapAmount option parameter and set its value to be 1/6 of the content length or some other value you can calculate(?)
Hi Malihu,
Great job with this plugin, congrats!
I’m trying to integrate it with a vertical scrollbar inside a carousel slide, and the issue I’m getting in is that on mobile the custom scrollbar prevents horzizontal swiping, so I can no longer swipe to the next carousel item. On desktop, mouse-swiping works great (mouse events are bubbling), but it seems like the touch events are not bubbling.
Check out this example: http://jsfiddle.net/tkzvzp54/5/embedded/result/.
Is there any easy way to allow horizontal swipe on a container with a vertical scrollbar?
Thanks,
Nick
The themes are great for the custom scroll bar! The problem I’m experience is that touch swiping is completely broken when using this plugin. I am using a Microsoft Surface Pro 3.
Touch swiping does not work. Only if I scroll with my mouse and then swipe while the scroll is already in action does it work.
my code:
$.mCustomScrollbar.defaults.scrollButtons.enable = true; //enable scrolling buttons by default $(".table__wrapper").mCustomScrollbar({ theme: "rounded-dots-dark", scrollInertia: 400, scrollButtons: { scrollAmount: 10, snapAmount: 100, contentTouchScroll: 25 } }); $(".table__wrapper").on("mousewheel", function (e, delta) { e.preventDefault(); $(".table__wrapper .mCustomScrollBox").trigger("mousewheel", [delta]); });
Update – apparently touch scrolling works on Chrome – but does not work on IE11 or Edge.
On firefox, the touch scrolling only barely moves a few pixes – where on Chrome, it scrolls as intended. Any solution for cross browser touch scrolling?
This is a known issue with IE and Edge browsers on some hybrid devices (for example on Windows phone it works as expected). I’m trying to solve this but it takes a bit longer as I don’t have such device for testing.
There is a bug on Firefox using Surface Pro 3. When browser is “full screen”, touch scrolling/swiping the content completely does not work. It only highlights the text. When full screen is disabled, touch scrolling/swiping works fine.
Hi, great article here! I am using this plugin on a website, and for the most part it works great. However, there isn’t very great control over the trackpad/touch swipe of the vertical scroll bar. i.e I am not able to stop the scroll in a position while I scroll, it kind of lags and drags a long and sometimes is just completely not scrolling as it should. I adjusted the “scrollInertia” property of the scrollbar, but it did not help much. Any thoughts?
Hi,
Can you test it with the latest development plugin version?
https://github.com/malihu/malihu-custom-scrollbar-plugin/tree/3.x
Hi!
scrollTo (by id) does not always scroll to the top of the element. Sometimes it stops a few px under or above the element.
What could be the reason?
I’d have to see your page/code in order to help. Is your content fully loaded before using scrollTo method?
Am i right that searching for “a string” ctrl+f cmd+f is not jumping to its position, also if it is not visible on the current scroll position?
Searching with Ctrl/cmd+f will briefly disable the scrollbar and content will scroll/jump to the string. Initiating a scroll again (e.g. via mouse-wheel) will re-enable the custom scrollbar.
do i have to implement this, or is this functionality of your plugin. Because at the moment it is not working.
It’s a plugin functionality. Do you test it on Firefox? I can see it working on Chrome, IE, Opera etc.
Can you provide an example, cause’ it is also not working on your demo page for me. (Chrome)
Which plugin version are you using?
Example:
Go to this example. Hit Ctrl+f and search for the string “Quis autem”. The string(s) should be visible and highlighted in the content.
No sry, this one is not working either chrome or firefox (using macOSX). Of course, i was trying to search via cmd-f for “End of content.”
Can you try again the example? It uses the latest plugin version.
No sry, nothing changed. Still not useable with FF or Chrome (macOSX)
Something new? It should be easy reproducible.
The thing is that I cannot reproduce the issue.
I’ve tested it on Windows and OSX (Yosemite) in Safari (8), Chrome (46) and latest Firefox. It works in all browsers except Firefox, which is something that cannot be fixed as it’s about how Firefox prevents scrolling on hidden elements.
In every other browser it does work when I test it.
http://codepen.io/anon/pen/qOxded
Hi Malihu,
i m using ajax in my website and injecting content only while user clicking on menu item. Some times it is updating perfectly and but some times it was not working. Is there any refresh event for the plugin. I have gone through the plugin documentation but unable to find a proper way.
Also i tried to recall the plugin through ajax success event but it is also not working well.
Please remember it was working well at some points and some times not.
actually the scroll bar is missing while the user populating another group
Refresh event is the update method, e.g.:
$(selector).mCustomScrollbar("update");
I don’t know how your ajax function work or which scrollbar options you’re using (or the plugin version) but the archive contains an ajax example:
https://github.com/malihu/malihu-custom-scrollbar-plugin/blob/master/examples/ajax_example.html
Maybe it helps(?)
Hi Malihu,
actually second time dom generating
mCustomScrollbar _mCS_4 class and some times mCS_no_scrollbar classs too.
like this.
i written a global function for the scrollbar and calling it when ajax status success.
Sorry, I’m really new to this.
I’ve notice that you said, png file (buttons) should be on my directory. Does it mean that this should be in the same folder with the html file?
No problem 🙂
It should be on the same folder with the jquery.mCustomScrollbar.css file.
Hi! Somebody can help me???! I have mCSB container, and when cursor located over this container – scrolling with mousewheel don’t work!!! This scroll work only when cursor located on scrollbar. What’s wrong??
Need some more info… in what kind of element do you apply the scrollbar? Are there other scripts/plugins on the same element?
so, there are -list in mcsb_container. And exactly on this element my scrolling is not work.
http://loginov.getphoto.dev1.msoft.su/personal/card/order/?without_reg=Y
in form “Адрес доставки”. -> field (where default “Пермский край”)
Sorry but I cannot find where custom scrollbar is used…
Which form are you referring to? What do I need to do to see the scrollbar in action?
Hi,
Love your plugin and have used it several times.
Now I need something different and I wanted to try and achieve it using this plugin.
I have a horizontal scrolling div that I would like to scroll only when user scrolls vertically. I want to trigger x-scroll using only y-scroll.
Is that possible using your plugin?
Is it easy to implement it?
If yes, could you show me a starting point?
Thanks
Johnny
Hi Johnny,
I don’t think it’s possible, at least not without modifying plugin’s main script.
When you say x-scroll, you mean content touch-swipe scrolling, trackpad scrolling or both?
EDIT: Also, I don’t know how your page works but if we could do it, would this be user-friendly?
Hi,
Thank you for the prompt answer!
When I say x-scroll I only mean mouse input using either scrolling or trackpad (good point thanks) I would like to keep touch-swipe functionality as is.
Do you think it would be relatively easy for me to modify the code to achieve the above? (by giving me a starting point?)
I was thinking of making an adapter for mouse wheel event and prevent default on the x-scroll and only use y-scroll .. or something like that.
Thanks
J
In jquery.mCustomScrollbar.js try changing line 1471 from:
dlt=e.deltaX || e.deltaY || delta;
to:
dlt=e.deltaY;
and see if it does what you need.
Hello Malihu,
I’m trying to implement JCCS and it works ok, the only thing is that if I have a container with the height of 100px and the images max-eight 100% the scripts rewrite the css at the end of the page load. this happen in firofox and chrome, in safari it works ok.
Is it a vertical or horizontal scrollbar? If the container height is fixed (100px) maybe you can set your images to
max-eight: 100px
instead of 100%?Can you send me link or test page/JSFiddle?
Sorry Malihu, It’s on my localhost.
Gonna paste the code here, The contaniner is responsive so is using max-height 100% so the images. Works only with px or vh but it becomes static or acting weird.
.GalleryWrapper { position:relative; width:100%; padding-bottom:30%; height:0; overflow:hidden; } .GalleryContent { position:absolute; overflow: inherit; background: #fff none repeat scroll 0% 0%; margin: 0px; height: 100%; box-sizing: border-box; display: block; float: left; } .GalleryImage{ max-height:100%; float: left;}
<div class="GalleryWrapper"> <!-- content --> <div class="GalleryContent"> <img class="GalleryImage" src="/wp-includes/images/store/01.jpg"/> <img class="GalleryImage" src="/wp-includes/images/store/02.jpg"/> <img class="GalleryImage" src="/wp-includes/images/store/03.jpg"/> <img class="GalleryImage" src="/wp-includes/images/store/04.jpg"/> <img class="GalleryImage" src="/wp-includes/images/store/05.jpg"/> <img class="GalleryImage" src="/wp-includes/images/store/06.jpg"/> <img class="GalleryImage" src="/wp-includes/images/store/07.jpg"/> <img class="GalleryImage" src="/wp-includes/images/store/08.jpg"/> <img class="GalleryImage" src="/wp-includes/images/store/09.jpg"/> </div> </div>
and to note that with the code above in safari works like a charm.
the result I need is soethins similar to this
http://www.kinfolk.com/
thank you for your help!
I’m assuming you apply the scrollbar on
.GalleryContent
element(?)If yes, try giving it a width value, e.g.
.GalleryContent{ width: 100%; }
I’m also assuming you’ve enabled autoExpandHorizontalScroll option(?)
hello, i have a problem with this plugin jquery.mCustomScrollbar.concat.min.js on firefox 40.0.3,
the method mCustomScrollbar(“scrollTo”,”100px”)doesn’t work all the time , here is my code
$("#areaPageView").mCustomScrollbar("scrollTo","500px");
i add dinamicly content by scrolling down or up, i would like ,in case of scrolling down,
to scroll before bottom after adding the ajax content on page, so user understand that there is more content to load, i used the alwaysTriggerOffsets:false to prevent multiples triggers
i have the same probleme on 9.0.8112.16421,
i hope i was clear, and thank you in advance for you answeer,
M’Barek
Without seeing your ajax code I can’t really say why this happens but you need to make sure you call the scrollTo method after the new content is fully loaded (this includes newly added images etc.). Otherwise, the script will cancel scrollTo method while its updating the scrollbar to accommodate for the new content. I’m not sure this is the case for you. If it is, you might also use the scrollTo method’s timeout option to delay it.
Hi Malihu,
First of all thx for your great plugin.
But there is a small problem with the drag.
Once i clicked on the drag from any point of view port i can drag and scroll the content. I want to stop scrolling once i focus out from the drag button. How can i do it? actually i m trying to fix it for horizontal scroll bar.
The scrollbar should be dragged when you click on it. What do you mean by “Once i clicked on the drag from any point of view port…”?
Also, do you want to drag-scroll only when cursor is exactly over the scrollbar? This is not very user-friendly(?)
Hi there,
I want to use this for a grid but to work the same way your thumbnail scroll does .. and to have the arrows only for the horizontal scroll ( gallery browsing type ) .. I need to set the arrows somewhere bellow the th and lets say if I have 100 records the arrows should follow when I scroll vertically ..
Can I achieve that with this plugin ? Thank you very much, I hope you can respond fast : )
Also I dont need the dragger .. only the arrows
Probably yes. It would require some special CSS (e.g. to hide the scrollbar, position the buttons etc.) and it also might require few callbacks and/or options set.
If you can create and send me the HTML along with some visualization (e.g. few images) showing how exactly you want it to work, I could probably make it work for you.
Thanks for the quick response.
I managed to hide the scroll bar .. the problem is that I want to set bigger arrows like 48×48 .. if I modify the container’s height I’m afraid it will prevent me from selecting the row ( selectable rows grid ) .. also can I modify the speed of the arrows on click ? Like in your thumbnail scroll .. click-25 .. click-50 .. something like that is what I’m looking for.
I managed to make the arrows follow the vertical scroll .. I’ve sent you a picture of how it looks so far.
So I really need to remove the dragger so it wont create any problems .. this is how I initialize the plugin:
$(“.grid-selector”).mCustomScrollbar({
axis:’x’,
alwaysShowScrollbar: 0,
scrollButtons:{ enable: true },
mouseWheel:{ enable: false },
autoDraggerLength:false,
theme:”dark”
});
Did you get my email ?
So what’s needed yet ..
-increase scrolling for arrows (I see it’s 1px / click now )
-remove dragger (hidden works but it overlaps the grid and I can select the row under it )
-hide left arrow after page loads and hide right arrow when we reach the end with the scrolling
Thank you : )
Yeap. Just replied 😉
Thanks a ton, that has been really helpful.
Cheers : )
Can I use this with a WordPress site I am working on? I have webkit scrollbars now, but they do not help in FireFox so if your plugin can be included in WP, I will try. Do I need to enqueue the jquery in functions.php, or can I just link it in the header?
Sure.
The best way is to enqueue the files in your functions.php.
Create a custom-scrollbar folder in your theme’s (or child-theme’s) directory and upload plugin files in it.
Open your functions.php and insert:
add_action('wp_enqueue_scripts', 'custom_scrollbar_files' ); function custom_scrollbar_files(){ // plugin css wp_enqueue_style('custom-scrollbar-styles', get_template_directory_uri().'/custom-scrollbar/jquery.mCustomScrollbar.css', '3.1.0'); // jquery wp_enqueue_script('jquery'); // plugin script wp_register_script('custom-scrollbar-script', get_template_directory_uri().'/custom-scrollbar/jquery.mCustomScrollbar.concat.min.js', array('jquery'), '3.1.0', 1); wp_enqueue_script('custom-scrollbar-script'); }
For child-themes change:
get_template_directory_uri()
to:
get_stylesheet_directory_uri()
Then, you can initialize the scrollbar via HTML or javascript. For example, to add the scrollbar with javascript, you could insert something like the following script in your theme’s footer.php below
wp_footer()
function<script> (function($){ $(window).load(function(){ $(selector).mCustomScrollbar(); }); })(jQuery); </script>
or you could add the above (without the
script
tags) inside a new .js file in custom-scrollbar folder and enqueue it by adding it incustom_scrollbar_files
function in functions.php// plugin init script wp_register_script('custom-scrollbar-init-script', get_template_directory_uri().'/custom-scrollbar/your-filename.js', array('jquery', 'custom-scrollbar-script'), '3.1.0', 1); wp_enqueue_script('custom-scrollbar-init-script');
Thank you so much for the detailed instructions!!
I am going to do this soon, hope I won’t have to bug you for more help!!
Much appreciated.
http://www.annweiner.com/sculpture/
For a reason I can’t find, when this site opens in Safari the horizontal scrollbars for the long thumbnail divs do not appear. But if I resize the browser window they pop right in.
When you say Safari you mean the OSX/desktop or iOS/mobile version?
desktop.
I have the script in the footer like this:
(function($){
$(window).load(function(){
$(“.heroText”).mCustomScrollbar();
axis:”y”,
scrollbarPosition: “outside”
});
})(jQuery);
(function($){
$(“.TNHolder “).mCustomScrollbar({
axis:”x”,
scrollbarPosition: “outside”
});
})(jQuery);
seems I should be able to consolidate it, but when I try it breaks.
The script tags were not copied into that code I pasted, but each function is wrapped in script tags, and that seems not quite efficient- however, without both it ceases to function properly.
To post code in comments you need to use the code tag (see info below the comment field).
The first part of your code is not correct. It should be:
$(".heroText").mCustomScrollbar({ axis:"y", scrollbarPosition: "outside" });
Plugin options should be inside
mCustomScrollbar
function (like in the second part of your code).Thank you,
I repaired that, but still Safari does not open the horizontal scrollbar on the .TNHolder divs until I resize the window. Works fine in other browsers. Odd.
And the vertical one works just fine in Safari.
This is the code:
It doesn’t even use $(window).load
(image heavy site)
(function($){
$(“.TNHolder “).mCustomScrollbar({
axis:”x”,
scrollbarPosition: “outside”
});
})(jQuery);
<script> (function($){ $(window).load(function(){ $(".heroText").mCustomScrollbar({ axis:"y", scrollbarPosition: "outside" }); })(jQuery); </script> <script> (function($){ $(".TNHolder ").mCustomScrollbar({ axis:"x", scrollbarPosition: "outside" }); })(jQuery); </script>
posted correctly here I hope
Hey,
I think I fixed it, it now works in Safari as well as elsewhere. I added the
setWidth:false
option and now it is fine. I think that div had a max width in the css, so that may have been the problem, anyway, it works and I want to thank you again for your great and very useful work and your attention to your public!Eve
I spoke too soon, closed and re-opened Safari and re-tested and still no horizontal scrollbars on the thumbnail div! :>((
I will try whatever you tell me.
here is code:
<script> (function($){ $(window).load(function(){ $(".heroText").mCustomScrollbar({ axis:"y", scrollbarPosition: "outside" }); })(jQuery); </script> <script> (function($){ $(".TNHolder ").mCustomScrollbar({ axis:"x", setWidth: false, scrollbarPosition: "outside" }); })(jQuery); </script>
verticals are perfect!!
Try calling both on window load and enable autoExpandHorizontalScroll option on the horizontal scrollbar:
<script> (function($){ $(window).load(function(){ $(".heroText").mCustomScrollbar({ axis:"y", scrollbarPosition: "outside" }); $(".TNHolder ").mCustomScrollbar({ axis:"x", scrollbarPosition: "outside", advanced:{autoExpandHorizontalScroll:true} }); }); })(jQuery); </script>
Hello, on beginning thanks for that great script! I was using it for the last year for showing images in my web portfolio and was happy with it. However the older version couldn’t offer scrolling via keyboard I decided to upgrade to the newest version. After some problems I finally run new script and almost everything is ok. Almost because I have two issues which I can’t manage. The first problem is themes… If I set for example the “rounded-dots” I can’t see the arrows on both ends – just dots – it is with all of them – I can see them in demo pages.
The second issue is more important for me. In new script I can’t show image using scrolling when image width is bigger then DIV field. For example DIV is 900px width but image is 1000px. It is working on old script but not in new one. Maybe I have to add something in configuration but I don’t know what… I hope I explained clear enought. If no please look on the source code in:
http://busiakiewicz.com/index.php?page=exteriors – there is old script and it is ok, while in this link:
http://busiakiewicz.com/index1.php?page=exteriors there is new script and script is showing only 1 image. The problem is definitely the last image which is longer than DIV
thanks in advance
Hello,
To enable the buttons use the scrollButtons option parameter like this:
scrollButtons:{ enable: true }
and make sure you have mCSB_buttons.png on your directory.
For the second issue I’ll need to investigate further but try setting autoExpandHorizontalScroll value to
2
(integer).So, make your script like this:
$("#portfolio_image").mCustomScrollbar({ axis:"x", theme:"rounded-dots", advanced:{ autoExpandHorizontalScroll: 2 }, scrollButtons:{ enable: true } });
Let me know if it works
Thank You so much for that quick respond to my issue. It’s working like a charm! I spend good few hours on it trying to find solution and it was just a small thing…
There is one more thing which I am thinking about and trying to do – is it possible to automatically activate keyboard scrolling (using arrows?) at this moment I have to “click” on the image first and than I can use arrows… The same with the mouse-wheel – it scrolls images only when the pointer is over the image.
Thanks again!
Yes this is the standard behavior (it simulates exactly what browser’s default scrollbar does).
You’ll need to add few lines of extra code to achieve those.
For the mouse-wheel (check this example), you need something like:
$("#another-element").on("mousewheel",function(e,delta){ e.preventDefault(); $("#portfolio_image .mCustomScrollBox").trigger("mousewheel",[delta]); });
#another-element
being the element to trigger the mouse-wheel when hovered.For the keyboard arrows, the following will work on any element with custom scrollbars (as it is):
$(".mCustomScrollBox").on("mouseenter",function(){ $(this).attr("tabindex","0").focus(); }).on("mouseleave",function(){ $(this).blur(); });
Just make sure you don’t break the user experience especially with the mouse-wheel functionality 🙂
Ok I checked your markup a bit further. Setting autoExpandHorizontalScroll to
2
(as suggested) will work.Another way is to set it to
true
(as you had it before) but you need to set your element’s CSS towhite-space: nowrap;
, e.g.:#portfolio_image{ overflow-x: auto; white-space: nowrap; }
I’ve recently updated the autoExpandHorizontalScroll function (in version 3.1.0) to act exactly like browser’s default scrollbar on overflowed-x content. This means that according to your elements CSS rules you can set its value (in addition to
true
/false
or1
/0
) to2
or3
for more control on how elements are expanded horizontally (see option info).In some cases this might require a specific value (in your case
2
), so you just got a bit unlucky there 🙂Good feedback though so thank you!