Here is the basic idea. It can be improved with CSS sprites. Using sprites will also reduce the delay when encoding an image on a click. For an introduction to CSS Sprites, see: https://css-tricks.com/css-sprites/
I did not use sprites here because they are difficult to imitate usind palceholdit, which I used for the image.
Code entry. I will describe what it does and key functions after.
.games-sub-menu input[type=radio] { display: none } .games-sub-menu label { display: block; width: 150px; height: 100px; } .games-sub-menu label.topimgG1 { background-image: url("http://placehold.it/150x100/FF0000/FFFFFF/?text=Image1"); } .games-sub-menu input[type=radio]:checked + label.topimgG1 { background-image: url("http://placehold.it/150x100/?text=Image1"); } .games-sub-menu label.topimgG2 { background-image: url("http://placehold.it/150x100/00FF00/FFFFFF/?text=Image2"); } .games-sub-menu input[type=radio]:checked + label.topimgG2 { background-image: url("http://placehold.it/150x100/?text=Image2"); }
<div class="games-platform-item pt-item"> <ul class="games-sub-menu clearfix"> <li class="tab1 current"> <input type="radio" name="imgSwap" id="rdoImg1"> <label class="topimgG1" for="rdoImg1"></label> <span>็ผ่พ็ฒพ้</span> </li> <li class="tab2 current"> <input type="radio" name="imgSwap" id="rdoImg2"> <label class="topimgG2" for="rdoImg2"></label> <span>็ผ่พ็ฒพ้</span> </li> </ul> </div>
Basically, what happens here is a hidden switch that maintains the state of the image. From there, we use the checked CSS selector in combination with the adjacent sibling + selector to create the background image of the label. Important Note The check box must be installed immediately before the label. You can use a common selector, but the flag should be even before the label.
You can also replace radio with a checkbox if the selection is not mutually exclusive.
You can also get a similar result using the id links on the page, and use the :target selector. But this will lead to a page jump.
Some of my answers that use similar concepts:
source share