Code Monkey home page Code Monkey logo

jquery-nstslider's Introduction

jquery.nstSlider.js

Fully customizable with CSS, Single/Double handles, Touch-enabled, IE 7+ Compatibility, Custom Digit Rounding, Non linear step increments!

Build Status

Example

Initialize with:

$(".mySlider").nstSlider({
    "left_grip_selector": ".leftGrip",
    "right_grip_selector": ".rightGrip",
    "value_bar_selector": ".bar",
    "value_changed_callback": function(cause, minValue, maxValue, prevMinValue, prevMaxValue) {
        // show the suggested values in your min/max labels elements
    }
});

Method call:

$(".mySlider").nstSlider("set_position", 10 /* min */, 90 /* max */);

Destroy with:

$(".mySlider").nstSlider("teardown");

Demo

For live demos please visit the project webpage:

http://lokku.github.io/jquery-nstslider/

For a Quick Start have a look at the source html of the following file:

https://github.com/lokku/jquery-nstslider/blob/master/demo/index.html

Options

Option Type Default Description
animating_css_class string nst-animating the css class to be used when the slider is to be animated (this happens when a certain min/max value is being set for example).
touch_tolerance_value_bar_x number 15 the horizontal tolerance in pixels by which a handle of the slider should be grabbed if the user touches outside the slider bar area.
touch_tolerance_value_bar_y number 30 the vertical tolerance in pixels by which a handle of the slider should be grabbed if the user touches outside the slider bar area.
left_grip_selector string .nst-slider-grip-left the selector of the left grip handle. The left grip element must exist in the page when the slider is initialized.
right_grip_selector string undefined the selector of the right grip handle. This is optional. A single handler bar is assumed if this selector is not specified.
value_bar_selector string undefined the selector of the value bar. If not specified assumes a value bar representing the selection is not wanted.
rounding object or number 1 the rounding for a certain value displayed on the slider. This rounds the values returned in the value_changed_callback as roundedValue : int(actualValue / rounding) * rounding. The rounding parameter can be a number (i.e., fixed rounding) or can depend on actualValue (i.e., dynamic rounding). To perform dynamic rounding an object must be passed instead of a value. For example, passing rounding : { '1' : '100', '10' : '1000', '50' : '10000' } will use rounding = 1 when actualValue <= 100, rounding = 10 when 100 < actualValue <= 1000 and so on...
crossable_handles boolean true Allow handles to cross each other while one of them is being dragged. This option is ignored if just one handle is used.
value_changed_callback function function(cause, curMin, curMax, prevMin, prevMax) { return; } a callback called whenever the user drags one of the handles.
user_mouseup_callback function function(vmin, vmax, left_grip_moved) { return; } a callback called whenever the mouse button pressed while dragging a slider grip is released
user_drag_start_callback function function () { return; } a callback called before the user drags one of the handles

Methods

When calling methods, use positional arguments. For example, for the set_position method, call:

$(".mySlider").nstSlider("set_position", 10 /* min */, 90 /* max */);

do not call:

$(".mySlider").nstSlider("set_position", { min: 10, max: 90 });

unless the documentation says that the first argument is an object.

Method Arguments (positional) Description
get_range_min None return the current minimum range of the slider
get_range_max None return the current maximum range of the slider
get_current_min_value None return the current minimum value of the slider
get_current_max_value None return the current maximum value of the slider
is_handle_to_left_extreme None return a boolean indicating whether or not the left handler is moved all the way to the left
is_handle_to_right_extreme None return a boolean indicating whether or not the right handler is moved all the way to the right
refresh None force a refresh of the slider
disable None disable the slider (i.e., user cannot move the handles)
enable None enable the slider (i.e., user can move the handles)
is_enabled None return a boolean indicating whether or not the slider can be moved by the user
set_position min: number, max: number set the handles at the specified min and max values
set_step_histogram histogram : array of numbers use a non-linear step increment for the slider that is stretched where the histogram provided counts more items
unset_step_histogram None use a linear scale of increments for the slider
set_range rangeMin : number, rangeMax : number set the minimum and the maximum range of values the slider
set_rounding rounding: number or object set the rounding for the slider
get_rounding None return the current rounding of the slider
teardown None remove all data stored in the slider
value_to_px number given a value in the range of the slider, returns the corresponding value in pixel relative to the slider width

Dependencies

jQuery 1.6.4+

License

Copyright (c) 2014 Lokku Ltd.

Licensed under the MIT license.

jquery-nstslider's People

Contributors

darksmo avatar luthlee avatar mtmail avatar thetron avatar tony avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

jquery-nstslider's Issues

Switch rounding object definition

I am having a problem:
I have certain values in my range slider where I have to use the same rounding between different ranges. But since the object defines the rounding value as key I cant use the same rounding for different ranges.

Would it make sense to set the ranges as keys and the roundings as values?

Cannot set position programatically

In combination with jQuery 3.1.0 I get the following error when trying to use set_position:

Uncaught Error: cannot compare s: [object Object] with a[145]. a is: [...]

Usage:
$('.nstSlider').nstSlider('set_position', {min:100, max:5000});

Suggestion with non-crossable grips

Given the custom style:
image image

I wanted the handles not to cross, but also not overlap. I understand that this would currently result in the fact that the range values will not be the same on left and right. Which is fine for my use-case.

I modified the function "validateAndMoveGripsToPx", and included a conditional statement tin the if statement (last part:)

  'validateAndMoveGripsToPx': function (nextLeftGripPositionPx, nextRightGripPositionPx) {
            var $this = this;

            //[! edit:
            settings = $this.data('settings'); // added settings to get crossable_handles value
            // end edit !]
            var draggableAreaLengthPx = _methods.getSliderWidthPx.call($this) - $this.data('left_grip_width');

            //
            // Validate & Move
            //
            if (nextRightGripPositionPx <= draggableAreaLengthPx &&
                nextLeftGripPositionPx >= 0 &&
                nextLeftGripPositionPx <= draggableAreaLengthPx &&
                (!$this.data('has_right_grip') || nextLeftGripPositionPx <=
                nextRightGripPositionPx
                // [! edit:
                // subtract the grip width to let the handles stop when they touch..
                - (!settings.crossable_handles ? $this.data('left_grip_width') : 0)
                // end edit!]
                )) {

                var prevMin = $this.data('cur_min'),
                    prevMax = $this.data('cur_max');

                // note: also stores new cur_min, cur_max
                _methods.set_position_from_px.call($this, nextLeftGripPositionPx, nextRightGripPositionPx);

                // set the style of the grips according to the highlighted range
                _methods.refresh_grips_style.call($this);

                _methods.notify_changed_implicit.call($this, 'drag_move', prevMin, prevMax);
            }

            return $this;
        },

Of course it would be beter if this was an additional setting, because now when not allowing to cross, it will always force the 'no touch' policy (actually a better name would be 'touch slightly only' 😄 )

rtl support

how can i use your slider to low value is right and high value is in left (RTL support)

width of grip not calculated correctly (in certain cases..)

Hi,

great plugin! Am in the process of customising it with a different look and feel:

image

And it seems that you use "width()" to get the current width of the grip. However i modified the grip to include a padding, but that 'breaks' the right grip, in that it will extend beyond the container.

I modified lines 192 and 204 (getLeftGripWidth, getRightGripWidth) to use outerWidth() instead of width(). Which does the trick. Any reason you use width (that may break something in the future) for me?

have another issue, but will create a seperate issue for that :)

'is_handle_to_right_extreme' never true with single handle.

I would like to set up a single handle slider, range between 0 and 1000. When range = 1000, I would like the method 'is_handle_to_right_extreme' to return true when the handle reaches 1000, but currently, it does not. The 'is_handle_to_left_extreme' does return true when the handle is on the left (0).

Add bower_components/ to .gitignore

Would it be okay to add bower_components/ to .gitignore? I don't think that you need to keep versions of the libraries installed by bower given the fact that everyone should run bower install when cloning the repo.

Improve accessibility of slider

Copied from my comment on Reddit. Please let me know if you prefer separate issues for the aria and for keyboard access.

