简体   繁体   English

如何在更改事件或立即值更改事件中获取纸张滑块的立即值/当前值

[英]How to get immediate value /current value of paper-slider on change event or immediate value change event

我试图改变纸张滑块的更改功能div的背景颜色与滑块的当前值,但未能获得滑块的值我尝试过 - 立即值更改或更改功能,但我不知道如何获取纸张滑块的当前值

Well, according to the documentation , there are two different kinds of value for the paper-slider : 那么,根据文档paper-slider有两种不同的值:

  • value The number that represents the current value. value 表示当前值的数字。
  • immediateValue The immediate value of the slider. immediateValue 滑块的直接值。 This value is updated while the user is dragging the slider. 用户拖动滑块时会更新此值。

One can access these properties directly on the paper-slider element. 可以直接在paper-slider元素上访问这些属性。

While the user drags the slider, a first event is fired continuously: immediate-value-change . 当用户拖动滑块时,会连续触发第一个事件: immediate-value-change When the user stops dragging the slider, two other events are fired: change and value-change . 当用户停止拖动滑块时,会触发另外两个事件: changevalue-change The difference between the two is that the change event is only fired on user's interaction. 两者之间的区别在于,仅在用户的交互中触发change事件。

Considering these informations, you may add to your host element the following snippets (your slider should have a slider id, but you can obviously change that): 考虑到这些信息,您可以在主机元素中添加以下代码段(您的滑块应该有一个slider ID,但您显然可以更改它):

listeners: {
    'immediate-value-change': 'immediateValueChangeHandler',
    'change': 'changeHandler'
},

immediateValueChangeHandler: function (e) {
    var value = this.$.slider.immediateValue;
    // do something
},

changeHandler: function (e) {
    var value = this.$.slider.value;
    // do something
}

easiest way i know of to get the value of a slider is to assign it a declarative variable and use that in your javascript. 我知道获取滑块值的最简单方法是为其分配声明性变量并在javascript中使用它。

example

 <paper-slider min="0" max="100" value="{{sliderValue}}" on-change="{{sliderChange}}"></paper-slider>

then in js 然后在js

sliderChange: function () {
  var value = this.sliderValue;
  // do something with value
}

plunker example http://plnkr.co/edit/gihpf5aZH4obhquaDtZQ?p=preview plunker示例http://plnkr.co/edit/gihpf5aZH4obhquaDtZQ?p=preview

edit: original post using event sender value returned the value of the beginning of the event and not the end value 编辑:原始帖子使用事件发送者值返回事件开头的值而不是结束值

I would suggest listening to the change event and then accessing the srcElement.value to get the current value 我建议听取change事件,然后访问srcElement.value以获取当前值

sliderElement.addEventListener('change', function(e) {
    console.log(e.type, e.srcElement.value);
  });

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

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