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

Slow Android emulator

I have a 2.67 GHz Celeron processor, 1.21 GB of RAM on a x86 Windows XP Professional machine. My understanding is that the Android emulator should start fairly quickly on such a machine, but for me it does not. I have followed all instructions in setting up the IDE, SDKs, JDKs and such and have had some success in staring the emulator quickly but is very particulary. How can I, if possible, fix this problem?

CCNA 3 Final Exam => latest version

1 . Which security protocol or measure would provide the greatest protection for a wireless LAN? WPA2 cloaking SSIDs shared WEP key MAC address filtering   2 . Refer to the exhibit. All trunk links are operational and all VLANs are allowed on all trunk links. An ARP request is sent by computer 5. Which device or devices will receive this message? only computer 4 computer 3 and RTR-A computer 4 and RTR-A computer 1, computer 2, computer 4, and RTR-A computer 1, computer 2, computer 3, computer 4, and RTR-A all of the computers and the router   3 . Refer to the exhibit. Hosts A and B, connected to hub HB1, attempt to transmit a frame at the same time but a collision occurs. Which hosts will receive the collision jamming signal? only hosts A and B only hosts A, B, and C only hosts A, B, C, and D only hosts A, B, C, and E   4 . Refer to the exhibit. Router RA receives a packet with a source address of 192.168.1.65 and a destination address of 192.168.1.161...