jquery slider with value above it which travels wi

2020-05-24 04:55发布

I've made a slider with query/slider and styled it with css. ( http://jsfiddle.net/9qwmX/ )

I succeeded to put the selected value into a div container; but now I want the container to 'travel' with the slider accordingly. I've tried to use the ui.handle and its css left property which is a percentage. But the behavior is somewhat strange: sometimes it travels with the left side of the slider and sometimes with an offset of 10px.

Question: does anybody know a good way to let the box containing the slided-value travel with the handle?

3条回答
The star\"
2楼-- · 2020-05-24 05:08

You can do something like this to build what you want, I am using event.pageX

var newLeft = 100;
$('#move').text(100).css('margin-left',115);
$("#slider").slider(
{
            value:100,
            min: 0,
            max: 500,
            step: 50,
            slide: function( event, ui ) {
                $( "#slider-value" ).html( ui.value );
                var  pos = (event.pageX > newLeft)? event.pageX + 8 
                                                  : event.pageX - 42;
                newLeft = event.pageX;                
                $('#move').text(ui.value).css('margin-left',pos);
            }
}
);

Demo: http://jsfiddle.net/joycse06/Cw6u8/1/

查看更多
戒情不戒烟
3楼-- · 2020-05-24 05:11

This is how i would have done it. To get you started:

Make use of the "handle" position:

var offset = $('.ui-slider-handle .ui-state-default .ui-corner-all"').offset();
var offsetLeft = offset.left;
var offestTop = offset.top;

Position your slide value box with absolute positioning:

#tijdlijnDatum { position: absolute; }

And do something with it in the slide event:

$( "#slider" ).slider({
   slide: function(event, ui) { 

       $('#tijdlijnDatum').css({ left: offsetLeft + 'px'  }); // etc
   }
});
查看更多
迷人小祖宗
4楼-- · 2020-05-24 05:20

You can just attach a tooltip-like div that you can append to the handle so it moves along with it. You just have to position it with CSS, like so:

JS

var initialValue = 2012; // initial slider value
var sliderTooltip = function(event, ui) {
    var curValue = ui.value || initialValue; // current value (when sliding) or initial value (at start)
    var tooltip = '<div class="tooltip"><div class="tooltip-inner">' + curValue + '</div><div class="tooltip-arrow"></div></div>';

    $('.ui-slider-handle').html(tooltip); //attach tooltip to the slider handle
}

$("#slider").slider({
    value: initialValue,
    min: 1955,
    max: 2015,
    step: 1,
    create: sliderTooltip,
    slide: sliderTooltip
});

CSS tooltip borrowed from the twitter bootstrap framework

.tooltip {
    position: absolute;
    z-index: 1020;
    display: block;
    padding: 5px;
    font-size: 11px;
    visibility: visible;
    margin-top: -2px;
    bottom:120%;
    margin-left: -2em;
}

.tooltip .tooltip-arrow {
    bottom: 0;
    left: 50%;
    margin-left: -5px;
    border-top: 5px solid #000000;
    border-right: 5px solid transparent;
    border-left: 5px solid transparent;
    position: absolute;
    width: 0;
    height: 0;
}

.tooltip-inner {
    max-width: 200px;
    padding: 3px 8px;
    color: #ffffff;
    text-align: center;
    text-decoration: none;
    background-color: #000000;
    -webkit-border-radius: 4px;
       -moz-border-radius: 4px;
            border-radius: 4px;
}

Demo: http://jsfiddle.net/9qwmX/2/

查看更多
登录 后发表回答