jQuery UI slider for dynamic content

On one of the projects I’ve worked on, we used jQuery UI slider for dynamic content. The slider’s handle needed to resize according to that dynamically loaded content.

The main issue was that with the usual suggested ways to do it, there was a jump for first mouse initiated movement on the slider. That rather ugly behavior is related to the slider’s requirement that the slider bar and handle parent must be of same width[1]. That, in combination with the requirement to give the handle a half margin offset[2] due to handle sizing, causes that ugly jump to occur.

This is how the default setup looks like:

jquery-ui-slider-dynamic-content-before

You can see that the bar and the handle parent are of the same size. Slider is initialized on bar element.

The fix is rather simple. Instead of initializing the slider on bar element, do it on ui-handle-helper-parent. Then you can size that element as needed, and the slider does not exhibit the ugly jump. The ui-handle-helper-parent element needs to be centered in bar and a bit of custom styling is needed, but in the end it works nicely. This is how the setup looks like after the fix:

jquery-ui-slider-dynamic-content-after

Due to peculiar sizing of the ui-handle-helper-parent element, another fix was needed as well. When clicking on the area of bar element that the ui-handle-helper-parent element doesn’t cover, the slider value needs to be recalculated and set accordingly. All of these changes and fixes are packaged in the DynamicSlider[4] component and there is a working example[3].

Resources:

  1. Jquery UI Scroll Bar not fitting the contents
  2. jQuery UI slider jumps
  3. A working example @ CodePen
  4. The gist of the example
Advertisements
Tagged , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: