最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - D3 mousewheel zoom direction - Stack Overflow

programmeradmin4浏览0评论

I'm trying to create some custom zoom functionality in d3.js. Currently the zoom is triggered on a single click and zooms in to focus only on the area that was clicked on.

Currently my code has a function zoom(d) that does exactly what it needs to. There is also a var zoomTransition which resides inside zoom() and is responsible for much of the functionality. I'm unfortunately unable to share much of my code.

The zoom needs to also occur on a mouse scroll. The difficulty I'm having is that this:

        .on("wheel", function(d){
            zoom(d);
        });

disregards the scroll wheel direction. Zoom is called simply because the wheel is scrolled, either in or out.

Is there any way I can access the scroll direction and pass it into zoom()? Or a better way to do this?

I'm trying to create some custom zoom functionality in d3.js. Currently the zoom is triggered on a single click and zooms in to focus only on the area that was clicked on.

Currently my code has a function zoom(d) that does exactly what it needs to. There is also a var zoomTransition which resides inside zoom() and is responsible for much of the functionality. I'm unfortunately unable to share much of my code.

The zoom needs to also occur on a mouse scroll. The difficulty I'm having is that this:

        .on("wheel", function(d){
            zoom(d);
        });

disregards the scroll wheel direction. Zoom is called simply because the wheel is scrolled, either in or out.

Is there any way I can access the scroll direction and pass it into zoom()? Or a better way to do this?

Share Improve this question asked Jul 20, 2016 at 11:58 xandermonkeyxandermonkey 4,4222 gold badges37 silver badges60 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 11

Was looking for this:

.on("wheel", function(d){
            var direction = d3.event.wheelDelta < 0 ? 'down' : 'up';
            zoom(direction === 'up' ? d : d.parent);
        });

More so javascript than d3, but that's how you access the scroll wheel information.

d3 has a zoom behaviour that you might find useful.

Example code:

 var zoom = d3.behavior.zoom()
        .scaleExtent([0, 10])
        .on("zoom", redraw); //if you are sure that your zoom function is working just replace redraw with your zoom function


function redraw() {
    return svg.attr("transform", "translate(" + d3.event.translate + ")" + " scale(" + d3.event.scale + ")");
}

Full example:

http://bl.ocks/mbostock/2206340

发布评论

评论列表(0)

  1. 暂无评论