【问题标题】:jQuery slider-bar tooltip not moving in sync with slider handlejQuery滑块工具提示不与滑块句柄同步移动
【发布时间】:2014-09-04 04:00:23
【问题描述】:

我在 Django Python 应用程序中使用 tutorial by Thoriq Firdaus 自定义了一个 jQuery 滑块。我一直在寻找的主要功能是一个工具提示,它会随着滑块手柄的移动而更新,并且反过来会随着滑块手柄移动,这两者都有。

但是原始滑块的范围和大小太小。我需要min: -100max: +100。我很容易将滑块的 JS 和 CSS 更新为所需的大小。但是,工具提示不再与滑块手柄匹配或保持同步。当我用 Firebug 检查元素时,我可以看到工具提示位于滑块的左侧,我相信它只在 -100px 和 +100px 内移动。 (见下图)

滑块工具提示位于最左侧:

最右侧的滑块工具提示:

我尝试了多种解决方案,包括为 JS 中的工具提示设置 min:max: 范围,(从未工作过)并更改 CSS 以尝试将其居中并使其内联移动,但我似乎无法理解.

有谁知道如何让工具提示复制滑块手柄的移动,这是我必须在 JS 或 CSS 中更改的东西吗?

我也尝试在JSFiddle 中复制该问题,但没有出现滑块手柄。

我的 HTML

<section>   
    <span class="tooltip"></span>   
    <div id="slider"></div>  
</section> 

我的 CSS

#slider{  
    border-width: 1px;  
    border-style: solid;  
    border-color: #333 #333 #777 #333;  
    border-radius: 25px;  
    width: 700px;  
    position: absolute;  
    height: 13px;  
    background-color: #8e8d8d;  
    background: #85837A;  
    box-shadow: inset 0 1px 5px 0px rgba(0, 0, 0, .5),   
                0 1px 0 0px rgba(250, 250, 250, .5);  
    left: 20px;  
} 

.tooltip {  
    position: absolute;  
    bottom: 10px;
    display: block;  
    z-index: 1031;
    width: 35px;  
    height: 20px;  
    color: #E456ff;  
    bgcolor: #E456ff;      
    text-align: center;  
    font: 10pt Tahoma, Arial, sans-serif ;  
    border-radius: 3px;  
    border: 1px solid #333;  
    box-shadow: 1px 1px 2px 0px rgba(0, 0, 0, .3);  
    box-sizing: border-box;  
    background: linear-gradient(top, rgba(69,72,77,0.5) 0%,rgba(0,0,0,0.5) 100%);  
    opacity: 1;
    filter: alpha(opacity=1);       
}  

我的 JS

$(function() {

    //Store frequently elements in variables
    var slider  = $('#slider'),
        tooltip = $('.tooltip');

    //Hide the tooltip at first
    tooltip.hide();

    //Call the Slider
    slider.slider({
        //Config
        //range: "min",
        min: -100,
        max: +100,
        value: 0,

        start: function(event,ui) {
            tooltip.fadeIn('fast');
        },

        //Slider Event
        slide: function(event, ui) { //When the slider is sliding

            var value  = slider.slider('value'),
                volume = $('.volume');

            tooltip.css('left', value).text(ui.value);  //Adjust the tooltip accordingly
        },

        stop: function(event,ui) {
            tooltip.fadeOut('slow');
        },
    });

});

【问题讨论】:

  • 等一下,更新你的小提琴
  • 如果有任何帮助,我正在使用 jQuery-ui.css 1.11.0 版本。抱歉,我无法在那里复制错误
  • 嗨,是的,它在 JSFiddle 中工作,完美,我现在只是想把它放到我的页面中
  • 好的..去试试吧。
  • 如果有效,请将答案标记为已接受,如果无效,请说明问题..:)

标签: jquery css jquery-ui tooltip jquery-slider


【解决方案1】:

http://jsfiddle.net/NKTpT/2/

现在检查..我认为它完成了。

改变了:

init values acc to width %

编辑:以防 JSFiddle 消失

下面的代码是 nsthethunderbolt 提供的 JSFiddle 解决方案,我在这里复制了它。

HTML

    <section> <span class="tooltip"></span> 
        <div id="slider"></div>
        <div id="slider"></div>
    </section>

CSS

section {
    width: 700px;
    height: auto;
    margin: 0px auto 0;
    position: relative;
}
#slider {
    border-width: 1px;
    border-style: solid;
    border-color: #333 #333 #777 #333;
    border-radius: 25px;
    width: 400px;
    position: absolute;
    height: 13px;
    background-color: #8e8d8d;
    background: #85837A;
    box-shadow: inset 0 1px 5px 0px rgba(0, 0, 0, .5), 0 1px 0 0px rgba(250, 250, 250, .5);
    left: 20px;
}


.tooltip {
    border: 1px solid #333;
    border-radius: 3px;
    top:20px;
    position:absolute;
    box-shadow: 1px 1px 2px 0 rgba(0, 0, 0, 0.3);

    color: #e456ff;
    display: block;
    font: 10pt Tahoma, Arial, sans-serif;
    height: 20px;
    opacity: 1;

    text-align: center;
    width: 35px;
    z-index: 1031;
}

JS

$(function () {
    var initX=0,minX=50,width=400;
    //Store frequently elements in variables
    var slider = $('#slider'),
        tooltip = $('.tooltip');

    //Hide the tooltip at first
    //tooltip.hide();

    //Call the Slider
    slider.slider({
        //Config
        //range: "min",
        min: -50,
        max: +50,
        value: 0,

        start: function (event, ui) {
            tooltip.fadeIn('fast');
        },

        //Slider Event
        slide: function (event, ui) { //When the slider is sliding
            var value = slider.slider('value'),
                volume = $('.volume');
            tooltip.css('left', initX+(value*width)/100).text(ui.value); //Adjust the tooltip accordingly
        },

        stop: function (event, ui) {
          //  tooltip.fadeOut('slow');
        },
    });
    initX=slider.slider("value");
    var txt=initX;
   initX+=(minX*width)/100;
tooltip.css('left',initX).text(txt);
});

【讨论】:

  • 您的解决方案在 JSFiddle 中有效,但是当我在我的应用程序中复制代码时,工具提示仍未与句柄同步移动。我想知道是否与我现在正在寻找的另一个 css 或 js 文件存在冲突。
  • 你可以从fiddle的左侧看到使用的资源,并与你的交换。并且一定要复制css,清除缓存(如果你没有)。
  • 现在完美运行。谢谢您的帮助。我将使用您提供的 JSFiddle 代码更新答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-23
相关资源
最近更新 更多