简体   繁体   English

Material Design Lite 滑块更新

[英]Material Design Lite slider update

I've been trying to use MDL sliders to make my website, but I get a problem.我一直在尝试使用MDL滑块来制作我的网站,但我遇到了问题。 Everything is working, however, when I try to change the value of the slider through jQuery , the "bar" of the slider isn't synced as you can see here :一切正常,但是,当我尝试通过jQuery更改滑块的值时,滑块的“条”未同步,如您在此处看到的:

在此处输入图片说明

or the "blue circle" isn't colored (note that the default value is the minimum one which disables the color of the slider and when I update it, it doesn't put the color back) :或者“蓝色圆圈”没有着色(请注意,默认值是禁用滑块颜色的最小值,当我更新它时,它不会恢复颜色):

在此处输入图片说明

The code I'm using to change sliders value:我用来更改滑块值的代码:

  $("#mySlider").val(10);

But it doesn't update the graphical part, only the value.但它不更新图形部分,只更新值。

This is the property which handles the bar color :这是处理条形颜色的属性:

在此处输入图片说明

Minimal reproducible example:最小可重现示例:

To reproduce this, insert a slider with a default value then change it using the above javascript code to change the value.要重现这一点,请插入一个带有默认值的滑块,然后使用上面的 javascript 代码更改它以更改值。

You shouldn't change value like that, there is a native way:你不应该那样改变value ,有一种原生的方式:

 $('#click').on('click', function() { $('#mySlider').get(0).MaterialSlider.change(10); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons"> <link rel="stylesheet" href="https://code.getmdl.io/1.3.0/material.indigo-pink.min.css"> <script defer src="https://code.getmdl.io/1.3.0/material.min.js"></script> <input class="mdl-slider mdl-js-slider" id="mySlider" type="range" min="0" max="100" value="25" tabindex="0"> <button id="click" class="mdl-button mdl-js-button mdl-button--raised"> Update </button>

Although the value attribute is used to set a slider's initial value, it should not be used to modify the value programmatically;虽然 value 属性用于设置滑块的初始值,但不应用于以编程方式修改值; instead, use the MDL change() method.相反,请使用 MDL change() 方法。 For example, assuming that slider1 is a slider object and newvalue is a variable containing the desired value, do not use slider1.value = newvalue;例如,假设slider1是一个slider对象,newvalue是一个包含想要的值的变量,就不要使用slider1.value = newvalue; instead, use slider1.MaterialSlider.change(newvalue)相反,使用slider1.MaterialSlider.change(newvalue)

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

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