admin管理员组文章数量:1419233
I have a technical issue and I am not sure what the best way is to solve it.
Basically im working on an audio progress bar where the user can change the position of the song. This should be done by either clicking or by keeping the mousedown and moving it.
elem.addEventListener("mouseup", function(event){
elem.removeEventListener("mousemove", fn);
});
elem.addEventListener("mousedown", function(event){
elem.addEventListener("mousemove", function(event){
// Stuff to do
}, fn);
});
So essentially:
If mouse is held down and then moved do something. If mouse is released prevent the trigger of mousemove
.
I know if I continue with this it might work but it looks like a horrible mess to me. I dont want to pick up bad habits.
My question is: How do I make make this work using best practices?
Note: I also tried the following:
function fn(event) {
if(event.button == 0) {
elem.addEventListener("mousemove", function(event){
// Stuff to do
});
}
}
What i'm missing here !
I have a technical issue and I am not sure what the best way is to solve it.
Basically im working on an audio progress bar where the user can change the position of the song. This should be done by either clicking or by keeping the mousedown and moving it.
elem.addEventListener("mouseup", function(event){
elem.removeEventListener("mousemove", fn);
});
elem.addEventListener("mousedown", function(event){
elem.addEventListener("mousemove", function(event){
// Stuff to do
}, fn);
});
So essentially:
If mouse is held down and then moved do something. If mouse is released prevent the trigger of mousemove
.
I know if I continue with this it might work but it looks like a horrible mess to me. I dont want to pick up bad habits.
My question is: How do I make make this work using best practices?
Note: I also tried the following:
function fn(event) {
if(event.button == 0) {
elem.addEventListener("mousemove", function(event){
// Stuff to do
});
}
}
What i'm missing here !
Share Improve this question edited Dec 26, 2015 at 19:59 Venkat.R 7,7765 gold badges44 silver badges68 bronze badges asked Dec 26, 2015 at 18:20 AspergerAsperger 3,23211 gold badges59 silver badges106 bronze badges 8- How about just using input type="range" ? Set the min to 0 and the max value to the song length in seconds. Then simply use the change event of the input control to seek to the correct position. – HaukurHaf Commented Dec 26, 2015 at 18:24
- @HaukurHaf im aware but I am trying to find out how to do this without. Also it allows me for more custom work since I done most of GUI myself. – Asperger Commented Dec 26, 2015 at 18:26
- jqueryui./draggable Did you ever hear about this? – Ahmad Asjad Commented Dec 26, 2015 at 18:28
- @AhmadAsjad ya I did but I want to avoid jquery – Asperger Commented Dec 26, 2015 at 18:29
- but you like the same thing? – Ahmad Asjad Commented Dec 26, 2015 at 18:30
1 Answer
Reset to default 7Don't add/remove the listener, just set a variable at trigger actions accordingly:
var isMousedown = false;
elem.addEventListener("mousedown", function (event) {
isMousedown = true;
}
elem.addEventListener("mouseup", function (event) {
isMousedown = false;
});
elem.addEventListener("mousemove", function (event) {
if (isMousedown) {
// do stuff
} else {
// do different stuff, or nothing
}
});
You may even want to put the mousedown/mouseup listeners on the parent (or even the root element) to avoid scenarios where the mouseup happens outside of the target, if that is a possibility in your case.
本文标签: javascriptIf mousedown then trigger mousemove else do nothingStack Overflow
版权声明:本文标题:javascript - If mousedown then trigger mousemove else do nothing - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745300429a2652339.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论