Shohidul Alam
Shohidul Alam

Reputation: 703

smooth jquery Ui slider with 5 steps

I am working on a project in which I have several jq UI slider. I have a special one which have only 5 steps. Here is the fiddle and Here is the snippet(First fiddle and snippet):

$(function() {
    $( "#slider" ).slider({
        min: 1,
        range: false,
        max: 5,
        value: 1,
        animate:"slow",
        orientation: "horizontal",
        slide: function( event, ui ) {
            $(".value").text("slider value: " + ui.value);
        }
    });
});
body {
    padding: 0px;
    margin: 0px;
    background-color: #333;
    color: #FFF;
    font-family: arial;
    font-size: 20px;
}
.slider-holder {
    padding: 15px;
    margin: 0px;
}
.value {
    margin: 15px 0px 0px 0px;
    display: inline-block;
}
<html>
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
  <link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
</head>
<body>
      <div class="slider-holder">
        <div id="slider"></div>
        <a class="value">slider value: 1</a>
    </div>
</body>
</html>

I want slide smooth like this jsfiddle and this snippet(First fiddle and snippet):

$(function() {
    $( "#slider" ).slider({
        min: 1,
        range: false,
        max: 1000,
        value: 1,
        animate:"slow",
        orientation: "horizontal",
        slide: function( event, ui ) {
            $(".value").text("slider value: " + ui.value);
        }
    });
});
body {
    padding: 0px;
    margin: 0px;
    background-color: #333;
    color: #FFF;
    font-family: arial;
    font-size: 20px;
}
.slider-holder {
    padding: 15px;
    margin: 0px;
}
.value {
    margin: 15px 0px 0px 0px;
    display: inline-block;
}
<html>
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
  <link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
</head>
<body>
      <div class="slider-holder">
        <div id="slider"></div>
        <a class="value">slider value: 1</a>
    </div>
</body>
</html>

I want when user click and hold the slider handle and move the handle, It should move clearly like fiddle and snippet 2. But it's only have 5 values.
How can I do It?

Upvotes: 2

Views: 10022

Answers (3)

Imran Bughio
Imran Bughio

Reputation: 4931

A small improvise on @PVL answer, I have added transition to the fiddle.

It will animate to the step you have covered once you user leave the handle.

Click here to view

CODE:

$(function() {
  var slider = $( "#slider" ).slider({
    min: 1,
    range: false,
    step: .0001,
    max: 5,
    value: 1,
    animate:"slow",
    orientation: "horizontal",
    slide: function( event, ui ) {
      $(".value").text("slider value: " + Math.round(ui.value));
    },
    stop: function( event, ui ) {
      $("#slider").slider('value',Math.round(ui.value));
      console.log(ui);
    }
  });
});
body {
    padding: 0px;
    margin: 0px;
    background-color: #333;
    color: #FFF;
    font-family: arial;
    font-size: 20px;
}
.slider-holder {
    padding: 15px;
    margin: 0px;
}
.value {
    margin: 15px 0px 0px 0px;
    display: inline-block;
}
span.ui-slider-handle.ui-state-default.ui-corner-all {
    transition: all .3s;
}

span.ui-slider-handle.ui-state-default.ui-corner-all.ui-state-active {
    transition: none;
}
<html>
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
  <link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
</head>
<body>
  <div class="slider-holder">
    <div id="slider"></div>
    <a class="value">slider value: 1</a>
</div>
</body>
</html>

Upvotes: 3

Czettner S&#225;ndor
Czettner S&#225;ndor

Reputation: 21

The problem with using step with .0001 is that the user could select values between integers. An easier approach would be CSS transitioning:

.ui-slider-handle {
    transition-property: left;
    transition-duration: 0.15s;
}

Fiddle example

Upvotes: 1

PVL
PVL

Reputation: 587

Here you go :P

 $(function() {
        $( "#slider" ).slider({
            min: 1,
            range: false,
            step: .0001,
            max: 5,
            value: 1,
            animate:"slow",
            orientation: "horizontal",
            slide: function( event, ui ) {
                $(".value").text("slider value: " + Math.round(ui.value));
            }
        });
    });

Btw if you want even smoother effect go for step: .001 for example.

Upvotes: 7

Related Questions