简体   繁体   English

用对角线将正方形除以每个生成的三角形作为单独的按钮

[英]Dividing a square by the diagonals and making each resulting triangle a separate button

Suppose I have a square and divide it by the diagonals resulting in four identical triangles (apart from their rotation). 假设我有一个正方形,然后将其除以对角线,得到四个相同的三角形(除了旋转)。

  • Using CSS/HTML/Javascript, what is the best way to turn each triangle into a clickable area while not splitting apart the encompassing square? 使用CSS / HTML / Javascript,将每个三角形变成可点击区域而不将周围的正方形分开的最佳方法是什么?

I have been looking into creating the triangles using image maps ( <map> ), but it seems to me it only works as expected in Internet Explorer. 我一直在研究使用图像映射( <map> )创建三角形,但在我看来,它只能按预期在Internet Explorer中工作。

As another possible solution, I have tried rectangular divs mimicking the triangles by placing gradually smaller rectangles towards the center of the encompassing square from all four "corners of the world". 作为另一种可能的解决方案,我尝试通过将四个“世界角落”的较小的矩形朝着所包围的正方形的中心逐渐放置来模拟矩形的div。 However, that is not an elegant solution, and really not what I'm aiming for, since I want "smooth", not jagged, triangles. 但是,这不是一个优雅的解决方案,实际上也不是我想要的目标,因为我想要“平滑”而不是锯齿状的三角形。

I would do it like this: 我会这样做:

  1. Create a div and style it to be a square. 创建一个div并将其设置为正方形。 Use a background image to illustrate the triangles 使用背景图片来说明三角形
  2. Create a variable, square, in javascript to hold the square element 在javascript中创建一个变量square来保存square元素
  3. Get the position, height, and width of square in your js 在您的js中获取正方形的位置,高度和宽度
  4. Do some math to determine the coordinates of each triangle's vertices 做一些数学运算以确定每个三角形顶点的坐标
  5. Write a function, getQuadrant(), that determines which triangle any given point within the square is in 编写一个函数getQuadrant(),该函数确定正方形内任何给定点所在的三角形
  6. Add an event listener to click events on the square. 添加事件侦听器以单击广场上的事件。 The event listener should call the getQuadrant function 事件侦听器应调用getQuadrant函数
  7. Use a switch/case to execute whatever code you need to call conditional upon which quadrant the click lands in 使用开关/案例来执行您需要调用的任何代码,条件是单击位于哪个象限

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

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