简体   繁体   English

jQuery UI滑块附加标签

[英]Jquery UI slider attach label

I have a modal, that shows up at certain times and I have two sliders on it, which should show some values. 我有一个模态,它会在某些时间显示,并且上面有两个滑块,应该显示一些值。 Here's the code: 这是代码:

div#averageAnswerModal.modal.fade.custom-modal(tabindex="-1", role="dialog", aria-labelledby="myModalLabel" aria-hidden="true")
div.modal-content
    div.modal-body(style='text-align: center;')
        h2 Your neighbor's answers average was:
        div
            div#noAvg Your neighbors didn't give any anwer
            div#avgSlider
            span#avgLabel
        h2 Your answer was:
        div
            dvi#notAnswered You didn't give anwer
            div#yourSlider
            span#yourLabel

I have this code to set up the sldiers 我有这段代码来设置sldiers

initializeModal = function() {
        var positionLabel = function(label, slider, val) {
            $(label).html(val).position({
                my: 'center top',
                at: 'center bottom',
                of: $(slider + ' > .ui-slider-handle')
            })
        }

        myModal = $('#averageAnswerModal').modal({
            show: false,
            keybord: false
        })

        $("#avgSlider").slider({
            max: 100,
            disabled: true,
            change: function(event, ui) {
                positionLabel('#avgLabel', "#avgSlider", ui.value)
            }
        })

        $("#yourSlider").slider({
            max: 100,
            disabled: true,
            change: function(event, ui) {
                positionLabel('#yourLabel', "#yourSlider", ui.value)
            }
        })
    }

And this code fires them up: 这段代码激发了它们:

createSplashSliders = function(avg, your) {
    myModal.modal('show')
    if (avg === -1) {
        $("#noAvg").show()
        $("#avgSlider").hide()
        $('#avgLabel').hide()
    } else {
        $("#noAvg").hide()
        $("#avgSlider").show()
        $('#avgLabel').show()
        $("#avgSlider").slider('value', avg)
    }

    if (your === -1) {
        $("#notAnswered").show()
        $("#yourSlider").hide()
        $('#yourLabel').hide()
    } else {
        $("#notAnswered").hide()
        $("#yourSlider").show()
        $('#yourLabel').show()
        $("#yourSlider").slider('value', your)
    }
}

The sliders show up correctly and I wanted to attach the selected value below the handle. 滑块正确显示,我想将选定的值附加到手柄下方。 It attaches the values like 2 lines below and if I use the page repeatedly the span just moves down with a line every time the modal shows up. 它在下面附加了2行之类的值,如果我重复使用该页面,则每次出现模态时,跨度都会向下移动一行。 When I stop the code with debugger in positionLabel and just copy+paste the same code the label gets attached to the right place. 当我在positionLabel使用调试器停止代码并仅复制并粘贴相同的代码时,标签将粘贴到正确的位置。

I've been stuck with this problem for half a day and I still cannot figure out what's wrong. 我已经被这个问题困扰了半天,但我仍然无法弄清楚出什么问题了。

UPDATE UPDATE

Fiddle . 小提琴 If you put a debugger in the positionLabel and just step through it will position the labels correctly. 如果将调试器放在positionLabel然后逐步执行,它将正确放置标签。 So somewhere I have a timing issue, just don't know where or why. 所以在某个地方,我有时间问题,只是不知道在哪里或为什么。

If I add a timeout to 如果我添加超时

        var positionLabel = function(label, slider, val) {
            setTimeout(function() {
                $(label).html(val).position({
                    my: 'center top',
                    at: 'center bottom',
                    of: $(slider + ' > .ui-slider-handle')
                })
            }, 200);
        }

This works... but not with timeout of 5 . 这可以...但是超时不为5 Why do I need this timeout? 为什么我需要这个超时时间?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM