简体   繁体   English

为什么删除了 hover state 行为?

[英]Why is the hover state behaviour removed?

I have a D3 generated map which needs to be able to dynamically change the fill element of drawn paths.我有一个 D3 生成的 map 需要能够动态更改绘制路径的填充元素。 The original paths are generated and assigned a class of 'boundaries'.生成原始路径并为其分配“边界”的 class。 The hover behaviour is set to turn the country yellow when the user hovers the cursor over the country.当用户将 cursor 悬停在国家上空时,hover 行为设置为将国家变为黄色。 However, if I then go and dynamically change the fill color of the country, for example by using d3.selectAll- (I have simplified the below example so that this behaviour is simulated by uncommenting the last section), the hover behaviour stops working.但是,如果我然后 go 并动态更改国家/地区的填充颜色,例如使用 d3.selectAll- (我简化了下面的示例,以便通过取消注释最后一部分来模拟此行为),hover 行为将停止工作。 The class has not changed, so why is the hover behaviour now not occurring.. and is there a workaround for this? class 没有改变,那么为什么现在没有发生 hover 行为.. 是否有解决方法?

CSS CSS

.countryMap{
 width: 500px;
 height: 500px;
 position: relative;
}

.boundaries{
 fill:green;
}

.boundaries:hover{
 fill:yellow;
}

Javascript Javascript

const countryMap = {};
      const FILE = `aus.geojson`; // the file we will use
      var svg = d3
        .select('div.country__map')
        .append('svg')
        .attr('width',200)
        .attr('height',200)
        .attr('preserveAspectRatio', 'xMinYMin meet')
        .attr('viewBox','770 270 200 150')

        d3.json(FILE).then(function (outline) {
        countryMap.features = outline.features;

        // choose a projection
        const projection = d3.geoMercator();
        // create a path generator function initialized with the projection
        const geoPath = d3.geoPath().projection(projection);
        drawOutline();

        function drawOutline() {
          svg
            .selectAll('path.boundaries') // CSS styles defined above
            .data(countryMap.features)
            .enter()
            .append('path')
            .attr('class', 'boundaries')
            .attr('d', geoPath)
            // .style('fill', d => {
            //   return 'green';
            // })
        }
        })

As @Michael mentioned it will be better to manually add or remove class using js.正如@Michael 提到的,最好使用js手动添加或删除 class 。

D3 provides us mouseover and mouseout events which can be used to add and remove class. D3 为我们提供了 mouseover 和 mouseout 事件,可用于添加和删除 class。

Here on hover, we are applying the 'active' class on the element.在 hover 上,我们在元素上应用“活动”class。

svg
   .selectAll('path.boundaries')
   .data(countryMap.features)
   .enter()
   .append('path')
   .attr('class', 'boundaries')
   .attr('d', geoPath)
   .on('mouseover', function () {
      d3.select(this).classed("active", true)  
   })
   .on('mouseout', function () {
      d3.select(this).classed("active", false)
   })

We also need to update the CSS according to these changes.我们还需要根据这些更改更新 CSS。 You can update the CSS to:您可以将 CSS 更新为:

.boundaries{
 fill:green;
}

.boundaries.active{
 fill:yellow;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM