多边形onclick事件问题及其形状

时间:2019-07-03 16:21:34

标签: javascript svg polygon

我已经建立了一个由4个多边形(梯形)围绕的正方形,我需要通过单击来顺序更改其颜色。

我在网上找不到任何类似的问题。 但是我确实尝试过反复更改鼠标上的zIndex,但这并不是一个好的解决方案。

您可以在此处检查问题https://jsfiddle.net/d9Lh31sv/1/ 即使多边形html像矩形一样显示它,并且它们在侧面都彼此叠置,如您在这张图片上看到的Focus

想知道单击事件是否只能遵守其多边形限制。 预先感谢。

	var objTrapezium = document.getElementsByClassName('trapezium');	
	if (objTrapezium) {		
		for (var i = 0; i < objTrapezium.length; i++) {
			objTrapezium[i].onclick = function() {
				var _Color = this.firstChild.nextSibling.attributes.fill.value;	
				_Color = _Color=="none"  ? "grey" :  _Color =="grey" ? "red":  _Color =="red" ? "green": _Color =="green" ?"blue": _Color =="blue" ? "grey": "";							
				this.firstChild.nextSibling.attributes.fill.value = _Color;
        
			};

			objTrapezium[i].onmouseover = function() {
				this.style.zIndex = 9999;
				this.style.backgroundColor = "lightsteelblue";
			  }
			objTrapezium[i].onmouseout = function(){
				this.style.zIndex = 1;
				this.style.backgroundColor = "transparent";
			  }


		}

	}
.trapezium{
	position: relative;
}
.square{
  left: 202px;
  width: 73px;
  height: 73px;
  top: 102px;
}
.bottom{
  left: 53px;
  top: 175px;
  z-index: 1;
}
.left{
  transform: rotate(90deg);
  left: -243px;
  top: 102px;
}
.right{
  transform: rotate(-90deg);
  left: -315px;
  top: 101px;
}
.top{
 transform: rotate(-180deg);
 left: 129px;
 top: -48px;
}
<div>
  <svg class="trapezium square">
    <rect stroke="black" fill="none" width="73" height="73" />				
  </svg>
  <svg class="trapezium bottom" height="72" width="217">
    <polygon stroke="black" fill="none" points="0,72 72,0 144,0 216,72" />
  </svg>
  <svg class="trapezium left" height="72" width="217">
    <polygon stroke="black" fill="none" points="0,72 72,0 144,0 216,72" />
  </svg>
  <svg class="trapezium right" height="72" width="217">
    <polygon stroke="black" fill="none" points="0,72 72,0 144,0 216,72" />
  </svg>
  <svg class="trapezium top" height="72" width="217">
    <polygon stroke="black" fill="none" points="0,72 72,0 144,0 216,72" />
  </svg>
</div>

 

2 个答案:

答案 0 :(得分:1)

正如我评论的那样,我会做不同的事情:我会将所有内容都放在一个svg元素中。 请看一下。也许这就是您所需要的。

svg{border:1px solid; width:300px}
use{fill:white;}
use:hover{fill:gold}
 <svg viewBox="-115 -115 230 230">
    <defs>
      <polygon id="poly" stroke="black"  points="-36.5,36.5 36.5,36.5 108, 108 -108,108 -36.5,36.5" transform="translate(0,3)"  />
    </defs>

    <use xlink:href="#poly" />
    <use xlink:href="#poly" transform="rotate(90)" /> 
    <use xlink:href="#poly" transform="rotate(180)" />
    <use xlink:href="#poly" transform="rotate(270)" />
    
    <rect stroke="black" fill="none" x="-35" y="-35" width="70" height="70" />	
    
  </svg>
  

答案 1 :(得分:0)

我可以想到的解决方案是使用offsetX上的offsetYjs来确定点击是否超出了梯形范围,然后使用一些逻辑来更改正确的元素中的颜色。

var objTrapezium = document.getElementsByClassName('trapezium');
if (objTrapezium) {
  for (var i = 0; i < objTrapezium.length; i++) {
    objTrapezium[i].onclick = function(e) {
      var id = this.attributes.id.value;
      var x = e.offsetX;
      var y = e.offsetY;
      var height = this.attributes.height.value;
      var outLeft = (y + x) < height;
      var outRight = (x - y) > (height * 2);
      var nextElemetId = "";
      var _Color = "";
      if ((id !== "square") && (outLeft || outRight)) {
        switch (id) {
          case "top":
            nextElemetId = outRight ? "left" : "right";
            break
          case "right":
            nextElemetId = outRight ? "top" : "bottom";
            break
          case "bottom":
            nextElemetId = outRight ? "right" : "left";
            break
          case "left":
            nextElemetId = outRight ? "bottom" : "top";
            break
        }
        var objNextTrapezium = document.getElementById(nextElemetId);
        _Color = objNextTrapezium.firstChild.nextSibling.attributes.fill.value;
        _Color = _Color == "none" ? "grey" : _Color == "grey" ? "red" : _Color == "red" ? "green" : _Color == "green" ? "blue" : _Color == "blue" ? "grey" : "none";
        objNextTrapezium.firstChild.nextSibling.attributes.fill.value = _Color;

      } else {
        _Color = this.firstChild.nextSibling.attributes.fill.value;
        _Color = _Color == "none" ? "grey" : _Color == "grey" ? "red" : _Color == "red" ? "green" : _Color == "green" ? "blue" : _Color == "blue" ? "grey" : "none";
        this.firstChild.nextSibling.attributes.fill.value = _Color;
      }
    };
  }
}
.trapezium {
	position: relative;
}

#square {
	left: 199px;
    width: 72px;
    height: 72px;
    top: 101px;
}

#top {
	transform: rotate(180deg);
	left: 51px;
	top: 29px;
}

#right {
	transform: rotate(270deg);
	left: -98px;
	top: 101px;
}

#bottom {
	left: 127px;
	top: 96px;	
}

#left {
	transform: rotate(90deg);
    left: -165px;
    top: 24px;
}
<svg class="trapezium" id="square" width="72" height="72">
  <rect stroke="black" fill="none" width="72" height="72" />				
</svg>
<svg class="trapezium" id="top" height="72" width="216">
  <polygon stroke="black" fill="none" points="0,72 72,0 144,0 216,72" />
</svg>
<svg class="trapezium" id="right" height="72" width="216">
  <polygon stroke="black" fill="none" points="0,72 72,0 144,0 216,72" />
</svg>
<svg class="trapezium" id="bottom" height="72" width="216">
  <polygon stroke="black" fill="none" points="0,72 72,0 144,0 216,72" />
</svg>			
<svg class="trapezium" id="left" height="72" width="216">
  <polygon stroke="black" fill="none" points="0,72 72,0 144,0 216,72" />
</svg>