admin管理员组文章数量:1315826
As the title says, I have an object, and i need all it's drag and click events. There was some discussion about this issue, but it mainly concerned the click and drag events. (and a reply fiddle didnt work properly)
I have a fiddle here of where I am at. When I drag, click is prevented, but when I click the dragstart and end events fire. I'd like them not to fire when I click, and I'd like click not to fire when I want to drag.
var drag = d3.behavior.drag()
.origin(function(d){return d})
.on('drag', function(d){
d3.select(this).attr('cx', function(d){ return d.x += d3.event.dx });
d3.select(this).attr('cy', function(d){ return d.y += d3.event.dy });
console.log('dragging');
})
.on('dragstart', function(d){
d3.event.sourceEvent.stopPropagation()
console.log('drag start');
})
.on('dragend', function(d){
console.log('drag end');
})
// .....
MySvgElementWith3DStuffOnIt.on('click', function(){
if(d3.event.defaultPrevented) return;
console.log('clicked');
});
As the title says, I have an object, and i need all it's drag and click events. There was some discussion about this issue, but it mainly concerned the click and drag events. (and a reply fiddle didnt work properly)
I have a fiddle here of where I am at. When I drag, click is prevented, but when I click the dragstart and end events fire. I'd like them not to fire when I click, and I'd like click not to fire when I want to drag.
var drag = d3.behavior.drag()
.origin(function(d){return d})
.on('drag', function(d){
d3.select(this).attr('cx', function(d){ return d.x += d3.event.dx });
d3.select(this).attr('cy', function(d){ return d.y += d3.event.dy });
console.log('dragging');
})
.on('dragstart', function(d){
d3.event.sourceEvent.stopPropagation()
console.log('drag start');
})
.on('dragend', function(d){
console.log('drag end');
})
// .....
MySvgElementWith3DStuffOnIt.on('click', function(){
if(d3.event.defaultPrevented) return;
console.log('clicked');
});
Share
Improve this question
edited May 23, 2017 at 11:46
CommunityBot
11 silver badge
asked May 30, 2016 at 20:40
valval
7496 silver badges20 bronze badges
1
- so many examples online for this. ill have a look for one – thatOneGuy Commented May 30, 2016 at 20:42
2 Answers
Reset to default 4A small update of @thatOneGuy to use d3v4 drag module
https://jsfiddle/mhebrard/q5eL5qyv/
var drag = d3.drag()
.on('drag', function(d){
d3.select(this).attr('cx', function(d){ return d.x += d3.event.dx });
d3.select(this).attr('cy', function(d){ return d.y += d3.event.dy });
console.log('dragging');
})
.on('start', function(d){
console.log('drag start');
})
.on('end', function(d){
console.log('drag end');
})
Your example works as expected. See updated fiddle : https://jsfiddle/thatOneGuy/dd4nujxo/1/
I have put a few added console logs. When you drag this line :
if (d3.event.defaultPrevented) {console.log('return'); return};
Will stop the click event from firing. And when you only click, the dragstart
and dragend
get fired (as expected), but the drag
doesn't. Which is why you put the code to move the circle inside the drag
function. The whole thing works as expected :)
本文标签: javascriptd3jsDifferentiate between dragstartend and click eventStack Overflow
版权声明:本文标题:javascript - d3.js : Differentiate between dragstartend and click event - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741986753a2408726.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论