激活"onmoousemove".仅当"onmousedown"事件发生时才发生此事件.事件已激活

编程入门 行业动态 更新时间:2024-10-27 11:20:16
本文介绍了激活"onmoousemove".仅当"onmousedown"事件发生时才发生此事件.事件已激活的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在发现了对类型范围的输入进行样式设置的困难之后,我最好还是简单地使用css创建一个并隐藏原始输入.我正在尝试制作音量滑块,但我想我不完全了解如何连接 onmousemove 和 onmousedown .我尝试关注以下帖子

After discovering the difficulties of styling inputs of type range, I though it best to simply create one using css and hiding the original. I'm trying to Make a volume slider, but I don't think I fully understand how to connect onmousemove and onmousedown. I tried following the following post

如何将onmousemove与onmousedown连接?

但是我的 volumeSlider 函数-被注释掉的javascript代码-仍然无法运行;

but my volumeSlider function - the javascript code that is commented out - still isn't working;

我想要的是 onmousemove 仅在 onmousedown 上被激活,而不仅仅是通过移动鼠标来激活.

What I want is that onmousemove is only activated on onmousedown and not by simply moving the mouse.

const volume_div = document.querySelector('.volume'); const volumeBtn_div = document.querySelector('.volume-button'); function volumeClick(event) { let x = event.offsetX; volume_div.style.width = (Math.floor(x) + 10) + 'px'; } /* volumeBtn_div.onmousedown = function() { volumeBtn_div.onmousemove = volumeSlide; }; function volumeSlide(event) { let x = event.offsetX; volume_div.style.width = Math.floor(x) + 'px'; }*/

body { background-color: #2a2a2a; } .volume-range { margin-top: 80px; height: 5px; width: 250px; background: #555; border-radius: 15px; } .volume-range>.volume { height: 5px; width: 50px; background: #2ecc71; border: none; border-radius: 10px; outline: none; position: relative; } .volume-range>.volume>.volume-button { width: 20px; height: 20px; border-radius: 20px; background: #FFF; position: absolute; right: 0; top: 50%; transform: translateY(-50%); cursor: pointer; outline: none; }

<!DOCTYPE html> <html lang="en" dir="ltr"> <head> <meta charset="utf-8"> <title>Volume</title <link rel="stylesheet" href="use.fontawesome/releases/v5.6.3/css/all.css" integrity="sha384-UHRtZLI+pbxtHCWp1t77Bi1L4ZtiqrqD80Kn4Z8NTSRyMA2Fd33n5dQ8lWUE00s/" crossorigin="anonymous"> </head> <body> <div class="volume-range" onclick="volumeClick(event)"> <div class="volume"> <div class="volume-button"></div> </div> </div>

推荐答案

如果我正确理解了您的问题,我认为您可以将标志设置为onmousedown并将其重置为onmouseup.像这样:

If I understand your question correctly, I think you could just set a flag onmousedown and reset it onmouseup. Something like:

let mouseIsDown = false; volumeBtn_div.onmousedown = function() { mouseIsDown = true }; volumeBtn_div.onmouseup = function() { mouseIsDown = false }; volumeBtn_div.onmousemove = volumeSlide; function volumeSlide(event) { if(mouseIsDown){ let x = event.offsetX; volume_div.style.width = Math.floor(x) + 'px'; } }

...

为回应您的评论,此类似示例可在Chrome中运行.我更改了EventListener语法.它应该使您走上正确的轨道.

In response to your comment, this similar example works in Chrome. I changed the EventListener syntax. It should get you on the right track.

<!DOCTYPE html> <html> <head> <style> div { width: 200px; height: 100px; border: 1px solid black; } </style> </head> <body> <div id="input"></div> <p id="output"></p> <script> const input = document.getElementById("input"); const output = document.getElementById("output"); let mouseIsDown = false; input.addEventListener("mouseup", up); input.addEventListener("mousedown", down); input.addEventListener("mousemove", slide); function down(){ mouseIsDown = true; } function up(){ mouseIsDown = false; } function slide(e) { if(mouseIsDown){ var x = e.clientX; var pos = "pos: " + x; output.innerHTML = pos; } } </script> </body> </html>

更多推荐

激活"onmoousemove".仅当"onmousedown"事件发生时才发生此事件.事件已激活

本文发布于:2023-11-06 16:16:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1564186.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:事件   发生   时才   quot   onmoousemove

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!