It'd be nice if you could make it accessible which, in this case isn't difficult.
First, give it a 'role' of 'slider'.
Instead of using data-* attributes, you could use WAI-ARIA.
Instead of data-range_min, use aria-valuemin
Instead of data-range_max, use aria-valuemax
Represent the current value with 'aria-valuenow'. If you have a text-based representation of the value, also add 'aria-valuetext'.
Also, add tabindex=0 on the "grips" so they can be interacted with via the keyboard. Allow the value to be modified via keyboard: left/ down arrow should decrease the value and right/ up arrow should decrease the value.
jQueryUI does a good job on keyboard accessibility of their slider, but I always have to add the ARIA stuff whenever I use it. It'd be cool to have something like yours as an alternative.

Unable to get value from slider

As far as I know this is not documented yet. I am unclear on how to get a value from the slider into my other functions to do calculations with. Apologies in advanced, I am new to web designing. Your implementation was a super solution to one of my problems of getting a pretty looking slider.

Support for float steps - Not only integers

Hello Savio.
Here is the issue I found
The steps in between the starting/end point are always integers.

So if I have a slider that is min/max 0 1 and I drag to about 33% to the right,
the returned value is 0 and not 0.3

http://jsfiddle.net/28YCS/34/

Can I do something to overcome this?

On the same fiddle, you will see that the returned value behaves differently for the negative and the positive.
If i drag to 60% to the right, the value from 0 goes to 1. If i drag to the left, it only goes to -1 on the far left.

Thank you

bower.json doesn't contain main CSS file

The grunt-wiredep auto-removes link to plugin's styles sheet, because of the absence of distribution CSS file path in the bower.json "main" section.
Was that intentional?
Do you think I can add it and make a pull request?

user_mouseup_callback work incorrent

Hi! I have two progress bar in page. I click progress bar and volume bar, the user_mouseup_callback event is not work incorrent.

Here is the console result:
progress drag
progress value changed
progress up
volume drag
volume value changed
progress up

Volume bar up event is not happen.

        $('#volume').nstSlider({
            'left_grip_selector': '#volume-slide',
            'value_bar_selector': '#volume-progress',
            'user_mouseup_callback': function(progress) {
                console.log('volume up');
            },
            'user_drag_start_callback': function() {
                console.log('volume drag');
            },
            'value_changed_callback': function(c, progress) {
                console.log('volume value changed');
            }
        });
        $('#progress-bar').nstSlider({
            'left_grip_selector': '#slide-block',
            'value_bar_selector': '#progress-played',
            'user_mouseup_callback': function(progress) {
                console.log('progress up');
            },
            'user_drag_start_callback': function() {
                console.log('progress drag');
            },
            'value_changed_callback': function(c, progress) {
                console.log('progress value changed');
            }
        });

CSS classnames

I noticed that the documentation uses the CSS class slider, but in the CSS file the class is nstSlider.
Should the documentation be updated? Or the CSS? Or should the javascript add the class to the element during initialization?
I have no preference. It was just unexpected when I copy&pasted the example code :)

Vertical orientation

Can it have vertical orientation as well, instead of just horizontal?
thanks.

touch devices scroll issue

When a user scrolls on a touch enabled device ( tested on iPhone 5c, Safari, iOS 9.1) while touching the sliderbar it prevents the user from scrolling even if the handle is outside of the touch_tolerance_value_bar_x / y.

This prevents the user from scrolling and also causes the value to update unintentionally as the handle drops straight to where the user has touched and the value updates.

Init in hidden layer

Hi.
I've a some problem.
If init slider into hidden layer (display:none) and .nstSlider has 100% width I've got wrong rightGrip position.

Not able to use textfields after implemented the .js files

Hey Guys,
i just found out that i'm not able to use my textfields (input type="text") after implementing the jquery.nstSlider.js.

I'm working with XCode 5, and i'm just programming a Cordova iOS7 application. So...yes, maybe some Code would help you:

    <link href="http://fonts.googleapis.com/css?family=Comfortaa" rel="stylesheet" type="text/css">
    <link rel="stylesheet" type="text/css" href="css/jqm-struc-1.4.3.css">
    <link rel="stylesheet" type="text/css" href="css/stylesheet.css">
    <link rel="stylesheet" type="text/css" href="css/jquery.nstSlider.css">
    <script type="text/javascript" src="js/jq-1.11.1.js"></script>
    <script type="text/javascript" src="js/jqm-1.4.3.js"></script>
    <script type="text/javascript" src="js/jquery.nstSlider.js"></script>
    <script type="text/javascript" src="cordova.js"></script>

            <div class="nstSlider" data-range_min="0" data-range_max="20" data-cur_min="5" data-cur_max="-1">
                <div class="bar"></div>
                <div class="leftGrip"></div>
            </div>
        <div class="leftLabel"></div>
            <script>
                $('.nstSlider').nstSlider({
                                          "left_grip_selector": ".leftGrip",
                                          "value_bar_selector": ".bar",
                                          "value_changed_callback": function(cause, leftValue, rightValue) {
                                          var $container = $(this).parent(),
                                          g = 150,
                                          r = 94,
                                          b = 33;
                                          $container.find('.leftLabel').text(leftValue);
                                          $(this).find('.bar').css('background', 'rgb(' + [r, g, b].join(',') + ')');
                                          }
                                          });
                </script>

Think this is a bug, or?

PS: In my Browser (FireFox) it works fine, but not on the iPhone with iOS7

PSS: Sorry, i'll close it now and will ask first on stackoverflow. Maybe my Problem can get solved over there.

Slider not working with Windows 8.1 in IE11 with zoom level greater than 100%

Tested out your preview page inside IE11 with a zoom level at 200%. The sliderball keeps accelerating out of control compared to the mouse movement. This works fine inside both FF and Chrome. Did a check with a easyUI slider, which handles IE with zoom levels just fine.

This is especially troublesome for computers with high screen resolution that are using Windows 8.1's DPI settings to negate the screen size. Default there is to adjust the IE zoom levels from Windows.

From what I can understand, it has something to do with pageX not handling zoom levels in IE very well

Not working with Safari mobile

Hi,
The library is not working properly with Safari mobile (and other mobile browser).
To reproduce: open your demo page with a mobile browser (with no mouse) and drag the cursor.
In the console log you'll see an error like:
TypeError: undefined is not a function (evaluating 'e.preventDefault()')

It's because you event e is empty (no mouse) on tablet or mobile.

Thanks

Floating point array lookup

Clicking in the middle of a slider with a histogram set may generate lookup in the pixel_to_value array with a floating point pixel.

value_changed_callback trying to call a function during initialization

I want use 2 nstSliders and changing one with other. For this i have created 2 sliders:

$sliderOne.nstSlider({
    "left_grip_selector": ".leftGrip",
    "value_changed_callback": function(cause, leftValue, rightValue) {
        $sliderTwo.nstSlider('set_position', 30);
    }
});
$sliderTwo.nstSlider({
    "left_grip_selector": ".leftGrip",
    "value_changed_callback": function(cause, leftValue, rightValue) {
        $sliderOne.nstSlider('set_position', 30);
    }
});

But when refresh page i get Error: method set_position called on an uninitialized instance of nstSlider. So, sliderOne trying to call a function during initialization?

Init with width: 100% fails

Hi,

we just noticed following problem:

We have a responsive website for a customer, so we have to set a width: 100% for the less variable @slider-width. That works quite well, but the initial setup ist wrong.

I´ve set following:

<div class="nstSlider" data-range_min="0" data-range_max="30000" data-cur_min="0" data-cur_max="30000">

result:
image

then when i first try to scale the max jumps to 16552 and i can slide to the right and get my 30000.

image

Any help about this would be really nice.

Cheers

Minimum Range / Distance between handles

I need the user to choose a range between 0 and 100 and the selected range should be more than or equal to 10. This is very common use case. Many sliders implement this feature but I am not sure how this can be done on my favorite nstSlider!

Custom styling, bar background positioning

Given the following customization:
image

I had to add some CSS tricks to get the gray '.bar' aligned correctly on the left side. The rounded corners were starting at the center of the left handle, and not the beginning. On the right side it goes all the way to the right border of the right grip correctly. Zoomed image:

image

i made the grips sem-transparent to see whats going on. The same thing happens with the CSS you supply with the plugin, if you hide the handles, you can see that the bar does not start at the left side.

I fixed it with CSS for now but would be better if i knew where in the code this goes wrong.

Grips don't receive focus when clicked on

If you move the mouse cursor outside the slider while dragging handles, text on the page gets selected. I don't think it was intended that way, and it's kinda distracting.

screenhunter_436 jul 14 10 11

Why not add e.preventDefault(); to the drag_move_func function to prevent text selection?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.