Why can't I successfully increase an HTML range slider value with JavaScript, but I can successfully decrease it?

huangapple go评论64阅读模式
英文:

Why can't I successfully increase an HTML range slider value with JavaScript, but I can successfully decrease it?

问题

我尝试通过JavaScript来调整HTML范围滑块,但它的效果不如预期。

我可以成功地向下调整它,但向上调整也无效,情况一样。

这是在JSFiddle中的代码:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="icon" href="" />
        <style type="text/css">
            * {
                margin: 0;
                padding: 0;
                color: #FFFFFF;
                background-color: #131313;
                font-family: sans-serif;
            }

            button {
                padding: 0.5em 1em;
            }

            #content {
                margin: 2em;

                display: flex;
                flex-direction: column;
            }

            #content > div {
                margin: 1em 0;
            }

            .output {
                width: 400px;
                padding: 1em;
                background-color: #C0C0C0;
                color: #000000;
                padding: 1em;
                font-family: monospace;
                font-size: 2em;
                border: dotted 2px #A9A9A9;
            }
        </style>
        <title></title>
    </head>
    <body>
        <div id="container">
            <div id="content">
                <div>
                    <input class="volume" type="range" min="0" max="4" value="2" step="0.01">
                </div>
                <div class="output"></div>
                <div>
                    <button id="down"> &#x2012; </button>
                    <button id="up"> + </button>
                </div>
            </div> <!-- end content div -->
        </div> <!-- end container div -->
        <script type="text/javascript">
            const VOLUME = document.querySelector('.volume');
            const OUTPUT = document.querySelector('.output');
            const UP = document.querySelector('#up');
            const DOWN = document.querySelector('#down');

            const STEP_VALUE = parseFloat(VOLUME.step);

            OUTPUT.innerHTML = VOLUME.value;

            VOLUME.addEventListener('input', function(e){
                OUTPUT.innerHTML = VOLUME.value;
            });

            UP.addEventListener('click', function(){
                VOLUME.value = (VOLUME.value + STEP_VALUE);
                OUTPUT.innerHTML = VOLUME.value;
            });

            DOWN.addEventListener('click', function(){
                VOLUME.value = (VOLUME.value - STEP_VALUE);
                OUTPUT.innerHTML = VOLUME.value;
            });

            document.addEventListener('keydown', function(e){
                switch(e.key){
                    case 'ArrowUp':
                        VOLUME.value = (VOLUME.value + STEP_VALUE);
                        OUTPUT.innerHTML = VOLUME.value;
                        break;
                    case 'ArrowDown':
                        VOLUME.value = (VOLUME.value - STEP_VALUE);
                        OUTPUT.innerHTML = VOLUME.value;
                        break;
                    case 'ArrowLeft':
                        break;
                    case 'ArrowRight':
                        break;
                }
            }, false);
        </script>
    </body>
</html>
英文:

I am attempting to adjust an HTML range slider via JavaScript, but it is not working as intended.

I can successfully adjust it down, but up is not working as well, the same.

Here is the code in a JSFiddle

&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta charset=&quot;UTF-8&quot;&gt;
&lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1&quot;&gt;
&lt;link rel=&quot;icon&quot; href=&quot;&quot; /&gt;
&lt;style type=&quot;text/css&quot;&gt;
* {
margin: 0;
padding: 0;
color: #FFFFFF;
background-color: #131313;
font-family: sans-serif;
}
button {
padding: 0.5em 1em;
}
#content {
margin: 2em;
display: flex;
flex-direction: column;
}
#content &gt; div {
margin: 1em 0;
}
.output {
width: 400px;
padding: 1em;
background-color: #C0C0C0;
color: #000000;
padding: 1em;
font-family: monospace;
font-size: 2em;
border: dotted 2px #A9A9A9;
}
&lt;/style&gt;
&lt;title&gt;&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div id=&quot;container&quot;&gt;
&lt;div id=&quot;content&quot;&gt;
&lt;div&gt;
&lt;input class=&quot;volume&quot; type=&quot;range&quot; min=&quot;0&quot; max=&quot;4&quot; value=&quot;2&quot; step=&quot;0.01&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;output&quot;&gt;&lt;/div&gt;
&lt;div&gt;
&lt;button id=&quot;down&quot;&gt; &amp;#x2012; &lt;/button&gt;
&lt;button id=&quot;up&quot;&gt; + &lt;/button&gt;
&lt;/div&gt;
&lt;/div&gt; &lt;!-- end content div --&gt;
&lt;/div&gt; &lt;!-- end container div --&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
const VOLUME = document.querySelector(&#39;.volume&#39;);
const OUTPUT = document.querySelector(&#39;.output&#39;);
const UP = document.querySelector(&#39;#up&#39;);
const DOWN = document.querySelector(&#39;#down&#39;);
const STEP_VALUE = parseFloat(VOLUME.step);
OUTPUT.innerHTML = VOLUME.value;
VOLUME.addEventListener(&#39;input&#39;, function(e){
OUTPUT.innerHTML = VOLUME.value;
});
UP.addEventListener(&#39;click&#39;, function(){
VOLUME.value = (VOLUME.value + STEP_VALUE);
OUTPUT.innerHTML = VOLUME.value;
});
DOWN.addEventListener(&#39;click&#39;, function(){
VOLUME.value = (VOLUME.value - STEP_VALUE);
OUTPUT.innerHTML = VOLUME.value;
});
document.addEventListener(&#39;keydown&#39;, function(e){
switch(e.key){
case &#39;ArrowUp&#39;:
VOLUME.value = (VOLUME.value + STEP_VALUE);
OUTPUT.innerHTML = VOLUME.value;
break;
case &#39;ArrowDown&#39;:
VOLUME.value = (VOLUME.value - STEP_VALUE);
OUTPUT.innerHTML = VOLUME.value;
break;
case &#39;ArrowLeft&#39;:
break;
case &#39;ArrowRight&#39;:
break;
}
}, false);
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;

答案1

得分: 2

VOLUME.value的类型在您的上/下函数中是一个字符串而不是一个数字。将其转换为一个数字(例如,将VOLUME.value = (VOLUME.value + STEP_VALUE);更改为VOLUME.value = (+VOLUME.value + STEP_VALUE);)。

英文:

The type of VOLUME.value in your up/down functions is a string not a number. Cast it to a number (e.g. change VOLUME.value = (VOLUME.value + STEP_VALUE); to VOLUME.value = (+VOLUME.value + STEP_VALUE);.

huangapple
  • 本文由 发表于 2023年2月14日 00:23:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75438587.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定