更改另一个滑块时更新材质滑块UI

时间:2018-11-20 09:50:18

标签: javascript jquery html css material-design

我在输入时遇到麻烦。两个输入均已连接。当您单击第一个时,第二个输入将移动,但css将不会更新。

var $ra1 = $('#range1');
var $ra2 = $('#range2');

$ra1.on('input', function() {
    $ra2.val(this.max - this.value);
});
$ra2.on('input', function() {
    $ra1.val(this.max - this.value);
});
<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>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Range 1
<input type="range" min="0" max="400" value="0" class="mdl-slider mdl-js-slider " id="range1">

Range 2
<input type="range" min="0" max="400" value="400" class="mdl-slider mdl-js-slider" id="range2">

enter image description here

编辑: 我找到了IE11的解决方法

var $ra1 = $('#range1');
var $ra2 = $('#range2');
if (navigator.appName == 'Microsoft Internet Explorer' ||  !!(navigator.userAgent.match(/Trident/) || navigator.userAgent.match(/rv:11/)) || (typeof $.browser !== "undefined" && $.browser.msie == 1)){
    $ra1.change(function(){
        $ra2.get(0).MaterialSlider.change(this.max - this.value);
    });
    $ra2.change(function(){
        $ra1.get(0).MaterialSlider.change(this.max - this.value);
    });
 }else{
    $ra1.on('input', function() {
        $ra2.get(0).MaterialSlider.change(this.max - this.value);
    });
    $ra2.on('input', function() {
        $ra1.get(0).MaterialSlider.change(this.max - this.value);
    });
 }
<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>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Range 1
<input type="range" min="0" max="400" value="0" class="mdl-slider mdl-js-slider " id="range1"> 
Range 2
<input type="range" min="0" max="400" value="400" class="mdl-slider mdl-js-slider" id="range2">

1 个答案:

答案 0 :(得分:5)

为此,您需要通过Material库(而不是jQuery)设置值,以更新UI。为此,请在DOM元素的change属性上使用MaterialSlider函数,如下所示:

var $ra1 = $('#range1');
var $ra2 = $('#range2');

$ra1.on('input', function() {
  $ra2.get(0).MaterialSlider.change(this.max - this.value);
});

$ra2.on('input', function() {
  $ra1.get(0).MaterialSlider.change(this.max - this.value);
});
<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>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Range 1
<input type="range" min="0" max="400" value="0" class="mdl-slider mdl-js-slider " id="range1"> Range 2
<input type="range" min="0" max="400" value="400" class="mdl-slider mdl-js-slider" id="range2">