Skip to main content

Jquery UI slider how to make a box follow the handler?


I am trying to create a div that should follow the handler as it is seen here: http://m1.dk/Priser/#tab:#calc,#abb



My JSfiddle: http://jsfiddle.net/z3xV3/2/



I also want to make a calculation on the UI value that should appear in the box.



Here is a illustration what I want to achieve: enter image description here



HTML:




<div id="slider"></div>
<input id="sliderValue" />



Jquery:




$(document).ready(function() {


// Pris slider
$("#slider").slider({value:'',min: 0,max: 150,step: 1, range: 'min',
slide: function( event, ui ) {
$( "#amount" ).html( ui.value + ' timer');
}
});

$( "#amount" ).val( "$" + $( "#slider" ).slider( "value" ) );


});


Source: Tips4allCCNA FINAL EXAM

Comments

  1. I hope that's the effect you're looking for: http://jsfiddle.net/z3xV3/11/

    JS

    $(document).ready(function() {

    $("#slider").slider({value:'', min: 0, max: 150, step: 1, range: 'min'});

    var thumb = $($('#slider').children('.ui-slider-handle'));
    setLabelPosition();

    $('#slider').bind('slide', function () {
    $('#sliderValue').val($('#slider').slider('value'));
    setLabelPosition();
    });

    function setLabelPosition() {
    var label = $('#sliderValue');
    label.css('top', thumb.offset().top + label.outerHeight(true));
    label.css('left', thumb.offset().left - (label.width() - thumb.width())/ 2);
    }

    });


    HTML

    <div id="slider"></div>
    <input id="sliderValue" />


    CSS

    #sliderValue {
    position:absolute;
    width: 50px;
    }


    Best regards!

    ReplyDelete
  2. It's actually quite easy (and very useful) to turn this into a simple jQuery plugin. Like so:

    JSFiddle: http://jsfiddle.net/PPvG/huYRL/

    Note that I've added support for moving the slider by entering a value into the #sliderValue.

    [ Code sample removed ]

    You could also choose to remove #sliderValue from the HTML and let the plugin create it for you. In that case, you would rewrite the plugin so it would be called on the slider, instead of on #sliderValue. In my opinion, that would be a much neater solution.

    Update

    Based on your comments (and re-reading your question), I've rewritten the plugin example.

    If I understand correctly, you want to do some calculations based on the value of the slider and display the result of those calculations in the label below the slider handle. The following should make that really easy.

    JSFiddle: http://jsfiddle.net/PPvG/q5qg7/

    HTML

    <div id="slider"></div>


    Plugin

    (function($) {

    $.fn.sliderLabel = function(options) {

    var settings = $.extend({
    marginTop: 2,
    // Margin between the slider handle and the label (in pixels)
    callback: function(value) {
    return 'Value: ' + value;
    }
    }, options);

    return this.each(function() { // <- maintains chainability
    var slider = $(this);
    var handle = slider.children('.ui-slider-handle');
    var data = slider.data('sliderLabel');

    if (handle.length === 0) {
    // $(this) isn't a slider (it has no handle)
    console.log('[SliderLabel] Error: sliderLabel() can only be called on a slider.');
    return;
    }

    if (data === undefined) {
    // First time sliderLabel() is called on this slider
    data = {
    label: $('<div class="ui-slider-label" />').css('position', 'absolute'),
    callback: settings.callback
    };

    data.label.insertAfter(slider);

    function updateLabel() {
    var value = slider.slider('value');
    var labelText = data.callback(value);

    data.label.html(labelText);
    data.label.css('top', handle.offset().top + handle.outerHeight() + settings.marginTop);
    data.label.css('left', handle.offset().left - ((data.label.width() - handle.width()) / 2));
    }

    updateLabel();
    slider.bind('slide', updateLabel);

    } else {
    // sliderLabel() was called before; just update the callback:
    data.callback = settings.callback;
    updateLabel();
    }

    // Save (or update) the data inside the slider:
    slider.data('sliderLabel', data);
    });
    }
    })(jQuery);


    Usage

    $("#slider").slider();

    $("#slider").sliderLabel({
    callback: function(value) {
    return value + '%';
    }
    });


    As you can see, the plugin is now called on the slider itself. You can now provide the plugin with a callback, which allows you to do some calculations and return a correctly formatted label (HTML is allowed).

    In the example above, the callback simply takes the value that is passed to the callbackand appends '%'.

    ReplyDelete
  3. It would be nice to take a look at alternative solutions with jQuery plugins such as:


    http://craigsworks.com/projects/qtip2/demos/#ui-slider
    http://www.filamentgroup.com/lab/update_jquery_ui_slider_from_a_select_element_now_with_aria_support/

    ReplyDelete
  4. Taking your jsfiddle.

    HTML:

    <div id="slider"></div>
    <input id="sliderValue" />


    CSS:

    #sliderValue{width:50px;}

    Jquery:

    $(document).ready(function() {


    // Pris slider
    $("#slider").slider({value:'',min: 0,max: 150,step: 1, range: 'min',
    slide: function( event, ui ) {
    $( "#sliderValue" ).val( ui.value + ' timer');
    var offset = $(this).find('a').css("left");
    $("#sliderValue").css("margin-left", offset);
    }
    });

    $( "#sliderValue" ).val($( "#slider" ).slider( "value" ));


    });

    ReplyDelete
  5. You can just: http://jsfiddle.net/z3xV3/21/

    $(document).ready(function() {
    $("#slider").slider({
    value:0,
    min: 0,
    max: 150,
    step: 1,
    range: 'min',
    slide: function( event, ui ) {
    var offsetLeft = $(this).find(".ui-slider-handle").offset().left;
    $( "#sliderValue" )
    .val( ui.value + ' timer')
    .css({
    "position": "absolute",
    "left": offsetLeft
    });
    }
    });
    });


    The slide callback is called every time you move your handle:

    offsetLeft is the offset of the handle.

    $( "#sliderValue" ) is your input.

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex