1

I am trying to split the ring into 4 equal segments. This donut ring is clickable on all parts of the segment. But when I am trying to change the skew it cannot make them equal and actually in current donut ring there is a double line on the top segment left and right border. The top and bottom segment is quite bigger compare to left and right segment.

This code snippet is what I have tried working on.

<!DOCTYPE html>
<html>
    <head>
        <style>
            .container {
                width: 200px;
                height: 200px;
            }

            .pie {
                position: relative;
                margin: 1em auto;
                border: 4px solid black;
                padding: 0;
                width: 15em;
                height: 15em;
                border-radius: 50%;
                list-style: none;
                overflow: hidden;
            }

            .slice {
                overflow: hidden;
                position: absolute;
                top: 0;
                right: 0;
                width: 50%;
                height: 50%;
                transform-origin: 0% 100%;
                border: 2px solid black;
                box-sizing: border-box;
            
            }

            .slice-contents {
                position: absolute;
                left: -100%;
                width: 200%;
                height: 200%;
                border-radius: 50%;
            }

            .slice:nth-child(1) {
                transform: rotate(-60deg) skewY(30deg) scale(1.2);
            }

            .slice:nth-child(1) .slice-contents {
                transform: skewY(-30deg); /* unskew slice contents */
                background: white;
            }

            .slice:nth-child(2) {
                transform: rotate(60deg) skewY(30deg) scale(1.2);
            }

            .slice:nth-child(2) .slice-contents {
                transform: skewY(-30deg); /* unskew slice contents */
                background: white;
            }

            .slice:nth-child(3) {
                transform: rotate(180deg) skewY(30deg) scale(1.2);
            }

            .slice:nth-child(3) .slice-contents {
                transform: skewY(-30deg); /* unskew slice contents */
                background: white;
            }

            .slice:nth-child(4) {
                transform: rotate(120deg) skewY(30deg) scale(1.2);
            }

            .slice:nth-child(4) .slice-contents {
                transform: skewY(-30deg); /* unskew slice contents */
                background: white;
            }

            .inner-pie {
                position: absolute;
                width: 7em;
                height: 7em;
                top: 50%;
                left: 50%;
                transform: translate(-50%, -50%);
                border-radius: 50%;
                border: 4px solid black;
                background: white;
            }

            #top, #bottom, #left, #right, #center { 
                cursor: pointer 
            }
        </style>
    </head>

    <body>
        <div class="container">
            <ul class='pie'>
                <li class='slice'>
                <div id="top" class='slice-contents'>click 1</button>
                </li>
                <li class='slice'>
                <div id="right" class='slice-contents'>click 2</button>
                </li>
                <li class='slice'>
                <div id="left" class='slice-contents'>click 3</button>
                </li>
                <li class='slice'>
                    <div id="bottom" class='slice-contents'>click 4</button>
                </li>
                <li id="center" class='inner-pie'>
                </li>
            <ul>
        </div>    
    </body> 

    <script>
        document.getElementById('top').onclick = function() {
            document.getElementById("top").style.backgroundColor = "red";
        };

        document.getElementById('bottom').onclick = function() {
            document.getElementById("bottom").style.backgroundColor = "red";
        };

        document.getElementById('right').onclick = function() {
            document.getElementById("right").style.backgroundColor = "red";
        };
        document.getElementById('left').onclick = function() {
            document.getElementById("left").style.backgroundColor = "red";
        };

        document.getElementById('center').onclick = function() {
            document.getElementById("center").style.backgroundColor = "red";
        };
    </script>
</html>
wolfrogers
  • 21
  • 1
  • 9

1 Answers1

2

You can achieve it with this:
skewY is actually not needed.

        document.getElementById('top').onclick = function() {
            document.getElementById("top").style.backgroundColor = "red";
        };

        document.getElementById('bottom').onclick = function() {
            document.getElementById("bottom").style.backgroundColor = "red";
        };

        document.getElementById('right').onclick = function() {
            document.getElementById("right").style.backgroundColor = "red";
        };
        document.getElementById('left').onclick = function() {
            document.getElementById("left").style.backgroundColor = "red";
        };

        document.getElementById('center').onclick = function() {
            document.getElementById("center").style.backgroundColor = "red";
        };
            .container {
                width: 200px;
                height: 200px;
            }

            .pie {
                position: relative;
                margin: 1em auto;
                border: 4px solid black;
                padding: 0;
                width: 15em;
                height: 15em;
                border-radius: 50%;
                list-style: none;
                overflow: hidden;
            }

            .slice {
                overflow: hidden;
                position: absolute;
                top: 0;
                right: 0;
                width: 50%;
                height: 50%;
                transform-origin: 0% 100%;
                border: 2px solid black;
                box-sizing: border-box;
            
            }

            .slice-contents {
                position: absolute;
                left: -100%;
                width: 200%;
                height: 200%;
                border-radius: 50%;
            }

            .slice:nth-child(1) {
                transform: rotate(-45deg) scale(1.2);
            }

            .slice:nth-child(1) .slice-contents {
                transform: skewY(-30deg); /* unskew slice contents */
                background: white;
            }

            .slice:nth-child(2) {
                transform: rotate(45deg) scale(1.2);
            }

            .slice:nth-child(2) .slice-contents {
                transform: skewY(-30deg); /* unskew slice contents */
                background: white;
            }

            .slice:nth-child(3) {
                transform: rotate(135deg) scale(1.2);
            }

            .slice:nth-child(3) .slice-contents {
                transform: skewY(-30deg); /* unskew slice contents */
                background: white;
            }

            .slice:nth-child(4) {
                transform: rotate(225deg) scale(1.2);
            }

            .slice:nth-child(4) .slice-contents {
                transform: skewY(-30deg); /* unskew slice contents */
                background: white;
            }

            .inner-pie {
                position: absolute;
                width: 7em;
                height: 7em;
                top: 50%;
                left: 50%;
                transform: translate(-50%, -50%);
                border-radius: 50%;
                border: 4px solid black;
                background: white;
            }

            #top, #bottom, #left, #right, #center { 
                cursor: pointer 
            }
        
<!DOCTYPE html>
<html>
    <head>
    </head>

    <body>
        <div class="container">
            <ul class='pie'>
                <li class='slice'>
                <div id="top" class='slice-contents'>click 1</button>
                </li>
                <li class='slice'>
                <div id="right" class='slice-contents'>click 2</button>
                </li>
                <li class='slice'>
                <div id="left" class='slice-contents'>click 3</button>
                </li>
                <li class='slice'>
                    <div id="bottom" class='slice-contents'>click 4</button>
                </li>
                <li id="center" class='inner-pie'>
                </li>
            </ul>
        </div>    
    </body> 
</html>
Joshua Ooi
  • 1,139
  • 7
  • 18